为什么在简单的JAX-RS servlet中使用404?

时间:2016-05-02 22:39:19

标签: java eclipse tomcat servlets jax-rs

我现在已经把头撞在墙上太久了。我只想尝试一个简单的JAX-RS servlet工作示例。我正在使用Eclipse(火星)。我可以将它构建并表面上部署在Tomcat上(Tomcat很高兴地开始),但是我得到了一个404,我希望得到响应。

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>radio16-webservice</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Testme</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
           <param-name>jersey.config.server.provider.packages</param-name>
           <param-value>com.testme</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestMe</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

Testme.java:

package com.testme;

//import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
//import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Path("testRoot")
public class Tester {
    @Context
    private UriInfo context;

    public Tester() {}

    @GET
    @Path("testme")
    @Produces("text/html")
    public String TestMe() {
        return "<html lang=\"en\"><body><h1>Hello, World!!</h1></body></html>";
    }
}

这是Tomcat输出:

May 02, 2016 3:22:52 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:radio16-webservice' did not find a matching property.
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.33
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Mar 18 2016 20:31:49 UTC
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.33.0
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Linux
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            3.19.0-30-generic
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /opt/jdk/jdk1.8.0_65/jre
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_65-b17
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /fast/git/testme/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /fast/apache-tomcat-8.0.33
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/fast/git/testme/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/fast/apache-tomcat-8.0.33
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/fast/git/testme/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/fast/apache-tomcat-8.0.33/endorsed
May 02, 2016 3:22:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
May 02, 2016 3:22:52 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
May 02, 2016 3:22:53 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
May 02, 2016 3:22:53 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
May 02, 2016 3:22:53 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
May 02, 2016 3:22:53 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
May 02, 2016 3:22:53 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1778 ms
May 02, 2016 3:22:53 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 02, 2016 3:22:53 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.33
May 02, 2016 3:22:55 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
May 02, 2016 3:22:56 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
May 02, 2016 3:22:56 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
May 02, 2016 3:22:56 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3358 ms

转到http://localhost/testRoot/testme/会产生404.如何修复?

2 个答案:

答案 0 :(得分:0)

Eclipse不会将您的Web应用程序部署为Tomcat服务器的“ROOT”,除非您采取一些步骤来实现它。试试http://localhost/radio16-webservice/testRoot/testme/

答案 1 :(得分:0)

我的hello world正在运行,然后我将我的项目转换为maven项目并得到了这个问题。就我而言,而不是

localhost:8080/testRoot/testme

它在

localhost:8080/testRoot-0.0.1-SNAPSHOT/api/hello

其中0.0.1-SNAPSHOT是pom版本。

我可以通过testRoottestRoot-0.0.1-SNAPSHOT链接访问静态文件。

我还不知道如何在不在上下文根目录中包含maven版本的情况下访问REST API :(。