【问题标题】:404 error in JAXRS service using jersey and tomcat使用球衣和 tomcat 的 JAXRS 服务中的 404 错误
【发布时间】:2015-01-07 11:58:36
【问题描述】:

使用 jersey 编写 JAXRS 服务并部署在 tomcat 上。应用子类

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

    public RestApplication() {
        // TODO Auto-generated constructor stub
        System.out.println("Inside RestApplication Constructor");
    }

    @Override
    public Set<Class<?>> getClasses() {
        // TODO Auto-generated method stub

        System.out.println("Get Class");
        Set<Class<?>> s=new HashSet<Class<?>>();
        s.add(SupportDataService.class);

        return s;
    }
}

资源类

@Path("/supportdata")
public class SupportDataService {


    public SupportDataService() {
        // TODO Auto-generated constructor stub
        System.out.println("Inside SupportDataService Constructor");
    }

    @Path("/support")
    @GET
    @Produces(MediaType.TEXT_XML)
    public String getSupportData(){

        String xmlSupport=null;

        xmlSupport="<SupportData><Support><key>path1</key><value>value1</value></Support><Support><key>path2</key><value>value2</value></Support></SupportData>";

        return xmlSupport;
    }
}

在 WEB-INF/lib 中添加了所有 jersey jar,除了 javax.servlet-api-3.0.1.jar 和点击 url

http://localhost:8080/RestConfigurator/rest/supportdata/support

但收到 404 错误。未将任何 web.xml 指定为子类应用程序。

【问题讨论】:

  • 把这些语句打印出来了吗?
  • 没有打印语句没有被执行。
  • 您可能需要逐步确切地告诉我们如何重现问题。开始一个新的简单项目并记录您采取的每一步。包括环境和部署方式。列出所有的罐子。你的代码看起来不错。 URL 似乎匹配,因为战争是 RestConfigurator
  • 添加了 jersey bundle 中的所有 jars。之前不包括 jersey bundle 的 ext 文件夹中打包的 jar javax.servlet-3.0.1.jar。如果包含此 jar 信息: validateJarFile(D:\BATCH\UpdatedWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\RestConfigurator\WEB-INF\lib\javax.servlet-api- 3.0.1.jar) - 未加载 jar。请参阅 Servlet 规范 2.3,第 9.7.2 节。违规类:javax/servlet/Servlet.class.
  • 添加了除 javax.servlet-3.0.1 之外的所有球衣包 jar。部署在 tomcat6 上,上面发布了 RestApplication 和 SupportDataService 类。不包括部署 web.xml。访问 url @987654321 时出现 404 错误@。除了 WEB-INF\lib 中的 jersey bundle jar 之外,tomcat 上的 jersey 还需要任何其他配置。

标签: rest tomcat jax-rs jersey-2.0


【解决方案1】:

因此,对于 Tomcat 6,它是一个 Servlet 2.5 实现,我们需要有一个 web.xml 声明 ServletContainer。你可以看到更多关于它here。基本上,如果你想保留你的 RestApplication 类,web.xml 看起来像

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>thepackge.of.RestApplication</param-value>
        </init-param>
    </servlet>

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

鉴于您已经添加了来自 Jersey JAX-RS 2.0 RI bundle 的所有 jar(除了 servlet-api),这应该可以工作。我已经在 Tomcat 6 上使用 Maven 进行了测试,它运行良好。 Maven 只是从该捆绑包中提取所有大多数 jar。但应用部署并没有什么不同。

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 2016-01-03
    • 2016-05-28
    • 1970-01-01
    相关资源
    最近更新 更多