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
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.