【问题标题】:Restfull Webservice Maven Project netbeansRestful Web 服务 Maven 项目 netbeans
【发布时间】:2015-11-13 20:58:27
【问题描述】:

我正在寻找各种方法来尝试将请求类型获取到由 restfull Webservice 创建的我的 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) {
    }
}

【问题讨论】:

    标签: java rest maven netbeans jax-rs


    【解决方案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 使用的任何功能,您还需要包含一个实现

    所以现在,只需摆脱它,这样您就不会使用没有实现的 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>
    

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

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

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

    不需要额外的配置。

    【讨论】:

      猜你喜欢
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多