JAX-RS – How to read response body from a post request?

In JAX-RS, we can use response.readEntity(String.class) to read the response body from a post request. import jakarta.ws.rs.core.Response; Response response = //… // read response body String body = response.readEntity(String.class); P.S Tested with Jersey 3.x 1. Problem Below is a JAX-RS POST request endpoint to accept a JSON input and return a JSON response. // POST, …

Read more

Jersey and JSON examples (EclipseLink MOXy)

This article shows how to return a JSON response in the Jersey application, using EclipseLink MOXy. Tested with Jersey 3.0.2 EclipseLink MOXy 3 Jetty 11, HTTP Server Java 11 Maven 3 SLF4J, Logback, redirect Jersey J.U.L logs to logback JUnit 5 and JSONassert 1.5 (Unit Test) org.json 20210307, JSONObject Table of contents 1. EclipseLink MOXy …

Read more

Jersey and HK2 dependency injection (auto scanning)

This article shows how to use HK2 dependency injection framework in Jersey and enable auto-scanning auto-discovery of the declared @Contract and @Service components. Table of contents 1. Jersey and HK2 dependency injection 2. @Contract, @Service, and @Inject 3. HK2 manual register @Contract and @Service 4. HK2 auto scanning @Contract and @Service 4.1 HK2 inhabitant files …

Read more

Jersey and Jetty HTTP Server examples

This article shows how to start a Jetty HTTP Sever to run a JAX-RS or Eclipse Jersey application. Tested with Jersey 3.0.2 Jetty 11 Jackson 2.12.2 JUnit 5.4.0 (unit test) JSONassert 1.5.0 (unit test) Maven 3.8.3 Java 11 Tables of contents 1. Using Jersey with Jetty HTTP Server 2. Project Directory 3. Project dependencies 4. …

Read more

RESTful Java client with Jersey client

This tutorial show you how to use Jersey client APIs to create a RESTful Java client to perform “GET” and “POST” requests to REST service that created in this “Jersey + Json” example. 1. Jersey Client Dependency To use Jersey client APIs, declares “jersey-client.jar” in your pom.xml file. File : pom.xml <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.8</version> …

Read more

Jersey and JSON examples (Jackson)

This article shows how to return a JSON response in the Jersey application, using Jackson 2.x. Tested with Jersey 3.0.2 Grizzly 3 HTTP Server Jackson 2.12.2 Java 11 Maven JUnit 5 and JSONassert 1.5 (Unit Test) Table of contents 1. Jackson as the JSON provider in Jersey 2. Project Directory 3. Project dependencies 4. Jersey …

Read more

File upload example in Jersey

In this tutorial, we show you how do to file upload with Jersey, JAX-RS implementation. 1. Jersey Multipart Dependency To support multipart (file upload) in Jersey, you just need to include “jersey-multipart.jar” in Maven pom.xml file. <project …> <repositories> <repository> <id>maven2-repository.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> </repositories> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> …

Read more

Jersey hello world example

This article shows how to start a Grizzly HTTP server to run a JAX-RS or Eclipse Jersey application. Tested with Jersey 3.0.2 Grizzly 3 HTTP server Java 8 Maven JUnit 5 Table of contents 1. Project Directory 2. Jersey dependencies 3. Jersey and HK2 dependency injection 4. Jersey endpoints 5. Start Jersey application 6. Demo …

Read more

Jersey : The ResourceConfig instance does not contain any root resource classes

Problem Deploying Jersey REST service, hit following error message on Tomcat. SEVERE: Servlet /RESTfulExample threw load() exception com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. //… code omitted Here’s the web.xml <web-app …> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.mkyong.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> Solution Many reasons …

Read more

RESTful Java client with RESTEasy client framework

This tutorial show you how to create a RESTful Java client with RESTEasy client framework, to perform “GET” and “POST” requests to REST service that created in last “Jackson + JAX-RS” tutorial. 1. RESTEasy Client Framework RESTEasy client framework is included in RESTEasy core module, so, you just need to declares the “resteasy-jaxrs.jar” in your …

