簡體   English   中英

服務器總是返回澤西2 + Tomcat 6的404錯誤

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

我正在嘗試使用maven運行使用Jersey 2Tomcat 6運行的簡單REST服務。 IntelliJ說我的應用程序已成功部署,我可以訪問我的index.jsp文件。 嘗試導航到我的休息端點時,我總是收到404錯誤消息:

HTTP://本地主機:8080 / REST /你好

HTTP://本地主機:8080 /的HelloWorld / REST /你好

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中:

<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:

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!";
    }
}

在此輸入圖像描述

我的網址有問題嗎? tomcat日志中沒有錯誤。

更新:

一旦我在我的pom.xml中添加<packaging>war</packaging>並通過“ http:// localhost:8080 / rest / hello ”訪問REST端點,它就可以工作了。

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

您的類不在com.tutorial.example包中。 它在com.tutorial包中。 澤西島掃描您放在那里的課程,並注冊它們。 在你的情況下,課程會被選中。 相應地進行更改。

請注意,您輸入的包將以遞歸方式掃描。 您還可以將多個包用逗號分隔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM