java 如何在没有 web.xml 的情况下实现 jaxrs 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13375831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:30:20  来源:igfitidea点击:

How to implement jaxrs Application without web.xml

javarestjerseyjax-rs

提问by edwardmlyte

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.

我试图在没有 web.xml 配置的情况下部署一个非常简单的 jaxrs 应用程序,但无法使其工作。我希望访问的 URL 是 serverandport/{appname}/rest/welcomes/hello,我想我一定遗漏了一些显而易见的东西。

Application

应用

@ApplicationPath("/rest")
public class EngineApp extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(RestTestImpl.class);
        return s;
    }

}

Resource

资源

@Path("/welcomes")
public class RestTestImpl {
    @GET
    @Path("hello")
    public String sayPlainHello() {
        return "Hi from Rest";
    }
}

POM snippet

POM 片段

<groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>

Edit: 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 以及以下 web.xml。麻烦也返回 404,但是下面的 xml 声明“Servlet javax.ws.rs.core.Application 不可用”:

web.xml

网页.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>

采纳答案by Michal Gajdos

You need to deploy your application into a Servlet 3.0 compliant container to take advantage of this functionality. Try GlassFish 3.xor Tomcat 7.

您需要将应用程序部署到 Servlet 3.0 兼容容器中才能利用此功能。尝试GlassFish 3.xTomcat 7

回答by jabal

I have found a quite similar question and the problem found there could help you as well. I would definitely try it with an empty web.xml with 3.0 schema version to see if it helps.

我发现了一个非常相似的问题,那里发现的问题也可以帮助你。我肯定会尝试使用带有 3.0 架构版本的空 web.xml 来查看它是否有帮助。

JBoss AS 7 Restful Webservices not auto deploying

JBoss AS 7 Restful Webservices 不自动部署

回答by edwardmlyte

For those wishing to use the Servlet 2.0 specification, create a web.xml and add in the jersey servlet maven dependency:

对于那些希望使用 Servlet 2.0 规范的人,创建一个 web.xml 并添加 jersey servlet maven 依赖项:

<?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_3_0.xsd">
    <servlet>
        <servlet-name>REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.mycompany.rest.engine.EngineApp</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Add dependency to pom

给pom添加依赖

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.13</version>
</dependency>