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
JAX-RS Example Jersey:We can make JAX-RS case by pullover execution. To do as such, you have to stack shirt jostle records or utilize expert system.
JAX-RS example: - Hello.java
- web.xml
- index.html
- HelloWorldClient.java
JAX-RS Server Code
File: Hello.javapackage 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 "" + "
Hello Jersey " + "";}// This method is called if HTML is requested@GET@Produces(MediaType.TEXT_HTML)public String sayHtmlHello() {return " " + "
" + "Hello Jersey " + ""+ "
" + "Hello Jersey 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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"
><servlet>
Jersey REST Serviceorg.glassfish. jersey.servlet.ServletContainerjersey.config.server.provider.packagescom.javaspot.rest1Jeresy REST Service/rest/*File: index.html <a href="rest/hello"
>Click Here
a>JAX-RS Client CodeFile: 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 typeSystem.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. |
RESTful JAX-RS File Download: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")
RESTful JAX-RS File Upload:Extra configuration entry in web.xml file.<init-param>
jersey.config.server.provider.classnamesorg.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeatureJAX-RS File Upload
File: FileUploadService.java:
package com.javaspot.rest;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;import org.glassfish.jersey.media.multipart.FormDataContentDisposition;import org.glassfish.jersey.media.multipart.FormDataParam;@Path("/files")public class FileUploadService {@POST@Path("/upload")@Consumes(MediaType.MULTIPART_FORM_DATA)public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,@FormDataParam("file") FormDataContentDisposition fileDetail) {String fileLocation = "e://" + fileDetail.getFileName();//saving filetry {FileOutputStream out = new FileOutputStream(new File(fileLocation));int read = 0;byte[] bytes = new byte[1024];out = new FileOutputStream(new File(fileLocation));while ((read = uploadedInputStream.read(bytes)) != -1) {out.write(bytes, 0, read);}out.flush();out.close();}
catch (IOException e) {e.printStackTrace();}String output = "File successfully uploaded to : " + fileLocation;
return Response.status(200).entity(output).build();}}
File: web.xmlJersey REST Serviceorg.glassfish.jersey.servlet.ServletContainerjersey.config.server.provider.packagescom.javaspot.restjersey.config.server.provider.classnamesorg.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature1Jersey REST Service<url-pattern>/rest/*File: index.html File Upload Example