簡體   English   中英

Restfull Web服務Maven項目netbeans

[英]Restfull Webservice Maven Project netbeans

我正在尋找各種方法來嘗試使請求類型進入由restfull Web服務創建的Web服務,使其進入具有Tomcat,Maven和某些servlet的項目中,但是我沒有錯誤地開始並沒有找到資源。 我究竟做錯了什么? 我可能沒有配置web.xml嗎? 我能怎么做? 我將文件pom.xml放在下面,

<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>

這是我的網絡服務的代碼:

@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) {
    }
}
<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使用的所有功能都需要包括一個實現

因此,現在就刪除它,這樣您就不會使用沒有實現的ant類。 然后,您需要確定要使用的JAX-RS實現。 現在,我只說使用Jersey。 因此,只需添加此依賴項

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

然后,您需要在web.xml中配置應用程序。 您可以在這里看到更多選項 你基本上想要這樣的東西

<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是您希望Jersey掃描以獲取並注冊所有帶有@Path@Provider注釋的類的@Provider 掃描是遞歸的,因此如果資源分散在不同的程序包中,則可以列出項目中最根目錄的程序包。

從這里開始工作。 然后,對於JSON / POJO支持,您只需添加以下依賴項

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

不需要額外的配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM