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

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
DoArNa
  • 491
  • 2
  • 9
  • 29

3 Answers3

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.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • 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 Jan 08 '18 at 16:38
  • @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)`. – cassiomolin Jan 08 '18 at 16:49
  • 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? – DoArNa Jan 08 '18 at 20:06
0

@RequestParam is what you are looking for

public Response getEmployee(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName) {
...
}
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
  • 1
    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](https://stackoverflow.com/a/48154062/1426227) for further details. – cassiomolin Jan 08 '18 at 16:16
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)
codebrane
  • 4,290
  • 2
  • 18
  • 27
  • 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](https://stackoverflow.com/a/48154062/1426227) for further details. – cassiomolin Jan 08 '18 at 16:16