restful web-service - I get java.lang.IncompatibleClassChangeError - java

Hi I am new to web service I have googled for this many times. Could not get correct solution. This error occurs when I typed the path for my class. any body pls help
exception:
javax.servlet.ServletException: Servlet execution threw an exception
root cause:
java.lang.IncompatibleClassChangeError: Class javax.ws.rs.core.Response$Status does not implement the requested interface javax.ws.rs.core.Response$StatusType
com.sun.jersey.spi.container.ContainerResponse.getStatus(ContainerResponse.java:599)
com.sun.jersey.spi.container.ContainerResponse$CommittingOutputStream.commitWrite(ContainerResponse.java:157)
com.sun.jersey.spi.container.ContainerResponse$CommittingOutputStream.write(ContainerResponse.java:134)
sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
sun.nio.cs.StreamEncoder.flush(Unknown Source)
java.io.OutputStreamWriter.flush(Unknown Source)
java.io.BufferedWriter.flush(Unknown Source)
com.sun.jersey.core.util.ReaderWriter.writeToAsString(ReaderWriter.java:191)
com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.writeToAsString(AbstractMessageReaderWriterProvider.java:128)
com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:88)
com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:58)
com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:302)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.37 logs.
My Hello Program is this :
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> ";
}
}
and my web.xml file is this.
<?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>FirstRestWebService</display-name>
<display-name>FirstRestWebService</display-name>
<servlet>
<servlet-name>RestServlet</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>org.raj</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
pls anybody help.

I think your version of Jersey is too old. Please check your dependecies.
Jersey 2.0 supports JAX-RS 2.0.
Here is link for new Jersey.

Related

Allocate exception for servlet Jersey REST Service Error

