如何在没有web.xml的情况下实现jaxrs Application [英] How to implement jaxrs Application without web.xml
问题描述
我正在尝试部署一个非常简单的jaxrs应用程序而没有web.xml配置,并且无法使其正常工作。我希望访问的URL是serverandport / {appname} / rest / welcome / hello,我想我必须遗漏一些显而易见的东西。
I am trying to deploy a really simple jaxrs application with without a web.xml config and cannot get it working. My URL I'd expect to access is serverandport/{appname}/rest/welcomes/hello and I think I must be missing something dead obvious.
应用程序
@ApplicationPath("/rest")
public class EngineApp extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(RestTestImpl.class);
return s;
}
}
资源
@Path("/welcomes")
public class RestTestImpl {
@GET
@Path("hello")
public String sayPlainHello() {
return "Hi from Rest";
}
}
POM片段
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
编辑:在下面的响应中,我尝试使用空的web.xml并使用以下内容web.xml中。打扰也会返回404,但是下面的xml表示Servlet javax.ws.rs.core.Application不可用:
Further to the response below, I tried with an empty web.xml and also with the following web.xml. Bother also return 404, however the xml below states "Servlet javax.ws.rs.core.Application is not available":
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
推荐答案
您需要将应用程序部署到符合Servlet 3.0的版本中容器以利用此功能。尝试 GlassFish 3.x 或 Tomcat 7 。
You need to deploy your application into a Servlet 3.0 compliant container to take advantage of this functionality. Try GlassFish 3.x or Tomcat 7.
这篇关于如何在没有web.xml的情况下实现jaxrs Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!