2017-01-10 108 views
0

我正在學習使用jax RS(球衣)實現的Restful webservices。我使用jersery快速入門web應用程序創建了一個示例maven項目。 代碼:Baisc Restful Webservice項目404沒有找到

的index.jsp

<html> 
<body> 
<h2>Jersey RESTful Web Application!</h2> 
<p><a href="webapi/myresource">Jersey resource</a> 
<p>Visit <a href="http://jersey.java.net">Project Jersey website</a> 
for more information on Jersey! 

MyResource.java

package org.restful.sumanth.RestfulSumanth; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

/** 
* Root resource (exposed at "myresource" path) 
*/ 
@Path("myresource") 
public class MyResource { 

/** 
* Method handling HTTP GET requests. The returned object will be sent 
* to the client as "text/plain" media type. 
* 
* @return String that will be returned as a text/plain response. 
*/ 
@GET 
@Produces(MediaType.TEXT_PLAIN) 
public String getIt() { 
    return "Got it!"; 
} 
} 

WEB.XML

<?xml version="1.0" encoding="UTF-8"?> 
<!-- This web.xml file is not required when using Servlet 3.0 container, 
    see implementation details http://jersey.java.net/nonav/documentation /latest/jax-rs.html --> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<servlet> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>org.restful.sumanth.RestfulSumanth</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <url-pattern>/webapi/*</url-pattern> 
</servlet-mapping> 

當我運行該項目,我有輸出:

Jersey RESTful Web Application! 

[Jersey resource][1] 

Visit [Project Jersey website][2] for more information on Jersey! 


    [1]: http://webapi/myresource 
    [2]: https://jersey.java.net/ 

當我點擊澤西資源鏈接它給404,即使我有資源。有些人可以解釋爲什麼myresource在找到請求時無法找到?

+0

你能添加你的Maven依賴關係? – dsncode

+0

[Tomcat上的Jersey REST服務404錯誤]的可能重複(http://stackoverflow.com/questions/29315062/404-error-with-jersey-rest-service-on-tomcat) –

回答

0

我已得到另一計算器後回答:404 error with Jersey REST Service on Tomcat

我已經取代了下面的依賴這使得它的工作:

<dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-servlet</artifactId> 
</dependency> 

<dependency> 
<groupId>org.glassfish.jersey.containers</groupId> 
<artifactId>jersey-container-servlet</artifactId> 
<version>2.17</version> 
</dependency>