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

jakarta.activation.DataSource was not found

Using Jersey 3x + Jetty to develop endpoints, but hits the jakarta.activation.DataSource warning during application startup? Terminal SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details. May 27, 2021 10:09:48 AM org.glassfish.jersey.message.internal.MessagingBinders$EnabledProvidersBinder bindToBinder WARNING: A class jakarta.activation.DataSource for a default provider MessageBodyWriter<jakarta.activation.DataSource> was not found. …

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

Jersey + Spring integration example

This tutorial show you how to integrate Jersey web application with Spring framework. Technologies used : Jersey 1.8 Spring 3.0.5.RELEASE Eclipse 3.6 Maven 3 1. Project Dependency Declares Jersey 1.8, Spring3 and “jersey-spring.jar” dependencies in Maven pom.xml file. Note In “jersey-spring.jar” version, it will download all the Spring 2.5.6 dependencies. To use Spring 3, you …

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

XML example with Jersey + JAXB

This tutorial show you how to use JAXB to convert object to XML in Jersey, and return it back to user. 1. Dependency To integrate JAXB with Jersey, no extra dependency is required. Just include “jersey-server.jar” will do. 2. JAXB Annotation Annotate object with JAXB annotation, for conversion later. import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; …

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

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