0

I just need to know how to manipulate url parameters when someone calls my API with parameters.

http://localhost:8100/apis/employee?firstName=john&lastName=Do 

I want to use these firstName and lastName parameters on my GET method but I don't know how to extract them.

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getEmployee() {

//Just need to extract the parameters in here so I can use on the logic to return only the ones that meet the requirements. 

}

Any help would be appreciated. I am using SpringBootApplication on Java

3
  • What language are you using? Commented Jan 8, 2018 at 15:47
  • I am using SpringBootApplication on Java Commented Jan 8, 2018 at 15:49
  • Don't know much about Java, maybe this older answer helps you out Commented Jan 8, 2018 at 15:54

3 Answers 3

2

By the annotations shown in your question, you are using JAX-RS (probably with Jersey). So use @QueryParam:

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getEmployee(@QueryParam("firstName") String firstName, 
                            @QueryParam("lastName") String lastName) {
   ...
}

For Spring MVC (that also provides REST capabilities), you want to use @RequestParam:

@RequestMapping(method = RequestMethod.GET,
                produces = { MediaType.APPLICATION_XML_VALUE, 
                             MediaType.APPLICATION_JSON_VALUE })
public Response getEmployee(@RequestParam("firstName") String firstName, 
                            @RequestParam("lastName") String lastName) {
   ...
}

Both JAX-RS (with Jersey) and Spring MVC can be used with Spring Boot. See the documentation for details.

Also refer to this answer for details on the differences between Spring MVC and JAX-RS.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes that is correct, I am using JAX-RS. I have a question. If I want to pull an employee by ID, I can use employeeRepository.findOne() where employeeRepository is an interface extending PagingAndSortingRepository. How about if I want to select an employee based on firstName or lastName what do I do? Do I implement my method on employeeRepository interface?? I am asking just because I don't want to reinvent the wheel :)
@DoArNa That's out of the scope for your current question, so you should consider asking a new one. However, it would be something like Employee findByFirstNameIgnoreCaseAndLastNameIgnoreCase(String firstName, String lastName).
It seems it is not recognizing the firstName and lastName on "@QueryParam". Do I have to add any annotation "@Path" or something like that? If so, how would that look like?
0

@RequestParam is what you are looking for

public Response getEmployee(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName) {
...
}

1 Comment

The OP is using Spring Boot but @GET and @Produces belong to JAX-RS (which can be used with Spring). So the correct annotation is @QueryParam and not @RequestParam. See my answer for further details.
0

You can use @RequestParam in your controller method to access the query parameters. e.g.

public Response getEmployee(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName)

1 Comment

The OP is using Spring Boot but @GET and @Produces belong to JAX-RS (which can be used with Spring). So the correct annotation is @QueryParam and not @RequestParam. See my answer for further details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.