【问题标题】:404 not found with jersey 1.8 and tomcat 7 Resful and eclipsejersey 1.8和tomcat 7 Resful和eclipse找不到404
【发布时间】:2014-02-15 08:12:55
【问题描述】:

我是 J2EE 的新手,我想创建一个安静的 web 服务,所以我按照教程进行操作,并在此基础上在 eclipse(Kepler 服务版本 1)中创建了一个简单的动态 web 应用程序项目,然后我添加了 jersey 1.8 库我已经用eclipse配置了appache tomcat(apache-tomcat-7.0.50),然后在添加index.html之后我可以看到http://localhost:8080/myproject/页面内容,

但我得到错误 404 (nt found) 这个 url

http://localhost:8080/myproject/api/v1/status

http://localhost:8080/myproject/api/v1/status/version

这是java资源文件

package myproject;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/v1/status/*")
public class v1_status {

private static final String api_version = "00.01.00";

@GET
@Produces(MediaType.TEXT_HTML)
public String returnName(){
    return "<p>Web Services</p>";
}

@Path("/version")
@GET
@Produces(MediaType.TEXT_HTML)
public String returnVersion()
{
    return "<p>Web Services</p>" + api_version; 
}

  }

这是 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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>myproject</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>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <!-- com.sun.jersey.config.property.packages 
        jersey.config.server.provider.packages  -->
        <param-name>com.sun.jersey.config.property.packages</param-name>            
        <param-value>myproject</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

   </web-app>

我已经测试了球衣版本 2,但是当我添加 2 版本时,tomcat 在开始阶段出现错误... 这里有什么问题?我在 stackoverflow 中看到了关于这个问题的其他线程,但我不知道应该如何解决这个问题,因为项目中的一切看起来都很好,但最终结果却不是。

这是tomcat启动日志


Feb 15, 2014 5:04:50 PM org.apache.catalina.core.AprLifecycleListener init
INFO: 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 (x86)\Java\jre6\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;E:\oracle\product\10.2.0\client_2\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;E:\Program Files\MATLAB\R2013a\runtime\win64;E:\Program Files\MATLAB\R2013a\bin
Feb 15, 2014 5:04:50 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:myproject' did not find a matching property.
Feb 15, 2014 5:04:51 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 15, 2014 5:04:51 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Feb 15, 2014 5:04:51 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 720 ms
Feb 15, 2014 5:04:51 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Feb 15, 2014 5:04:51 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.50
Feb 15, 2014 5:04:51 PM org.apache.tomcat.websocket.server.WsSci onStartup
INFO: JSR 356 WebSocket (Java WebSocket 1.0) support is not available when running on Java 6. To suppress this message, run Tomcat on Java 7, remove the WebSocket JARs from $CATALINA_HOME/lib or add the WebSocketJARs to the tomcat.util.scan.DefaultJarScanner.jarsToSkip property in $CATALINA_BASE/conf/catalina.properties. Note that the deprecated Tomcat 7 WebSocket API will be available. 
Feb 15, 2014 5:04:52 PM com.sun.jersey.api.core.servlet.WebAppResourceConfig init
INFO: Scanning for root resource and provider classes in the Web app resource paths:
  /WEB-INF/lib
  /WEB-INF/classes
Feb 15, 2014 5:04:52 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class myproject.v1_status
Feb 15, 2014 5:04:52 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Provider classes found:
  class org.codehaus.jackson.jaxrs.JsonParseExceptionMapper
  class org.codehaus.jackson.jaxrs.JacksonJsonProvider
  class org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
  class org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper
Feb 15, 2014 5:04:53 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.18 11/22/2013 01:21 AM'
Feb 15, 2014 5:04:53 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Feb 15, 2014 5:04:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Feb 15, 2014 5:04:54 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2836 ms

【问题讨论】:

  • 泽西映射到/api/*。您的资源路径是/status/*。为什么你认为它会映射到/api/v1/status 而不是/api/status
  • @JBNizet 我这样做是为了测试,资源映射实际上是到“/v1/status/”,结果与旧 (404) 相同
  • @IndoKnight 404 错误

标签: rest jakarta-ee tomcat jersey


【解决方案1】:

已解决,@Path("/v1/status/*") 中的星号 (*) 是问题的原因 删除后,我得到了正确的结果,所以它应该是@Path("/v1/status/") ...

【讨论】:

    猜你喜欢
    • 2016-11-21
    • 2013-07-16
    • 1970-01-01
    • 2018-02-04
    • 2021-09-12
    • 2014-05-12
    • 2019-03-12
    • 1970-01-01
    • 2014-01-14
    相关资源
    最近更新 更多