简体   繁体   English

服务器总是返回泽西2 + Tomcat 6的404错误

[英]Server always returns 404 Error with Jersey 2 + Tomcat 6

I'm trying to get a simple REST-Service running with Jersey 2 and Tomcat 6 using maven. 我正在尝试使用maven运行使用Jersey 2Tomcat 6运行的简单REST服务。 IntelliJ says my app was successfully deployed and I can access my index.jsp file. IntelliJ说我的应用程序已成功部署,我可以访问我的index.jsp文件。 When trying to navigate to my rest endpoint I always get a 404 error message: 尝试导航到我的休息端点时,我总是收到404错误消息:

http://localhost:8080/rest/hello HTTP://本地主机:8080 / REST /你好

http://localhost:8080/HelloWorld/rest/hello HTTP://本地主机:8080 /的HelloWorld / REST /你好

web.xml: web.xml中:

<servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</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.tutorial.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

pom.xml: pom.xml中:

<groupId>com.tutorial.example</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>


<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.10.1</version>
    </dependency>
</dependencies>

HelloWorld.java: HelloWorld.java:

package com.tutorial;

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

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

    /**
     * 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 "Got it!";
    }
}

在此输入图像描述

Is something wrong with my URL? 我的网址有问题吗? There are no errors in the tomcat log. tomcat日志中没有错误。

UPDATE: 更新:

It worked once I added <packaging>war</packaging> in my pom.xml and accessed the REST-endpoint via " http://localhost:8080/rest/hello ". 一旦我在我的pom.xml中添加<packaging>war</packaging>并通过“ http:// localhost:8080 / rest / hello ”访问REST端点,它就可以工作了。

<param-value>com.tutorial.example</param-value>

Your class is not in the com.tutorial.example package. 您的类不在com.tutorial.example包中。 It's in the com.tutorial package. 它在com.tutorial包中。 Jersey scans the package you put there for classes, and registers them. 泽西岛扫描您放在那里的课程,并注册它们。 In your case, do classes get picked up. 在你的情况下,课程会被选中。 Make the change accordingly. 相应地进行更改。

Note that the package you put, will be scanned recursively. 请注意,您输入的包将以递归方式扫描。 Also you can put multiple packages separated by a comma. 您还可以将多个包用逗号分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM