java - Restful Web 服务 Maven 项目 netbeans

标签 java rest maven netbeans jax-rs

我正在寻找各种方法来尝试创建一个请求类型来访问我的由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) {
    }
}

最佳答案

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

那只是一个带有一堆接口(interface)的 jar 。没有实现。仅当您计划部署到符合 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 中配置应用程序。您可以看到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>

不需要额外的配置。

关于java - Restful Web 服务 Maven 项目 netbeans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701648/

相关文章:

javascript - 使用 Reactjs 的 Axios Post 表单

javascript - 如何通过 REST 使用 Kinvey 执行 'Hello World'?

wcf - 如何为 WCF REST C# 站点设置 oAuth 身份验证

java - 第谷 - 组装 JAR 时出错 : A zip file cannot include itself -> [Help 1]

java - 将增益应用于 PCM 时避免过度调制/失真

java - JsonNode 将 JSON 转换为字符串列表

java - 在其他java程序的类中调用类

java - 就性能而言,在什么时候用 BufferedOutputStream 包装 FileOutputStream 才有意义?

java - Maven 3项目间依赖与war打包

java - 如何从java服务器端使用参数运行vb exe文件