[英]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-param
的param-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.