RESTful Web Service - JAX-RS Annotations - Contents:
Annotation | Package Detail/Import statement |
---|---|
@GET | import javax.ws.rs.GET; |
@Produces | import javax.ws.rs.Produces; |
@Path | import javax.ws.rs.Path; |
@PathParam | import javax.ws.rs.PathParam; |
@QueryParam | import javax.ws.rs.QueryParam; |
@POST | import javax.ws.rs.POST; |
@Consumes | import javax.ws.rs.Consumes; |
@FormParam | import javax.ws.rs.FormParam; |
@PUT | import javax.ws.rs.PUT; |
@DELETE | import javax.ws.rs.DELETE; |
As stated earlier in Example Application, we are using Jersey for RESTful Web services and JAX-RS annotations.
REST follows one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP methods.
|
@GET
Annotate your Get request methods with @GET.
1
2
3
4
| @GET public String getHTML() { ... } |
@Produces
@Produces annotation specifies the type of output this method (or web service) will produce.
1
2
3
4
5
| @GET @Produces ( "application/xml" ) public Contact getXML() { ... } |
1
2
3
4
5
| @GET @Produces ( "application/json" ) public Contact getJSON() { ... } |
@Path
@Path annotation specify the URL path on which this method will be invoked.
1
2
3
4
5
6
| @GET @Produces ( "application/xml" ) @Path ( "xml/{firstName}" ) public Contact getXML() { ... } |
@PathParam
We can bind REST-style URL parameters to method arguments using @PathParam annotation as shown below.
1
2
3
4
5
6
7
| @GET @Produces ( "application/xml" ) @Path ( "xml/{firstName}" ) public Contact getXML( @PathParam ( "firstName" ) String firstName) { Contact contact = contactService.findByFirstName(firstName); return contact; } |
1
2
3
4
5
6
7
| @GET @Produces ( "application/json" ) @Path ( "json/{firstName}" ) public Contact getJSON( @PathParam ( "firstName" ) String firstName) { Contact contact = contactService.findByFirstName(firstName); return contact; } |
@QueryParam
Request parameters in query string can be accessed using @QueryParam annotation as shown below.
1
2
3
4
5
6
7
| @GET @Produces ( "application/json" ) @Path ( "json/companyList" ) public CompanyList getJSON( @QueryParam ( "start" ) int start, @QueryParam ( "limit" ) int limit) { CompanyList list = new CompanyList(companyService.listCompanies(start, limit)); return list; } |
@POST
Annotate POST request methods with @POST.
1
2
3
4
5
6
| @POST @Consumes ( "application/json" ) @Produces ( "application/json" ) public RestResponse<Contact> create(Contact contact) { ... } |
@Consumes
The @Consumes annotation is used to specify the MIME media types a REST resource can consume.
1
2
3
4
5
6
7
| @PUT @Consumes ( "application/json" ) @Produces ( "application/json" ) @Path ( "{contactId}" ) public RestResponse<Contact> update(Contact contact) { ... } |
@FormParam
The REST resources will usually consume XML/JSON for the complete Entity Bean. Sometimes, you may want to read parameters sent in POST requests directly and you can do that using @FormParam annotation. GET Request query parameters can be accessed using@QueryParam annotation.
1
2
3
4
5
| @POST public String save( @FormParam ( "firstName" ) String firstName, @FormParam ( "lastName" ) String lastName) { ... } |
@PUT
Annotate PUT request methods with @PUT.
1
2
3
4
5
6
7
| @PUT @Consumes ( "application/json" ) @Produces ( "application/json" ) @Path ( "{contactId}" ) public RestResponse<Contact> update(Contact contact) { ... } |
@DELETE
Annotate DELETE request methods with @DELETE.
1
2
3
4
5
6
| @DELETE @Produces ( "application/json" ) @Path ( "{contactId}" ) public RestResponse<Contact> delete( @PathParam ( "contactId" ) int contactId) { ... }
|
No comments :
Post a Comment