Web Services JAX-RS

Click to rate this post!
[Total: 0 Average: 0]
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.

  1. Jersey
  2. 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:

  1. Hello.java
  2. web.xml
  3. index.html
  4. 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.

 

 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.

  1. @Produces(“text/plain”)
  2. @Produces(“image/png”)
  3. @Produces(“application/pdf”)
  4. @Produces(“application/vnd.ms-excel”)
  5. @Produces(“application/msword”)

RESTful JAX-RS File Upload:

Extra configuration entry in web.xml file.

<init-param>

<param-name>jersey.config.server.provider.classnames</param-name>

<param-value>org.glassfish.jersey.filter.LoggingFilter;

org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>

</init-param>

JAX-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 file

try {

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

<init-param>

<param-name>jersey.config.server.provider.classnames</param-name>

<param-value>org.glassfish.jersey.filter.LoggingFilter;

org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>Jersey REST Service</servlet-name>

<url-pattern>/rest/*</url-pattern>

</servlet-mapping>

</web-app>

File: index.html

 <h2>File Upload Example</h2>

<form action=”rest/files/upload” method=”post” enctype=”multipart/form-data”>

<p>

Select a file : <input type=”file” name=”file” size=”45″ />

</p>

<input type=”submit” value=”Upload File” />

</form>

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

  • python training
    Python Certification Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.


100% Secure Payments. All major credit & debit cards accepted.

Call Now Button