简体   繁体   English

Restfull Web服务Maven项目netbeans

[英]Restfull Webservice Maven Project netbeans

I'm looking at every way to try to make a request type to get to my web service created by restfull Webservice into a project with Tomcat , Maven and some servlets , but nothing I do not start by mistake does not find the resource . 我正在寻找各种方法来尝试使请求类型进入由restfull Web服务创建的Web服务,使其进入具有Tomcat,Maven和某些servlet的项目中,但是我没有错误地开始并没有找到资源。 What am I doing wrong? 我究竟做错了什么? I may not have configured the web.xml ? 我可能没有配置web.xml吗? how can I do? 我能怎么做? I put the file pom.xml below and 我将文件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>

This is code of my web service: 这是我的网络服务的代码:

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

That is only a jar with a bunch of interfaces. 那只是一个带有一堆接口的罐子。 There is no implementation. 没有实现。 That dependency should only be used when you plan on deploying to a an EE compliant Server, like Glassfish or Wildfly. 仅当您计划部署到EE兼容服务器(例如Glassfish或Wildfly)时,才应使用该依赖项。 Tomcat is not an EE compliant server. Tomcat不是EE兼容服务器。 It is only a Servlet container. 它只是一个Servlet容器。 Therefore any features you use from that javaee-web-api , you need to also include an implementation . 因此,从该javaee-web-api使用的所有功能都需要包括一个实现

So for now, just get rid of it so you don't use ant classes for which there is no implementation. 因此,现在就删除它,这样您就不会使用没有实现的ant类。 Then you need to decide on a JAX-RS implementation to use. 然后,您需要确定要使用的JAX-RS实现。 For now I'll just say to use Jersey. 现在,我只说使用Jersey。 So just add this dependency 因此,只需添加此依赖项

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

Then you need to configure the application in the web.xml. 然后,您需要在web.xml中配置应用程序。 You can see here for more options . 您可以在这里看到更多选项 You basically want something like 你基本上想要这样的东西

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

The param-value in the init-param is the package you want Jersey to scan to pick up and register all your classes annotated with @Path and @Provider . init-paramparam-value是您希望Jersey扫描以获取并注册所有带有@Path@Provider注释的类的@Provider The scan is recursive, so you can list the root-most package in your project if you have your resource scattered in different packages. 扫描是递归的,因此如果资源分散在不同的程序包中,则可以列出项目中最根目录的程序包。

From here it should work. 从这里开始工作。 Then for JSON/POJO support, you can just add the following dependency 然后,对于JSON / POJO支持,您只需添加以下依赖项

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

No extra configuration is needed for that. 不需要额外的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM