Web Services JAX-RS
Last updated on Jun 15, 2020
Web Services JAX-RS:
JAX-RS instructional exercise is gives ideas and cases of REST JAX-RS API. This JAX-RS instructional exercise is intended for fledglings and experts. We can make REST JAX-RS case by pullover execution.
The two implementations of REST JAX-RS API.- Jersey
- RESTEasy
- Hello.java
- web.xml
- index.html
- HelloWorldClient.java
JAX-RS Server Code
File: Hello.java package com.javaspot.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") public class Hello { // This method is called if HTML and XML is not requested @GET @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello() { return "Hello Jersey Plain"; } // This method is called if XML is requested @GET @Produces(MediaType.TEXT_XML) public String sayXMLHello() { return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey " + "</hello>"; } // This method is called if HTML is requested @GET @Produces(MediaType.TEXT_HTML) public String sayHtmlHello() { return "<html> " + "<title>" + "Hello Jersey " + "</title>" + "<body><h1>" + "Hello Jersey HTML" + "</h1></body>" + "</html> "; } } File: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name> Jersey REST Service</servlet-name> <servlet-class>org.glassfish. jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.javaspot.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jeresy REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> File: index.html <a href="/rest/hello">Click Here</a> JAX-RS Client Code File: ClientTest.java: package com.javaspot.restclient; import java.net.URI; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import org.glassfish.jersey.client.ClientConfig; public class ClientTest { public static void main(String[] args) { ClientConfig config = new ClientConfig(); Client client = ClientBuilder.newClient(config); WebTarget target = client.target(getBaseURI()); //Now printing the server code of different media type System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_PLAIN).get(String.class)); System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_XML).get(String.class)); System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_HTML).get(String.class)); } private static URI getBaseURI() { //here server is running on 4444 port number and project name is restfuljersey return UriBuilder.fromUri("http://localhost:4444/restfuljersey").build(); } } RESTful JAX-RS Annotations:JAX-RS API gives following explanations to create RESTful applications in java. We are utilizing pullover usage for creating JAX-RS illustrations.
| Annotation | Description |
| Path | It distinguishes the URI way. It can be determined on class or technique. |
| PathParam | Represents the parameter of the URI way. |
| GET | Specifies technique reacts to GET ask. |
| POST | Specifies technique reacts to POST ask. |
| PUT | Specifies technique reacts to PUT ask. |
| HEAD | Specifies technique reacts to HEAD ask. |
| DELETE | Specifies technique reacts to DELETE ask. |
| OPTIONS | Specifies technique reacts to OPTIONS ask. |
| FormParam | Represents the parameter of the shape. |
| QueryParam | Represents the parameter of the question string of a URL. |
| HeaderParam | Represents the parameter of the header. |
| CookieParam | Represents the parameter of the treat. |
| Produces | Defines media sort for the reaction, for example, XML, PLAIN, JSON and so on. It characterizes the media sort that the strategies for an asset class or MessageBodyWriter can create. |
| Consumes | It characterizes the media sort that the techniques for an asset class or MessageBodyReader can create. |
We can download content records, picture documents, pdf records, exceed expectations records in java by JAX-RS API. To do as such we have to compose few lines of code as it were. Here, we are utilizing pullover execution for creating JAX-RS record download illustrations.
You have to determine distinctive substance sort to download diverse records. The @Produces explanation is utilized to determine the kind of record content.
- @Produces("text/plain")
- @Produces("image/png")
- @Produces("application/pdf")
- @Produces("application/vnd.ms-excel")
- @Produces("application/msword")
