泽西岛的Web服务仅返回404

时间:2017-05-09 21:01:50

标签: java rest tomcat jersey http-status-code-404

我在这里已经阅读了一些关于这个问题的问题,并坚信在这里并非如此。所以我有一个这样的课:

package Rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {

        String output = "Jersey say : " + msg;

        return Response.status(200).entity(output).build();

    }

}

我的web.xml

<web-app id="WebApp_ID" version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Restful Web Application</display-name>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>Rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

我的堆栈跟踪,没有显示异常:

Mai 09, 2017 10:48:25 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNUNG: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:RestTest' did not find a matching property.
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server version:        Apache Tomcat/9.0.0.M20
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server built:          Apr 12 2017 19:27:10 UTC
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server number:         9.0.0.0
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Name:               Windows 10
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Version:            10.0
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Architecture:          amd64
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Java Home:             C:\Program Files\Java\jre1.8.0_121
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Version:           1.8.0_121-b13
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Vendor:            Oracle Corporation
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_BASE:         C:\Users\phamt\OneDrive\MyLittleProject\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mai 09, 2017 10:48:25 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 9.0
Mai 09, 2017 10:48:26 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.base=C:\Users\phamt\OneDrive\MyLittleProject\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mai 09, 2017 10:48:26 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0
Mai 09, 2017 10:48:26 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dwtp.deploy=C:\Users\phamt\OneDrive\MyLittleProject\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Mai 09, 2017 10:48:26 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 9.0\endorsed
Mai 09, 2017 10:48:26 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dfile.encoding=UTF-8
Mai 09, 2017 10:48:26 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMATION: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_121\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.8.0_121/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_121/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_121/bin/../jre/lib/amd64;C:\Program Files\Java\jdk1.8.0_121\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Git\cmd;C:\Program Files (x86)\sbt\bin;C:\Users\phamt\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\Java\jre1.8.0_121\bin;;C:\Users\phamt\eclipse\java-neon\eclipse;;.]
Mai 09, 2017 10:48:26 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["http-nio-8080"]
Mai 09, 2017 10:48:27 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMATION: Using a shared selector for servlet write/read
Mai 09, 2017 10:48:27 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["ajp-nio-8009"]
Mai 09, 2017 10:48:27 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMATION: Using a shared selector for servlet write/read
Mai 09, 2017 10:48:27 PM org.apache.catalina.startup.Catalina load
INFORMATION: Initialization processed in 2620 ms
Mai 09, 2017 10:48:27 PM org.apache.catalina.core.StandardService startInternal
INFORMATION: Starting service [Catalina]
Mai 09, 2017 10:48:27 PM org.apache.catalina.core.StandardEngine startInternal
INFORMATION: Starting Servlet Engine: Apache Tomcat/9.0.0.M20
Mai 09, 2017 10:48:30 PM org.apache.jasper.servlet.TldScanner scanJars
INFORMATION: 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.
Mai 09, 2017 10:48:34 PM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["http-nio-8080"]
Mai 09, 2017 10:48:34 PM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["ajp-nio-8009"]
Mai 09, 2017 10:48:34 PM org.apache.catalina.startup.Catalina start
INFORMATION: Server startup in 7036 ms

我使用Jersey 2.xx和Tomcat v9.0。

我尝试了一些网址,例如:

http://localhost:8080/RestTest/rest/hello/Tuan

http://localhost:8080/RestTest/rest/hello/

http://localhost:8080/RestTest/Rest/hello/Tuan

始终显示404未找到。

知道为什么吗?

1 个答案:

答案 0 :(得分:2)

正如@Libin所说:

  

@Path(“/ {param}”)应该是@Path(“{param}”)

解决!