【问题标题】:Jersey application deployed on Tomcat returns 404部署在 Tomcat 上的 Jersey 应用程序返回 404
【发布时间】:2017-04-20 12:44:10
【问题描述】:

我尝试编写一个 REST 服务并使用 Tomcat 启动它。我的web.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0">

    <display-name>TomcatRestExample</display-name>

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
              <param-name>jersey.config.server.provider.packages</param-name>
              <param-value>de.continentale.testsvn.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/maven/*</url-pattern>
    </servlet-mapping>

</web-app>

我的班级de.continentale.testsvn.rest.RestCaller 看起来像:

package de.continentale.testsvn.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/service")
public class RestCaller {

    @GET
    @Produces(MediaType.TEXT_XML)
    public String convert() {
        return "<text>hallo</text>";
    }
}

我尝试访问http://localhost:8090/TomcatRestExample/maven/service,但得到:

HTTP Status 404 - /TomcatRestExample/maven/service

type Status report

message /TomcatRestExample/maven/service

description The requested resource is not available.

Apache Tomcat/7.0.41

Eclipse 中的 Tomcat 控制台输出:

INFORMATION: Reloading Context with name [/tomcat-rest-test] has started
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.servlet.WebAppResourceConfig init
INFORMATION: Scanning for root resource and provider classes in the Web app resource paths:
  /WEB-INF/lib
  /WEB-INF/classes
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFORMATION: Root resource classes found:
  class de.continentale.testsvn.rest.RestCaller
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFORMATION: No provider classes found.
Apr 20, 2017 2:23:19 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 05:39 AM'
Apr 20, 2017 2:23:20 PM org.apache.catalina.core.StandardContext reload
INFORMATION: Reloading Context with name [/tomcat-rest-test] is completed

知道我错过了什么吗?

【问题讨论】:

  • localhost:8090/TomcatRestExample 工作了吗?
  • 控制台上还有其他错误吗?您的依赖项是什么样的?
  • @aUserHimself localhost:8090/TomcatRestExample 也以相同的响应回答。我是否需要一个 html 页面来完成这项工作?
  • @CássioMazzochiMolin 我添加了 Tomcat 输出。依赖项是 asm、jersey-bundle、json、jersey-server、jersey-core。使用 Maven 编译可以正常工作。
  • 您的上下文似乎是/tomcat-rest-test 而不是/TomcatRestExample。尝试第一个:localhost:8090/tomcat-rest-test/maven/service.

标签: java rest tomcat jersey


【解决方案1】:

您的上下文似乎不正确。您的应用程序部署在/tomcat-rest-test 而不是/TomcatRestExample。后者只是管理工具使用的显示名称。 Tomcat 使用 WAR 文件名(不带 .war)作为上下文名称。

所以使用http://localhost:8090/tomcat-rest-test/maven/service 来访问你的资源方法。

【讨论】:

    【解决方案2】:

    您的上下文似乎是/tomcat-rest-test 而不是/TomcatRestExample。尝试第一个:localhost:8090/tomcat-rest-test/maven/service

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2015-10-18
      • 2015-08-16
      • 2016-11-15
      • 2016-02-20
      相关资源
      最近更新 更多