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 JAX-RS – How to read response body from a post request?

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 HK2 dependency injection (auto scanning)

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 RESTful Java client with Jersey client

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 File upload example in Jersey

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 Jersey : The ResourceConfig instance does not contain any root resource classes

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 RESTEasy client framework

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 RESTEasy – Could not find MessageBodyWriter for response object of type:xx of media type: application/xml

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 File upload example in RESTEasy

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 Download excel file from JAX-RS

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 – Could not find message body reader for type: multipart/form-data

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 RESTEasy Unable to scan WEB-INF for JAX-RS annotations ?

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 ClassNotFoundException : com.sun.jersey.spi.container.servlet.ServletContainer

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 Get HTTP header in JAX-RS

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 JAX-RS @FormParam example

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 Download pdf file from JAX-RS

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 JAX-RS @QueryParam example

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 ClassNotFoundException : org.jboss.resteasy.plugins.providers.multipart.MultipartInput

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 @MatrixParam 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 @PathParam 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 JAX-RS @Path URI matching example