Read more

RESTful Java client with Apache HttpClient

Apache HttpClient is a robust and complete solution Java library to perform HTTP operations, including RESTful service. In this tutorial, we show you how to create a RESTful Java client with Apache HttpClient, to perform a “GET” and “POST” request. Note The RESTful services from last “Jackson + JAX-RS” article will be reused. 1. Get …

Read more

RESTful Java client with java.net.URL

In this tutorial, we show you how to create a RESTful Java client with Java build-in HTTP client library. It’s simple to use and good enough to perform basic operations for REST service. The RESTful services from last “Jackson + JAX-RS” article will be reused, and we will use “java.net.URL” and “java.net.HttpURLConnection” to create a …

Read more

XML example with RESTEasy + JAXB

RESTEasy, is required JAXB to support XML file. In this tutorial, we show you how to create an “user” object, convert it into XML file, and return it back to the client. 1. RESTEasy + JAXB To use JAXB in RESTEasy, you need to include the “resteasy-jaxb-provider.jar” dependency. File : pom.xml <repositories> <repository> <id>JBoss repository</id> …

Read more

RESTEasy – Could not find MessageBodyWriter for response object of type:xx of media type: application/xml

Problem Developing RESTEasy + JAXB provider to support XML, when return it back to client, it prompts following error message : org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: com.mkyong.rest.User of media type: application/xml at org.jboss.resteasy.core.ServerResponse.writeTo(ServerResponse.java:216) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:500) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) //… Solution To use JAXB in RESTEasy, you need to include “resteasy-jaxb-provider.jar“. <repositories> …

Read more

File upload example in RESTEasy

Not many complete file upload example in JAX-RS, especially RESTEasy. Here, we show you two complete RESTEasy examples to handle file upload from HTML form. Normal way to handle uploaded file via MultipartFormDataInput Map uploaded file to a POJO class via @MultipartForm 1. RESTEasy Multipart Dependency In RESTEasy, you need “resteasy-multipart-provider.jar” to handle multipart file …

Read more

Download excel file from JAX-RS

In JAX-RS, for excel file, annotate the method with @Produces(“application/vnd.ms-excel”) : Put @Produces(“application/vnd.ms-excel”) on service method. Set “Content-Disposition” in Response header to prompt a download box. 1. Download Excel file in JAX-RS Full example to download an excel file from JAX-RS. import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/excel") public …

Read more

RESTEasy – Could not find message body reader for type: multipart/form-data

Note Tested with Eclipse 3.6, Maven 3 and RESTEasy 2.2.1.GA Question Developing file upload feature with RESTEasy, see following RESTEasy multipart file upload example : public class FileUploadForm { private byte[] data; @FormParam("file") public void setData(byte[] data) { this.data = data; } //…code omitted } @Path("/file") public class UploadFileService { @POST @Path("/upload") @Consumes("multipart/form-data") public Response …

Read more

RESTEasy Unable to scan WEB-INF for JAX-RS annotations ?

Question Developing REST service, with file upload function with resteasy, after added resteasy multipart dependency, following strange error message prompt during the application start up? P.S Using resteasy-jaxrs and resteasy-multipart version 2.2.1.GA. SEVERE: Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap java.lang.RuntimeException: Unable to scan WEB-INF for JAX-RS annotations, you must manually …

Read more

Download image file from JAX-RS

In JAX-RS, for user to download an image file, annotate the method with @Produces(“image/image-type”) : Put @Produces(“image/png”) on service method, for “png” image. Set “Content-Disposition” in Response header to prompt a download box. Note For other image types, refer to this list of the image types 1. Download Image in JAX-RS Full example to download …

Read more

Download text file from JAX-RS

In JAX-RS, for user to download a file, annotate the method with @Produces(“text/plain”) : Put @Produces(“text/plain”) on service method, with a Response return type. It means the output is a text file. Set “Content-Disposition” in Response header to tell browser pop up a download box for user to download. 1. Download File in JAX-RS See …

Read more

ClassNotFoundException : com.sun.jersey.spi.container.servlet.ServletContainer

Problem In Jersey development, hit following error message on Tomcat. SEVERE: Servlet /RESTfulExample threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) //… Here’s the Maven pom.xml <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.8</version> </dependency> Solution The “com.sun.jersey.spi.container.servlet.ServletContainer” is included in “jersey-server.jar“, not “jersey-core.jar“. Actually, to develop REST service with Jersey, you just need to include “jersey-server.jar“, it …

