我正在使用Jersey 1.8,我所指的教程很安靜。什麼都沒有我指的是給出的教程here澤西基礎教程
並得到一個類未找到異常。根據教程我做了我的Java類以及配置我的web.xml。它向我展示了一個例外,我沒有辦法解決這個問題。我希望爲澤西島實施提供完整的最新教程。如果有什麼比Jersey更好的REST實施請建議。我已經重新開始使用基於REST的Web服務,並希望如果你能建議我從哪裏開始(我只對REST感興趣)。以下是我使用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如下
<?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>
野老,我得到當我嘗試運行錯誤是
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)
好吧...我有點兒得到你。我在這裏學到的一件事是,使用默認包是非常糟糕的......哎呀!! ..和大多數人都建議我查看澤西島用戶手冊......我會做同樣的事情,然後回來。 –
@BalusC我怎麼能聯繫你?我有一個主張:) – codecompleting