Restfull Webservice Maven项目netbeans

时间:2015-11-13 20:58:27

标签: java rest maven netbeans jax-rs

我正在寻找各种方法来尝试创建一个请求类型,以便将我的Web服务由restfull Webservice创建到一个包含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) {
    }
}

1 个答案:

答案 0 :(得分:1)

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

这只是一个带有大量接口的jar。没有实施。只有在计划部署到符合EE的服务器(如Glassfish或Wildfly)时,才应使用该依赖项。 Tomcat不是符合EE标准的服务器。它只是一个Servlet容器。因此,您在javaee-web-api中使用的任何功能都需要包含实现

所以现在,只是摆脱它,所以你不使用没有实现的ant类。然后,您需要决定要使用的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>

param-value中的init-param是您希望Jersey扫描以获取并注册所有使用@Path@Provider注释的类的包。扫描是递归的,因此如果您的资源分散在不同的包中,则可以列出项目中最根目录的包。

从这里它应该工作。然后,对于JSON / POJO支持,您只需添加以下依赖项

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

不需要额外的配置。