2015-11-13 113 views
0

我正在尋找各種方法來嘗試使請求類型到達我的web服務創建的restfull Webservice到Tomcat,Maven和一些servlet的項目,但沒有我不以一開始錯誤沒有找到資源。我究竟做錯了什麼?我可能沒有配置web.xml?我能怎麼做? 我把文件pom.xml的下方,Restfull Webservice Maven項目netbeans

<dependencies> 
    <dependency> 
     <groupId>org.glassfish.metro</groupId> 
     <artifactId>webservices-rt</artifactId> 
     <version>2.3</version> 
    </dependency> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-web-api</artifactId> 
     <version>7.0</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.jsoup</groupId> 
     <artifactId>jsoup</artifactId> 
     <version>1.8.3</version> 
    </dependency> 
    <dependency> 
     <groupId>com.google.code.gson</groupId> 
     <artifactId>gson</artifactId> 
     <version>2.4</version> 
    </dependency> 
    <dependency> 
     <groupId>org.json</groupId> 
     <artifactId>json</artifactId> 
     <version>20150729</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.mail</groupId> 
     <artifactId>javax.mail-api</artifactId> 
     <version>1.5.4</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-lang3</artifactId> 
     <version>3.4</version> 
    </dependency> 
    <dependency> 
     <groupId>postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.1-901-1.jdbc4</version> 
    </dependency> 
</dependencies> 

這是我的web服務的代碼:

@Path("ws") 
public class WsUserJson { 

    @Context 
    private UriInfo context; 

    /** 
    * Creates a new instance of WsUserJson 
    */ 
    public WsUserJson() { 
    } 

    /** 
    * Retrieves representation of an instance of com.lillonet.testmavenservletws.WsUserJson 
    * @return an instance of java.lang.String 
    */ 
    @GET 
    @Produces("application/json") 
    public String getJson(@QueryParam("name") String nome) { 

     //TODO return proper representation object 
     return "{"+nome+"}"; 
    } 

    /** 
    * PUT method for updating or creating an instance of WsUserJson 
    * @param content representation for the resource 
    * @return an HTTP response with content of the updated or created resource. 
    */ 
    @PUT 
    @Consumes("application/json") 
    public void putJson(String content) { 
    } 
} 

回答

1
<dependency> 
    <groupId>javax</groupId> 
    <artifactId>javaee-web-api</artifactId> 
    <version>7.0</version> 
    <scope>provided</scope> 
</dependency> 

也就是說只有一堆界面的罐子。沒有實現。只有在您計劃部署到符合EE的服務器(如Glassfish或Wildfly)時才應使用該依賴關係。 Tomcat不是符合EE標準的服務器。它只是一個Servlet容器。因此,您從javaee-web-api使用的任何功能,您還需要包含一個實施

所以現在,只是擺脫它,所以你不使用沒有實現的螞蟻類。然後你需要決定使用一個JAX-RS實現。現在我只會說使用澤西島。因此,只需添加此依賴關係

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

然後,您需要在web.xml中配置應用程序。你可以看到here for more options。你基本上要像

<web-app> 
    <servlet> 
     <servlet-name>MyApplication</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.foo.myresources</param-value> 
     </init-param> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>MyApplication</servlet-name> 
     <url-pattern>/api/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

init-paramparam-value是你想要新澤西掃描拿起並註冊@Path@Provider註釋的所有類的包。掃描是遞歸的,所以如果你的資源分散在不同的包中,你可以列出項目中最根本的包。

從這裏它應該工作。然後,對於JSON/POJO支持,您可以添加以下依賴項:

<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-json-jackson</artifactId> 
    <version>2.22.1</version> 
</dependency> 

不需要額外的配置。