简体   繁体   中英

Implementing RESTful web service with two parameters?

I am writing Jersey RESTful web services. I have below two web methods.

@Path("/persons")
public class PersonWS {
    private final static Logger logger = LoggerFactory.getLogger(PersonWS.class);

    @Autowired
    private PersonService personService;

    @GET
    @Path("/{id}")
    @Produces({MediaType.APPLICATION_XML})
    public Person fetchPerson(@PathParam("id") Integer id) {
        return personService.fetchPerson(id);
    }


}

Now i need to write one more web method which takes two parameters one is id and one more is name. It should be as below.

public Person fetchPerson(String id, String name){

}

How can i write a web method for above method?

Thanks!

You have two choices - you can put them both in the path or you can have one as a query parameter.

ie do you want it to look like:

/{id}/{name}

or

/{id}?name={name}

For the first one just do:

@GET
@Path("/{id}/{name}")
@Produces({MediaType.APPLICATION_XML})
public Person fetchPerson(
          @PathParam("id") Integer id,
          @PathParam("name") String name) {
    return personService.fetchPerson(id);
}

For the second one just add the name as a RequestParam . You can mix PathParam s and RequestParam s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM