2014-09-10 100 views
2

我正在關注http://www.vogella.com/tutorials/REST/article.html教程。web.xml中的servlet-class路徑

我已經創建了名爲「JerseyDemoProject」的項目(Dyanamic Web項目),通過名稱「Package1」創建了一個包,並且我在該包中編寫了程序Hello.java(在教程中給出)。

我已經修改了我的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_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>JerseyDemoProject</display-name> 
    <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> 

    <servlet> 
    <servlet-name>HelloServlet</servlet-name> 
    <servlet-class>Package1.Hello</servlet-class> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>Package1</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>HelloServlet</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 


</web-app> 

當我嘗試「在服務器上運行」,它給了我404-未找到錯誤。 我使用Tomcat服務器

Hello類:

package Package1; 


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> "; 
    } 

} 

Here is my project structure

UPDATE:

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_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>JerseyDemoProject</display-name> 
    <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> 

    <servlet> 
    <servlet-name>HelloServlet</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>Package1</param-value> 
     </init-param> 

</servlet> 
    <servlet-mapping> 
    <servlet-name>HelloServlet</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 


</web-app> 

類文件

@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 
    @Path("/inhtmlform") 
    @Produces(MediaType.TEXT_HTML) 
    public String sayHtmlHello() { 
    return "<html> " + "<title>" + "Hello Jersey" + "</title>" 
     + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> "; 
    } 

} 

URL中使用

http://localhost:8080/JerseyDemoProject/rest/hello/inhtmlform 

顯示錯誤

HTTP Status 500 - Error instantiating servlet class com.sun.jersey.spi.container.servlet.ServletContainer 

堆棧跟蹤:

Exception: 
javax.servlet.ServletException: Error instantiating servlet class com.sun.jersey.spi.container.servlet.ServletContainer 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) 
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) 
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314) 
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    java.lang.Thread.run(Unknown Source) 

Rootcause: 

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) 
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) 
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314) 
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    java.lang.Thread.run(Unknown Source) 

我發現TESE警告我的控制檯

INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre7\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files (x86)/Java/jre7/bin/client;C:/Program Files (x86)/Java/jre7/bin;C:/Program Files (x86)/Java/jre7/lib/i386;C:\product\11.2.0\client_1\bin;C:\Informatica\961_989\961_Install\java\jre\bin\server;C:\Informatica\961_989\961_Install\tools\odbcdrv;C:\Informatica\961_989\961_Install\tools\jdbcdrv;C:\Informatica\961_989\961_Install\tools\datadirect;C:\app\vpalagum\product\11.2.0\client_2\bin;C:\app\vpalagum\product\11.2.0\client_1;C:\Program Files (x86)\RSA SecurID Token Common;C:\Program Files\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Microsoft Shared\Microsoft Online Services;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Box\Box Edit\;C:\Program Files (x86)\Java\jdk7_u15\bin;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Perforce;C:\Softwares\eclipse4.2;;. 

WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JerseyDemoProject' did not find a matching property. 
+0

什麼網址你得到404?粘貼在服務器上運行時在瀏覽器上生成的URL – zerocool 2014-09-10 11:10:51

+0

我正在eclipse中運行它。單擊「Hello.java」 - >在服務器上運行 – user3873909 2014-09-10 11:13:04

+0

顯示的URL爲http:// localhost:8080/JerseyDemoProject/WEB- INF/classes/Package1/Hello.java – user3873909 2014-09-10 11:13:30

回答

0

我認爲問題是因爲使用我使用球衣2.1。我刪除它,從https://jersey.java.net/download.html安裝澤西島1.18 JARbundle。我在我的WEB-INF庫文件夾中添加了這個jar,並且在這個文件夾中添加了asm.jar。 (這個jar可以在Jersey1.18 zip包中從同一個站點下載)。然後跟@SparkOn的Answer.It工作!

3

由於指定的URL模式是

<url-pattern>/rest/*</url-pattern> 

而且你的servlet代碼應該是這樣的

<servlet> 
    <servlet-name>HelloServlet</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>Package1</param-value> 
     </init-param> 
</servlet> 

,你可以在你的資源方法指定其他路徑來調用它單獨像

@Path("/hello") 
    public class Hello { 

    @GET 
    @Path("/intextform") 
    @Produces(MediaType.TEXT_PLAIN) 
    public String helloWorld(){ 
     return "Hello from Jersey!"; 

     } 
    } 

然後右鍵單擊該項目並在服務器上運行

試試這個網址

http://localhost:8080/JerseyDemoProject/rest/hello/intextform 
+0

你做了我在答案中指定的任何內容,並添加了映射路徑 – SparkOn 2014-09-10 11:29:47

+0

完全限定包名是什麼意思? – user3873909 2014-09-10 11:31:28

+0

在服務器上運行它之後,在servlet的頂部指定的包的名稱訪問資源方法 – SparkOn 2014-09-10 11:33:20

0

首先請在web.xml <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-cl‌​ass>更換<servlet-class>Package1.Hello</servlet-class>並重新運行該應用程序

請嘗試以下URL它應該工作:

http://localhost:8080/JerseyDemoProject/Package1/rest/hello 

前提是你已將您的控制器映射到@PATH("/hello")

+0

不,它的工作 – user3873909 2014-09-10 11:37:26

1

問題不在Jersey 2.1中,問題在於您的servlet類。 您應該使用在你的web.xml以下servlet類(適用於所有州2.x應用程序)

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 

代替:

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 

那麼一切都將正常工作。

最後,你的servlet應該是:

<servlet> 
    <servlet-name>HelloServlet</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>Package1</param-value> 
    </init-param> 
</servlet> 
相關問題