Just starting with Jersey, I've been trying to reproduce the simple example in the latest Jersey documentation 'building responses'. This part, as far as I understand, should show how Response and ResponseBuilder can be used to easily return a response in combination with Entity<T> for response content.
Now, the documentation states that several data types are by default supported (here: 'Representations and Java types'). String prime among them, matching any media type.
Of all the variations I've tried, the following is the simplest:
#POST
public Response post() {
URI createdUri;
try {
createdUri = new URI("http://test.lan");
} catch (final URISyntaxException e) {
throw new WebApplicationException(e);
}
return Response.created(createdUri).entity(Entity.text("someContent")).build();
}
I've always gotten the same error (full stacktrace below) on calling the request:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/plain, type=class javax.ws.rs.client.Entity, genericType=class javax.ws.rs.client.Entity.
I believe it's saying that a suitable provider wasn't found for this Entity generic-type. However, String should be supported OOTB?
I found that StringMessageProvider is probably the Jersey 1 implementation of this provider, and the closest related classes I found in my Jersey 2 libraries arer the classes in org.glassfish.jersey.message.internal in jersey-common. Among the many providers there is the StringMessageProvider, which appears to me like a potential intended provider for this.
I've looked up the issue, and while there's plenty of people who get this when incorrectly trying to use a custom Provider, I found nothing about the default OOTB providers not working..
I've checked my libs, and right now I have the following dependencies in my pom (among others):
jersey-container-servlet-core
jersey-client
jersey-common
jersey-server
I've looked online but this seems to be all I need, although I have not with certainty found the right provider classes for String and JAXB/JSON in the jars.
Context
Maven project
with tomcat servlet-api 6.0.29
Version 2.6 of all mentioned jersey libs
Eclipse kepler
Using tomcat6 maven plugin to run embedded tomcat (works fine so far)
Fiddler request used to test
POST HTTP/1.1
User-Agent: Fiddler
Host: 127.0.0.1
Content-Length: 0
And again tried several variations.
Full stacktrace
06-Jan-2015 21:13:54 org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/plain, type=class javax.ws.rs.client.Entity, genericType=class javax.ws.rs.client.Entity.
06-Jan-2015 21:13:54 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet TestService threw exception
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/plain, type=class javax.ws.rs.client.Entity, genericType=class javax.ws.rs.client.Entity.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:368)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:319)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
EDIT
The same error (for application/json) ocurs now that I've annotated a class with #XmlRootElement and try to return it in a method per the Jersey docs:
#GET
#Produces(MediaType.APPLICATION_JSON)
public Foo sampleFoo() {
Foo foo = new Foo();
return foo;
}
Where Foo is annotated with #XmlRootElement.
I also added jersey-media-json-jackson as dependency, which I can see contains an explicit JSONJaxb provider. However, it doesn't seem to be picked up somehow.
First Issue:
javax.ws.rs.client.Entity is a client side class. The JAX-RS spec doesn't say anything about its usage on the server side. But I can confirm with many different tests, that the result will be similar to what you are seeing (with Jersey at least). With Resteasy, it will just send out the Entity.toString()
Since this doesn't work for either Resteasy or Jersey, I won't say it's a bug, but possible a mistake in the Jersey documentation, which exampled it usage as follows:
#POST
#Consumes("application/xml")
public Response post(String content) {
URI createdUri = ...
String createdContent = create(content);
return Response.created(createdUri)
.entity(Entity.text(createdContent)).build();
}
The above failed for me also. But you are not wrong in saying
...several data types are by default supported
as they are. To get your example to work, simply change the Entity.text("someContent") to simply "someContent"
return Response.created(createdUri).entity("someContent").build();
And just for completeness, client side usage might look something like
Response response = webTarget.request().post(Entity.text("Hello World"));
which works just fine.
Second Issue:
Up until (I believe) Jersey 2.9, the jersey-media-json-jackson module is not auto-configured. So with 2.6, we need to set up the configuration either through package scanning in the web.xml or in the Application subclass. Either way, a web.xml is required. As stated here in regards to a 2.x servlet environment, which Tomcat 6 is.
In Servlet 2.5 environment, you have to explicitly declare the Jersey container Servlet in your Web application's web.xml deployment descriptor file.
So to scan for the JSON provider classes, you should specify the package in the jersey.config.server.provider.packages init-param. An example web.xml would be something like this
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
thepackage.of.your.resources,
org.codehaus.jackson.jaxrs <!-- Jackson providers -->
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
You are also allowed to use an Application subclass (which ResourceConfig extends from). We just need to specify it in the web.xml. An example configuration might be something like
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(JacksonFeature.class);
packages("thepackage.of.your.resources");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>jersey2.tomcat6.MyApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
Note: All of this was tested against your same environment, besides using Eclipse. I am using Netbeans, though it shouldn't make any difference. Also the only Maven dependencies I needed were
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<jersey.version>2.6</jersey.version>
On another note, to simplify development, I just created a simple Maven archetype with the following coordinates
GroupId: org.glassfish.jersey.archetypes
ArtifactId: jersey-quickstart-webapp
Version: 2.6
You can also see Creating a New Project from Maven Archetype
As far as the text/plain format, does this work?
return Response.created(createdUri).type(MediaType.TEXT_PLAIN).entity("someContent").build();
For JSON output, I have these dependencies
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
as well as a jaxb implementation. Any will do, I use
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</dependency>
I also define an object mapper provider, though I'm not 100% sure that it's required (unless you want to customize):
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
#Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
ObjectMapper mapper;
public ObjectMapperProvider() {
mapper = new ObjectMapper();
mapper.configure( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false );
mapper.configure( SerializationFeature.INDENT_OUTPUT, true );
mapper.configure( SerializationFeature.WRITE_NULL_MAP_VALUES, true );
mapper.configure( SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true );
mapper.setAnnotationIntrospector( new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()) );
}
#Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
Also, I think you need to register the Jackson feature:
#ApplicationPath("")
public class Application extends ResourceConfig {
public Application() {
register( JacksonFeature.class );
}
}
I should note that this is all configured using Jersey 2.11.
In my case, I added the jersey-media-json-jackson dependency. It worked for me.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.20</version>
</dependency>
Related
I am developing some RESTful webservices using the following technologies
* Java 1.7
* Jersey 2.4.1
* spring 3.2.1
* Jboss-5.1.0.GA
So far, I have used eclipse to build the war file, both an exploded version and a compressed version. I have copied the files one at a time to {jboss-home}/server/web/deploy and have started the server using the command line by running run.bat -c web. In both cases the server starts up without error and the log files are identical. (timestamps excluded)
When I deploy the exploded war file and then navigate to the url http://mydomain.com:8080/{app}/rest/application.wadl, jersey is correctly identifying the annotated resources and I can use them as expected.
jersey annotated
#Path("/v1/ping")
#Service("pingV1")
public class PingV1 extends BaseResource {
#Autowired
private PingBusiness pingBusiness;
#GET
#Produces(MediaType.APPLICATION_XML)
public PingVO getPing(
#Context UriInfo uriInfo,
#Context HttpServletRequest httpServletRequest){
...
PingVO pingVO = pingBusiness.doGet();
...
return pingVO;
}
}
application.wadl (dynamically generated)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.4.1 2013-11-08 12:08:47"/>
<grammars>
<include href="application.wadl/xsd0.xsd">
<doc xml:lang="en" title="Generated"/>
</include>
</grammars>
<resources base="http://mydomain.com:8080/{app}/rest/">
<resource path="/v1/ping">...</resource>
<resource path="/">...</resource>
<resource path="application.wadl">...</resource>
</resources>
</application>
However, when I deploy the compressed .war file and navigation to the same url http://mydomain.com:8080/{app}/rest/application.wadl, jersey it not finding the annotated resource and when I hit the expected urls jboss returns a 404 - NOT FOUND.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.4.1 2013-11-08 12:08:47"/>
<grammars/>
<resources base="http://mydomain.com:8080/{app}/rest/">
<resource path="application.wadl">...</resource>
</resources>
</application>
Lastly, I used winRAR to extract the contents of the compressed war file into a folder, then used Beyond Compare to do a folder comparison of the original exploded war to the contexts that were just extracted and they were IDENTICAL.
This leads me to believe that there is something unique about how jboss deploys exploded wars vs compressed war files, but I don't understand what that may be or why it behaves this way. Can anyone help or point me in a new direction?
While a simple response could be ok, well just deploy the exploded war file, I would like to understand why this happens and continue to use my existing build.xml which produces the compressed .war file.
You can try this:
public class MyApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(HelloWorldResource.class);
return s;
}
}
or other examples from the Jersey documentation if you want to use reflection to get the resources.
You may have a condition where the unpacking of the war is interfering with the jersey autoscan process and this should fix it.
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.xyz.app.rest.MyApplication</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.xyz.app.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</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>
package com.xyz.app.rest;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class MyApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(MyDataResource.class);
return s;
}
}
I am trying to build a simple hello world application for two days using Jersey + Google app engine. For simple AppEngine project I followed these tutorials and both works just fine
https://developers.google.com/appengine/docs/java/gettingstarted/creating
https://developers.google.com/appengine/docs/java/webtoolsplatform
But now I am trying to add Jersey and following this tutorial http://www.vogella.com/articles/REST/article.html.
But server keeps giving me
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
when I add these lines in 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestServer</display-name>
<servlet>
<servlet-name>Jersey REST Service</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.test.myproject</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>
I have downloaded Jersey JAX-RS 2.1 RI bundle from here and have added all jar files in WEB-INF/lib folder as described in tutorial. And even after two days nothing is working. I have searched several times on Google and apparently people who are using Maven have solved it somehow but I am not using Maven neither did the guy who wrote that tutorial.
Just to check if even com.sun.jersey.spi.container.servlet.ServletContainer exists in imported Jersey jars I tried to just write this fully qualified name in Java and let the intellisense finish names but I couldn't get any intellisense after com.sun.je so my last guess is that there have been some package rearrangement in latest Jersey build and jersey is no longer inside com.sun. I am exhausted and I would appreciate any kind of help.
You have downloaded Jersey 2 (which RI of JAX-RS 2). The tutorial you're referring to uses Jersey 1. Download Jersey 1.17.1 from (here), should be sufficient for you.
Jersey 1 uses com.sun.jersey, and Jersey 2 uses org.glassfish.jersey hence the exception.
Also note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.
Edit
Registering Resources and Providers in Jersey 2 contains additional info on how to register classes/instances in Jersey 2.
If you are using jersey 2.x then you need different configuration in web.xml as servlet class is change in it. you can update your web.xml with following configuration.
<servlet>
<servlet-name>myrest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>your.package.path</param-value>
</init-param>
<init-param>
<param-name>unit:WidgetPU</param-name>
<param-value>persistence/widget</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myrest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Add this in pom
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17.1</version>
</dependency>
It's an eclipse setup issue, not a Jersey issue.
From this thread ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
Right click your eclipse project Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Gradle Dependencies -> Finish.
So Eclipse wasn't using the Gradle dependencies when Apache was starting .
I also faced a similar issue. Resolved the problem by going through the step step tutorial from the below link.
http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-hello-world-example/
The main thing to notice is that the jersey libraries should be placed correctly in TOMCAT WEB-INF/lib folder. It is done automatically by the Eclipse settings mentioned in the above link. It will create a WAR file with the dependent JAR Files. Else, you will run into problems with ClassNotFound Exception.
apache-tomcat-7.0.56-windows-x64\apache
-tomcat-7.0.56\webapps\JerseyJSONExample\WEB-INF\lib
"11/23/2014 12:06 AM 130,458 jersey-client-1.9.jar
11/23/2014 12:06 AM 458,739 jersey-core-1.9.jar
11/23/2014 12:06 AM 147,952 jersey-json-1.9.jar
11/23/2014 12:06 AM 713,089 jersey-server-1.9.jar"
4 File(s) 1,450,238 bytes
The second tutorial explains about how to create a Webservice which produces and consumes JSON output.
http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/
Both the links gave a good picture on how things work and save a lot of time.
try this :
org.glassfish.jersey.servlet.ServletContainer
on servlet-class
I had the same problem as you though I have followed a different guide: http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/
The strange part is that, in this guide I have used, I should not have any problem with compatibility between versions (1.x against 2.x) because following the guide you use the jersey 1.8.x on pom.xmland in the web.xmlyou refer to a class (com.sun.jersey.spi.container.servlet.ServletContainer) as said before of 1.x version. So as I can infer this should be working.
My guess is because I'm using JDK 1.7 this class does not exist anymore.
After, I tried to resolve with the answers before mine, did not helped, I have made changes on the pom.xmland on the web.xml the error changed to: java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
Which supposedly should be exist!
As result of this error, I found a "new" solution: http://marek.potociar.net/2013/06/13/jax-rs-2-0-and-jersey-2-0-released/
With Maven (archetypes), generate a jersey project, likes this:
mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.0
And it worked for me! :)
We get this error because of build path issue. You should add "Server Runtime" libraries in Build Path.
"java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer"
Please follow below steps to resolve class not found exception.
Right click on project --> Build Path --> Java Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0
I encountered the same error today although I was using Jersey 1.x, and had the right jars in my classpath. For those who'd like to follow the vogella tutorial to the letter, and use the 1.x jars, you'd need to add the jersey libraries to WEB-INF/lib folder. This will certainly resolve the problem.
you need to add jersey-bundle-1.17.1.jar to lib of project
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<!-- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> -->
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<!-- <param-name>jersey.config.server.provider.packages</param-name> -->
<param-value>package.package.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
You must replace in your web.xml:
<servlet>
<servlet-name>Jersey REST Service</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.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
for this:
<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.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
this is Jersey 2.x uses org.glassfish.jersey packages instead of com.sun.jersey (which is used by Jersey 1.x) and hence the exception. Note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.x once you migrate to JAX-RS 2.0 and Jersey 2.x
if at any moment you use maven, your pom.xml would be this:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.X</version>
</dependency>
replace 2.X for your desire version, e.g. 2.15
A simple workaround is , check whether you have dependencies or libs in deployment assembly of eclipse.probably if you are using tomcat , the server might not have identified the libs we are using . in that case specify it explicitly in deployment assembly.
Coming back to the original problem - java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
As rightly said above, in JAX 2.x version, the ServletContainer class has been moved to the package - org.glassfish.jersey.servlet.ServletContainer. The related jar is jersey-container-servlet-core.jar which comes bundled within the jaxrs-ri-2.2.1.zip
JAX RS can be worked out without mvn by manually copying all jars contained within zip file jaxrs-ri-2.2.1.zip (i have used this version, would work with any 2.x version) to WEB-INF/lib folder. Copying libs to right folder makes them available at runtime.
This is required if you are using eclipse to build and deploy your project.
In pom.xml file we need to add
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.8</version>
</dependency>
The same error and wasted 2+ hours debugging and trying all options. I was not using the Maven/POM, so I could not leverage that solution given by few.
Finally the following resolved it: Adding the jars directly to the tomcat/lib (NOT WEB-INF\lib) folder and restarting the tomcat.
If anyone is trying to build a hello world application using Jersey, I think one of the easiest ways is to follow Jersey documentation.
https://jersey.github.io/download.html
If you are already using maven, it'd take only a few minutes to see the result.
I used below.
mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.26
It basically depends on which version jersey you are using. If you are using Jersey ver.1.X.X you need to add
Jersey 1 uses "com.sun.jersey", and Jersey 2 uses org.glassfish. on servlet class tag.
Also, note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.
And Add all the jar file into WEB-INF lib folder
In my case, it worked after adding the jersey-bundle jar in my tomcat lib.
I've read through a number of posts, but just can't seem to solve my problem. You'll also see tons of posts very similar to this one, even the same tutorial. Even following them, I can't seem to get to the answer.
Essentially, I'm trying to follow the simple tutorial at: http://www.vogella.com/articles/REST/
I've made a few changes to make it compatible with Jersey 2.x
I'm using:
Eclipse
Tomcat 6 (Deployed/Run as within Eclipse)
jaxrs-ri-2.0
I've enabled the JAX-RS Facet in Eclipse
Everything builds fine
Tomcat starts fine within Eclipse
I can get to a static page content via:
http://localhost:8080/RestTEST2/index.html
However, when I try to access my service via:
http://localhost:8080/RestTEST2/jaxrs/hello
I receive a 404 with "message not found" and "The requested resource (Not Found) is not available."
Here is my web.xml which is located at /WebContent/WEB-INF/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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestREST2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>TestREST</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
</web-app>
Here is my Java class:
package TestREST;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// Plain old Java Object it does not extend as class or implements
// an interface
// The class registers its methods for the HTTP GET request using the #GET annotation.
// Using the #Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML.
// The browser requests per default the HTML MIME type.
//Sets the path to base URL + /hello
#Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
I also have a JAX-RS User Library configured and referenced that includes all the JAX-RS jars.
Thoughts on what would cause the web service to not be found?
Jersey 2.0 does not recognize init-param with name com.sun.jersey.config.property.packages (web.xml). Try to change it to jersey.config.server.provider.packages as described in ServerProperties.PROVIDER_PACKAGES.
UPDATE (2020): try this link for current apidocs ServerProperties.PROVIDER_PACKAGES:
Thanks a lot.. i've been fighting for it as well.
This combination worked for me:
Tomcat 7.0.55
Eclipse Luna
Java 1.6
Jersey 1.7
web.xml:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.trgr.cobalt.cmdb.jersey.resource;com.trgr.cobalt.cmdb.jersey.beans;com.trgr.cobalt.cmdb.elasticity</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>
I would like to add one answer in this post.I was struggling with same problem for two days and couldn't find the solution.
I tried all the possible solutions provided here but later on I realized that server was giving error of
org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
This link answers gives the solution if somebody is stuck like me..
org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
Actually first answer given in this post suggest the solution which is for jersey 2.x bundle and if you are using jersey 1.x then it will keep on giving error.
Kindly refer to link given in answer for further reference
Simply by modifying the configuration of Apache Tomcat v7.0 while creating Dynamic Web Project, to include REST, I solved my problem. It is not enabled by default.
Recently I found myself stuck in this problem. I have got it resolved by following these two points as the solution:
By making the src/main/java as the source directory, and keeping the Project Structure as shown below:
Changing com.sun.jersey.config.property.packages to jersey.config.server.provider.packages in the web.xml
I'm trying to deploy a simple web application under JBoss AS 7.1 which comes bundled with resteasy. According to the documentation all that is needed is (at bare minimum) is an empty web.xml, a class with annotated #ApplicationPath("/mypath") and #Path("/other_stuff") for your other classes
The documentation I'm following is here:
https://docs.jboss.org/author/display/AS7/JAX-RS+Reference+Guide
https://docs.jboss.org/author/display/AS7/Java+API+for+RESTful+Web+Services+(JAX-RS)
Still, when I hit:
host:8080/warname/applicationpath/path
I receive a 404 error on the webpage but nothing in the logs.
Is there a configuration file I need to change in order for JAX-RS to work?
Thank you in advance for any help!
Empty web.xml will do.
Just add some resteasy dependency to your classpath. For instance, if you use maven you can add this to your pom.xml:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.1.GA</version>
<scope>provided</scope> <!-- provided if youre deploying to jboss as 7.1 -->
</dependency>
Then set up the application using only this class:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/rest")
public class JaxRsActivator extends Application {
}
Just to make sure, add a resource like this:
#Path("/hello")
public class HelloResource {
#GET
#Produces("text/plain")
public String helloResource() {
return "Hello! It's "+new Date();
}
}
And that's all you need. Deploy it at a JBoss AS 7.1 and get to it, say:
http://127.0.0.1:8080/mywarname/rest/hello
Edit:
I have created a java war maven project with the bare minimum strucutre:
-pom.xml
-src
|
--main
|
--java
|
--rest
|
--HelloResource.java
--JaxRsActivator.java
I called it simpleRest as seen below. All the archives are exactly as shown:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>simpleRest</groupId>
<artifactId>simpleRest</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.1.GA</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!-- So maven doesn't require web.xml -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
HelloResource.java
package rest;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/hello")
public class HelloResource {
#GET
#Produces("text/plain")
public String helloResource() {
return "Hello! It's "+new Date();
}
}
JaxRsActivator.java:
package rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/rest")
public class JaxRsActivator extends Application {
}
This generates a simpleRest.war (through mvn clean package). I then deploy it to a freshly installed JBoss AS 7.1.1.Final. As you can see, no reference is made to JAX-RS in the log during the deploy:
22:48:08,677 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "simpleRest.war"
22:48:09,318 INFO [org.jboss.web] (MSC service thread 1-4) JBAS018210: Registering web context: /simpleRest
22:48:09,492 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "simpleRest.war"
After that, the URL is available as expected:
http://127.0.0.1:8080/simpleRest/rest/hello
Notice that everything else gives a 404 error. But it is a different kind of 404.
http://127.0.0.1:8080/simpleRest/ gives:
HTTP Status 404 - /simpleRest/
That is a page not found error. On the other hand, http://127.0.0.1:8080/simpleRest/rest gives:
HTTP Status 404 - Could not find resource for relative : / of full path: http://127.0.0.1:8080/simpleRest/rest
That is a resource (REST service) not found error. This way you know JAX-RS is acting, though it did not have a handler for that path.
Take a look at jboss quickstarts: http://www.jboss.org/jdf/quickstarts/jboss-as-quickstart/
You can get them from http://www.jboss.org/jbossas/downloads
These are working out of box. For helloword-rs quickstart I can see web.xml with content:
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
and also comment:
One of the way of activating REST Servises is adding these lines, the server is responsible for adding the corresponding servlet automatically. If the src folder, org.jboss.as.quickstarts.rshelloworld.HelloWorld class has the Annotations to receive REST invocation.
If you are using JBoss AS 7.1, you also need to add the "resteasy.resources" context parameter. You also need to send the init-param to the HttpServletDispatcher servlet.
I found the solution at the following link : http://www.javaroots.com/2013/05/creating-rest-services-with-rest-easy.html
Just one typo error(maybe!!) in the above link. In the "RootRestService" change the method signature of the getClasses() method to public Set<Class<?>> getClasses().
Thats it! It solved my 3 day headache. Hope it helps you too!! :)
Even though acdcjunior's answer is great and very thorough, i'd like to reinfoce Andrzej's answer.
It works as a charm and it is by far the more straightforward one. The JBoss quickstart samples (also available on GitHub) are always a great resource to answer questions like this one.
I have a Dynamic Web Project with a set of jsp's and a custom controller servlet, that is working great. But, I also need to implement a rest easy exposed service on it, and that's why i changed the web. xml to contain rest easy servlet also. and the application class which invokes the rest easy resource. But, when i am adding changes to web.xml, it is hitting rest easy servlet, but throwing exception..
java.lang.ClassNotFoundException: org.scannotation.AnnotationDB$CrossReferenceException
Web.xml :
<servlet>
<display-name>ControllerServlet</display-name>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.example.ControllerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>CollRestApi</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.exampl.RestApiApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/ControllerServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CollRestApi</servlet-name>
<url-pattern>/or/*</url-pattern>
</servlet-mapping>
Rest Api Application :
public class RestApiApplication extends Application{
private Set<Object> singletons = new HashSet();
private Set<Class<?>> empty = new HashSet();
public RestApiApplication() {
// ADD YOUR RESTFUL RESOURCES HERE
this.singletons.add(new CollRestApi());
}
public Set<Class<?>> getClasses()
{
return this.empty;
}
public Set<Object> getSingletons()
{
return this.singletons;
}}
And CollRestApi.. is having exposed services..
#Path("api")#Consumes({ "application/xml", "application/json" })#Produces({application/xml", "application/json" })public class CollRestApi {
/**
* #return
*/
#GET
#Path("ab")
public Response do() {
System.out.println("Inside the do Method.");
}}
Obviously, you need to add scannotation.jar to your classpath. RESTEasy 2.2.2.GA depends on scannotation-1.0.3 and javassist-3.12.1.GA.jar which you also need.
I just faced the same problem using maven and netbeans. The answer is to add the jboss maven repository to your pom.xml-file. Just adding <dependency>....</dependency>, e.g. by using netbeans maven browser, doesn't install the dependecies of resteasy.
<repositories>
<repository>
<id>jboss</id>
<url>http://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
I had the same exception, and fixed it like this:
Is it possible you have the resteasy**.jar libraries in a different location (e.g. the tomcat lib dir) than other resteasy-supporting libs like scannotation-1.0.3.jar (e.g. in your war file WEB-INF/lib) ?
In such a configuration your resteasy classes will start in a different classloader which does not see the scannotation-1.0.3.jar classes.
=> Try putting all resteasy jar files in either one of the locations (preferably your .war file).