Read more

Get HTTP header in JAX-RS

In this tutorial, we show you two ways to get HTTP request header in JAX-RS : Inject directly with @HeaderParam Pragmatically via @Context Note Refer to this wiki page for list of the HTTP header fields. 1. @HeaderParam Example In this example, it gets the browser “user-agent” from request header. import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import …

Read more

JAX-RS @FormParam example

In JAX-RS, you can use @FormParam annotation to bind HTML form parameters value to a Java method. The following example show you how to do it : 1. HTML Form See a simple HTML form with “post” method. <html> <body> <h1>JAX-RS @FormQuery Testing</h1> <form action="rest/user/add" method="post"> <p> Name : <input type="text" name="name" /> </p> <p> …

Read more

Download pdf file from JAX-RS

In JAX-RS, for pdf file, annotate the method with @Produces(“application/pdf”) : Put @Produces(“application/pdf”) on service method. Set “Content-Disposition” in Response header to prompt a download box. 1. Download Pdf file in JAX-RS Full example to download a pdf file from JAX-RS. import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/pdf") public …

Read more

JAX-RS @QueryParam example

In JAX-RS, you can use @QueryParam annotation to inject URI query parameter into Java method. for example, /users/query?url=mkyong.com In above URI pattern, query parameter is “url=mkyong.com“, and you can get the url value with @QueryParam(“url”). 1. @QueryParam example See a full example of using @QueryParam in JAX-RS. import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; …

Read more

ClassNotFoundException : org.jboss.resteasy.plugins.providers.multipart.MultipartInput

Question Working REST + file upload, using resteasy 2.2.1.GA and Eclipse 3.6. And the reseteasy multipart dependency is declared in maven pom.xml file. In compile mode, class “MultipartInput” is able to compile, but Eclipse prompts following error message during deployment or debugging mode? java.lang.NoClassDefFoundError: org/jboss/resteasy/plugins/providers/multipart/MultipartInput at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2427) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) Caused by: java.lang.ClassNotFoundException: …

Read more

JAX-RS @MatrixParam example

Matrix parameters are a set of “name=value” in URI path, for example, /books/2011;author=mkyong In above URI, the matrix parameter is “author=mkyong“, separate by a semi colon “;“. 1. @MatrixParam example See a full example of using @MatrixParam in JAX-RS. import javax.ws.rs.GET; import javax.ws.rs.MatrixParam; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/books") public class BookService { @GET …

Read more

JAX-RS @PathParam example

In JAX-RS, you can use @PathParem to inject the value of URI parameter that defined in @Path expression, into Java method. 1. @PathParam – Single Parameter A simple and normal way to use @PathParam. import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/users") public class UserRestService { @GET @Path("{id}") public Response getUserById(@PathParam("id") String id) { …

Read more

JAX-RS @Path URI matching example

In JAX-RS, you can use @Path to bind URI pattern to a Java method. See following examples to show you how it works. 1. Normal URI Matching See normal URI matching with @Path annotation. import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response; @Path("/users") public class UserRestService { @GET public Response getUser() { return Response.status(200).entity("getUser is called").build(); } …

Read more

RESTEasy hello world example

RESTEasy, JBoss project, implementation of the JAX-RS specification. In this tutorial, we show you how to use RESTEasy framework to create a simple REST style web application. Technologies and Tools used in this article: RESTEasy 2.2.1.GA JDK 1.6 Maven 3.0.3 Eclipse 3.6 What’s REST? Read this, this and this to understand what’s REST. 1. Directory …

Read more