I am trying to implement a jax-rs web service using jersey framework. I have written the web service but I don't fully understand what the web.xml tags mean so I don't know if I have configured it correct but when I try to access the service I get an error. Here is the web service:
package org.LMS.Controller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path ("/test")
public class Test {
private String name = "Worked";
@GET
@Produces (MediaType.APPLICATION_XHTML_XML)
public String getTest ()
{
return name;
}
}
my web.xml is:
<!-- Test web service mapping -->
<servlet>
<display-name>Test</display-name>
<servlet-name>Test</servlet-name>
<servlet-class>org.LMS.Controller</servlet-class>
<init-param>
<param-name>org.LMS.Controller.Test</param-name>
<param-value>eduscope</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<!--end Test web service mapping -->
and this is the error I'm getting when I try to access my application: HTTP Status 500 - type Exception report message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Wrapper cannot find servlet class org.LMS.Controller or a class it depends on
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:679)
root cause
java.lang.ClassNotFoundException: org.LMS.Controller
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:679)
Can you guys tell me what I'm doing wrong and explain what each tag in the web.xml file means has it relates to web services
You've set the wrong servlet. Assuming that you're using Jersey, You need to specify your servlet as follows:
<servlet>
<servlet-name>Rest</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.LMS.Controller.Test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
And when you want to access it, you use the following url
http://(host)[:port]/(context path)/rest/test
e.g.
http://localhost:8080/MyRestProject/rest/test
To configure jax-rs webservice using jersey, you can configure with most simple and with only 2 configurations on web.xml (using more steps at java code and annotations), follow steps:
1) Write a Application (Java code):
package your.package.example;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class ExampleApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
// Annotated @Path endpoint
s.add(ExampleWebServiceRestClass.class);
return s;
}
}
2) Add config reference (to Application code) on your web.xml:
<web-app>
<servlet>
<servlet-name>your.package.example.ExampleApplication
</servlet-name>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>your.package.example.ExampleApplication
</servlet-name>
<url-pattern>/wspath/*</url-pattern>
</servlet-mapping>
</web-app>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With