I'm trying to run a sample basic REST API on Eclipse but have been getting ServletException.
Here is the code:
Employee.java
package net.javabeat.articles.spring.rest.simple; public class Employee { private String id; private String name; private String department; public Employee(){} public Employee(String id, String name, String department){ this.id = id; this.name = name; this.department = department; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String toString(){ StringBuilder result = new StringBuilder(); result.append("[employee]"); result.append("[id]" + id + "[id]"); result.append("[name]" + name + "[name]"); result.append("[department]" + department + "[department]"); result.append("[employee]"); return result.toString(); } public String toXmlString(){ StringBuilder result = new StringBuilder(); result.append("<employee>"); result.append("<id>" + id + "</id>"); result.append("<name>" + name + "</name>"); result.append("<department>" + department + "</department>"); result.append("</employee>"); return result.toString(); } } EmployeeManager.java
package net.javabeat.articles.spring.rest.simple; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("employee-manager") public class EmployeeManager { private static Set<Employee> employees; @GET @Produces(MediaType.TEXT_XML) @Path("employeelist") public String getEmployeesInXmlFormat(){ StringBuilder xmlResult = new StringBuilder(); xmlResult.append("<employees>"); Iterator<Employee> iterator = employees.iterator(); while (iterator.hasNext()){ Employee employee = iterator.next(); String employeeAsXml = employee.toXmlString(); xmlResult.append(employeeAsXml); } xmlResult.append("</employees>"); return xmlResult.toString(); } @GET @Produces(MediaType.TEXT_PLAIN) @Path("employeelist") public String getEmployeesInPlainFormat(){ StringBuilder plainResult = new StringBuilder(); plainResult.append("[employees]"); Iterator<Employee> iterator = employees.iterator(); while (iterator.hasNext()){ Employee employee = iterator.next(); String employeeAsXml = employee.toXmlString(); plainResult.append(employeeAsXml); } plainResult.append("[employees]"); return plainResult.toString(); } static{ employees = new HashSet<Employee>(); employees.add(new Employee("1", "David", "IT")); employees.add(new Employee("2", "John", "SALES")); } } EMClient.java
package net.javabeat.articles.spring.rest.simple; import java.net.URI; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; public class EMClient { public static void main(String[] args) throws Exception{ ClientConfig clientConfig = new DefaultClientConfig(); Client clientObject = Client.create(clientConfig); String uriPath = "http://localhost:8080/REST-Simple/rest-simple"; URI uriObject = UriBuilder.fromUri(uriPath).build(); WebResource employeeResource = clientObject.resource(uriObject); xmlFormatTest(employeeResource); plainFormatTest(employeeResource); } private static void xmlFormatTest(WebResource employeeResource){ WebResource innerResource = employeeResource.path("employee-manager").path("employeelist"); WebResource.Builder builderObject = innerResource.accept(MediaType.TEXT_XML); String employeesAsXml = builderObject.get(String.class); System.out.println(employeesAsXml); } private static void plainFormatTest(WebResource employeeResource){ WebResource innerResource = employeeResource.path("employee-manager").path("employeelist"); WebResource.Builder builderObject = innerResource.accept(MediaType.TEXT_PLAIN); String employeesAsPlainText = builderObject.get(String.class); System.out.println(employeesAsPlainText); } } The deployment descriptor 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>REST-EmployeeManager</display-name> <servlet> <servlet-name>REST-Simple</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>net.javabeat.articles.spring.rest.simple</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>REST-Simple</servlet-name> <url-pattern>/rest-simple/*</url-pattern> </servlet-mapping> </web-app> On trying to access http://localhost:8080/Rest-EmployeeManager/rest-simple/employee-manager/employeelist the following is displayed:
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: Servlet.init() for servlet REST-Simple threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source) root cause java.lang.NoClassDefFoundError: Could not initialize class sun.proxy.$Proxy8 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) java.lang.reflect.Constructor.newInstance(Unknown Source) java.lang.reflect.Proxy.newInstance(Unknown Source) java.lang.reflect.Proxy.newProxyInstance(Unknown Source) com.sun.jersey.server.impl.application.WebApplicationImpl$26.run(WebApplicationImpl.java:1626) java.security.AccessController.doPrivileged(Native Method) com.sun.jersey.server.impl.application.WebApplicationImpl.createProxy(WebApplicationImpl.java:1623) com.sun.jersey.server.impl.application.WebApplicationImpl.<init>(WebApplicationImpl.java:335) com.sun.jersey.server.impl.container.WebApplicationProviderImpl.createWebApplication(WebApplicationProviderImpl.java:55) com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:66) com.sun.jersey.spi.container.servlet.ServletContainer.create(ServletContainer.java:394) com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.create(ServletContainer.java:309) com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:603) com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207) com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376) com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559) javax.servlet.GenericServlet.init(GenericServlet.java:160) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs. -------------------------------------------------------------------------------- Apache Tomcat/7.0.27 Any help on what I'm possibly doing wrong?