gpt4 book ai didi

java - Jersey 2.x(非 Maven)在 Tomcat 服务器上运行时给出 404 错误

转载 作者:行者123 更新时间:2023-11-28 23:36:15 25 4
gpt4 key购买 nike

我一直在尝试为我的 RESTful 应用设置 Jersey,但进展并不顺利。我不喜欢 maven 方法,因为我不太理解 jersey 文档中提供的 initial-maven-structure。它包含很多额外的依赖项,这些依赖项会造成混淆和产生问题。

因此,我在 eclipse 中使用 tomcat 服务器 7 设置了一个新的简单 Web 项目,只是为了确定,仅用于初始测试,将所有 jar 转储到构建路径中。(尽管即使对于基本的 jersey 实现,它需要很多 jar ,这很奇怪)

问题:

1)完成上述所有步骤后,当我启动我的 tomcat 服务器时,它总是给我一个 404 错误。控制台没有向我显示错误,但它也没有显示 服务器处于 Activity 状态http://localhost:8080/

附言我正在使用最新的 2.9 版 Jersey

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

启动服务器时的控制台

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! :(
}

}

最佳答案

我真傻,我没有使用 war name在 URL 中..但是我会为任何经历过设置 Jersey 的人保留这段代码

问题的答案

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

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

致谢:this answer

关于java - Jersey 2.x(非 Maven)在 Tomcat 服务器上运行时给出 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com