I'm re-coding a webservice that I've already created so that I can create a "how-to" with git for the other members of my group who will be taking over the project.
This time around, I started to get an error when trying to use my webservice and I can't seem to find the problem because the code looks exactly like what I've previously coded (a code that worked).
I am using apache tomcat and Jersey with JSON.
Here is the error:
mai 14, 2015 6:08:02 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet Jersey REST Service
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99)
at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1359)
at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180)
at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799)
at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:339)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1213)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
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:103)
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:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Here is my web.xml file:
<?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>com.labtabdb.rest</display-name>
<welcome-file-list>
<welcome-file>readme.html</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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.labtabdb.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/nxp/*</url-pattern>
</servlet-mapping>
</web-app>
Here is a class that uses the #Path annotation:
//url path for class it's methods/chemin url pour la classe et ses methodes
#Path ("/v1/inventory")
public class V1__inventory
{
#GET //url ending in only /v1/inventory /chemin terminé par /v1/inventory
#Produces(MediaType.APPLICATION_JSON)
public Response errorIfNoQueryParamSpecified() throws Exception
{
return Response
.status(400) //bad request
.entity("Error: Please specify extension and parameter for this search.")
.build();
}
/**
* Method that returns materials filter by material name
* #param material name
* #return Response
* #throws Exception
*/
#Path("/{material_label}")
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response returnMaterial(#PathParam("material_label") String material) throws Exception
{
material = URLDecoder.decode(material, "UTF-8"); //decode URL param
String returnString = null;
JSONArray json;
try
{
json = LabTab_MySQLQuerys
.getMaterialsByName(material);
//if there are no results to the query
if(json.length() == 0)
throw new CustomException("No results returned");
else //otherwise return results
returnString = json.toString();
}
catch(CustomException e)
{
return Response
.status(204)
.entity(e.getMessage())
.build();
}
catch (Exception e)
{
e.printStackTrace();
return Response
.status(500) //internal server error
.entity("Server was not able to process your request")
.build();
}
return Response
.ok(returnString)
.build();
}
I've been searching and reading the same articles over and over for 3 days now and I have yet to find a solution that solves my error.
All help is appreciated
I'm going to go ahead and respond to my own question in hopes that it helps someone new to rest.
I made a pretty stupid mistake while creating my tutorial:
My "url" for my web service was, for example, com.webservice.rest. When re-doing my project, I decided to change the names of my packages to correspond with the new url: so my class that had a path annotation, let's say, #PATH ("/v1/inventory") was changed to com.webservice.inventory.
When typing the new package name, I rushed and forgot to include the word rest in com.webservice.rest.inventory .
When programming a webservice, you have to remember to make sure the the prefix of each package containing a path annotation is the url of the web service.
Make sure to include org.javassist:* for all scopes, since javassist package that Jersey uses it to do bytecode manipulation and the shaded jar did not include the javassist package In my case.
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.26.0-GA</version>
</dependency>

Jersey Servlet not found after switching to Spring

I have a simple RESTful Java web service...
#Service
#Path("/getAccountBalance")
public class GetAccountBalanceService {
#Autowired
private ILicenseService licenseService;
#GET
#Path("/{param}")
public Response provideService(#PathParam("param") String licenseUUID) {
License license = this.licenseService.getByUUID(licenseUUID);
String output = "Balance on the account : " + license.getBalanceValue();
return Response.status(200).entity(output).build();
}
At first I was getting an error where the bean licenseService was null even though I am autowiring it. So based on advice from another post I swtiched to use the Spring servlet for jersey, thus in my web-xml I changed from com.sun.jersey.spi.container.servlet.ServletContainer to...
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.freedomoss.crowdcontrol.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
But now when I start Tomcat I am getting the error..
Dec 19, 2014 12:13:13 AM org.apache.catalina.startup.ContextConfig applicationWebConfig
SEVERE: Parse error in application web.xml file at jndi:/localhost/mturk-web/WEB-INF/web.xml
java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name jersey-serlvet
Why is this happening? As you can see there is a servlet defined with that name in web-xml. If there was something wrong with loading that servlet why don't the logs tell that story?
Thanks.

simple RESTful web services in java(JAX-RS) using jersey , throws HTTP Status 404

When i am running my simple Java code for RESTful Web Services (JAX-RS), it throws 404 error. I have mapped it will in web.xml and have no compilation error. One suspecios message i am getting in my log is " INFO: No provider classes found. ", please suggest me to eliminate this problem.
Console Log -:
May 31, 2014 10:23:27 AM org.apache.catalina.startup.HostConfig checkResources
INFO: Reloading context [/rest]
May 31, 2014 10:23:27 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
stk5
May 31, 2014 10:23:27 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class stk5.ConversionService
May 31, 2014 10:23:27 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
May 31, 2014 10:23:27 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.18 11/22/2013 01:21 AM'
my ConversionService.java file -:
package stk5;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("ConversionService")
public class ConversionService{
#GET
#Path("/InchToFeet/{i}")
#Produces(MediaType.TEXT_XML)
public String convertInchToFeet(#PathParam("i") int i) {
int inch=i;
double feet = 0;
feet =(double) inch/12;
return "<InchToFeetService>"
+ "<Inch>" + inch + "</Inch>"
+ "<Feet>" + feet + "</Feet>"
+ "</InchToFeetService>";
}
#Path("/FeetToInch/{f}")
#GET
#Produces(MediaType.TEXT_XML)
public String convertFeetToInch(#PathParam("f") int f) {
int inch=0;
int feet = f;
inch = 12*feet;
return "<FeetToInchService>"
+ "<Feet>" + feet + "</Feet>"
+ "<Inch>" + inch + "</Inch>"
+ "</FeetToInchService>";
}
}
my Web.xml file -:
<?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>restApp</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>stk5</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>
Instead of mentioning the URL pattern in web.xml I would recommend you to put this in your Service #Path.
You should so consider your context root, if you have not set it then by default it is your Deployment (war/ear/jar etc) name.
So modify your <url-pattern>/rest/*</url-pattern> to <url-pattern>/*</url-pattern>. Now your URL should look something like this
http://localhost:8080/[context root]/ConversionService/InchToFeet/2
What URL are you going to in order to test it? The context is /rest and your path is /InchToFeet/{i}, so are you going to /rest/InchToFeet/<num>?
Edit: I also noticed you have a #Path("ConversionService") on the class. That would make your URL /restConversionService/InchToFeet/<num>. You might want to precede the ConversionService with a slash to make it /rest/ConversionService/InchToFeet/<num>.

Tomcat giving 500 error on page load

I'm new to tomcat and jersey so hopefully this isn't a stupid mistake. Whenever I try and access my project located at http://127.0.0.1:8080/first/rest/hello I get the following error...
javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:679)
root cause
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
com.sun.jersey.server.impl.application.RootResourceUriRules.<init> (RootResourceUriRules.java:99)
com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationIm pl.java:1298)
com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:169)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:771)
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:679)
I've got a Java class and a web.xml file for this project. They are the following...
Hello.java
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 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> ";
}
}
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>first</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>first</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>
What am I missing?
EDIT: It's probably relevant that I've been using the following tutorial to get started. http://www.vogella.de/articles/REST/article.html#first
Best way how to start is look at tested working samples. So.. please see following:
Helloworld-webapp: https://maven.java.net/content/repositories/releases/com/sun/jersey/samples/helloworld-webapp/1.9.1/helloworld-webapp-1.9.1-gf-project.zip
And by the way, that error message means "Jersey is not able to find ANY #Path annotated class". My bet would be on this:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>first</param-value>
</init-param>
Is "first" really name of the package where "Hello" class resides?
In jersey 2.x docs use
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.rest.portal</param-value>
</init-param>

Jersey Tutorials from Basics

I am using Jersey 1.8 and the tutorials that I am referring are quiet old. Nothing works. I am referring to the tutorial given here
And get a class not found exception. as per the tutorial I made my Java Class as well as configured my web.xml. It shows me an exception and I am not getting a way to fix this. I would like to have a complete up to date tutorial for Jersey implementation. And if something is better than Jersey for REST implementation please suggest. I have rescently started with REST based web services and would appreciate if you can suggest me where to start from(I am only interested in REST). Below is the code that I wrote and compiled using eclipse.
Hello.java
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 prints the Plain Text
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello()
{
return "Hello Jersey";
}
//This is the XML request output
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello()
{
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
//This result is produced if HTML is requested
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHTMLHello()
{
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
web.xml is as follows
<?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>RESTFullApp</display-name>
<servlet>
<servlet-name>JerseyRESTService</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>RESTFullApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyRESTService</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- <welcome-file-list>-->
<!-- <welcome-file>index.html</welcome-file>-->
<!-- <welcome-file>index.htm</welcome-file>-->
<!-- <welcome-file>index.jsp</welcome-file>-->
<!-- <welcome-file>default.html</welcome-file>-->
<!-- <welcome-file>default.htm</welcome-file>-->
<!-- <welcome-file>default.jsp</welcome-file>-->
<!-- </welcome-file-list>-->
</web-app>
Noe the error I get while I try to run is
exception
javax.servlet.ServletException: Servlet.init() for servlet JerseyRESTService threw exception
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Unknown Source)
root cause
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99)
com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1298)
com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:169)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:771)
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
javax.servlet.GenericServlet.init(GenericServlet.java:212)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Unknown Source)
You're not exactly following the tutorial. You changed bits here and there without actually understanding what they represents. In this particular case, according to the exception,
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
the following init param is wrong
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>RESTFullApp</param-value>
</init-param>
It should refer to the package containing the services. Put your service in a package and specify that in the init param value. Jersey or not, using the default package is always a bad practice.
As to the tutorials, why don't you just read Jersey's own documentation? Jersey Wiki and Jersey User Guide.
Try the Jersey User Guide. It will pretty much always have the most up-to-date tutorial. As for the error, your web.xml declares that your resources classes will be in a package called "RESTFullApp". 1) That's a peculiar package name, and 2) I don't see a package declaration on your class.
Edit: For a working example, you check out my sample project on github. If you have git and maven, you can clone it and run it with
git clone git://github.com/zzantozz/testbed.git tmp
cd tmp
mvn jetty:run -pl basic-jersey
Then visit http://localhost:8080/basic-jersey/rest/test

Categories