在Tomcat服务器上运行时,Jersey 2.x(非Maven)会出现404错误

时间:2014-06-15 19:31:10

标签: java rest tomcat jersey jersey-2.0

我一直在尝试为我的RESTful App设置Jersey,但它并不顺利。我不喜欢maven方法,因为我不太了解jersey文档中提供的初始maven结构。它包含许多混淆并产生问题的其他依赖项。

所以我使用tomcat服务器7在eclipse中设置了一个新的简单web项目,并且只是为了确保,仅对于初始测试,将所有jar放入构建路径中。(尽管即使是基本的jersey实现,它也是如此)需要很多罐子,这很奇怪)

问题:

1) 完成上述所有步骤后,当我启动我的tomcat服务器时,它总是给我一个404错误..控制台没有显示错误,但它也没有显示the server is live at http://localhost:8080/

P.S。我正在使用最新的2.9版泽西岛

我的所有代码&控制台输出如下:

启动服务器时的控制台

Jun 16, 2014 12:36:00 AM 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: /Users/Sahil/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Jun 16, 2014 12:36:01 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
Jun 16, 2014 12:36:01 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:REST test' did not find a matching property.
Jun 16, 2014 12:36:02 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 16, 2014 12:36:02 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jun 16, 2014 12:36:02 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2339 ms
Jun 16, 2014 12:36:02 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 16, 2014 12:36:02 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.52
Jun 16, 2014 12:36:06 AM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.9 2014-05-22 05:12:10...
Jun 16, 2014 12:36:06 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jun 16, 2014 12:36:06 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jun 16, 2014 12:36:06 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4117 ms

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<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>REST test</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 Web Application</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.sahilgandhi</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

,只是另一个测试类

package com.sahilgandhi.rest;

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

import com.sahilgandhi.data.User;

/**
 * Root resource (exposed at "testapi" path)
 */
@Path("testapi")
public class TestApi {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "works like a dream!!"; // I HOPE IT DID! :(
    }

}

1 个答案:

答案 0 :(得分:0)

我有多愚蠢,我没有在网址中使用war name ..但是我会为那些经历设置球衣的人们保留这些代码

回答问题

http://localhost:8080/<your-war-name>/<your-servlet-URI>/<whatever>

所以对我来说是 - &gt; http://localhost:8080/REST_Test/rest/testapi

致谢:this answer

相关问题