简体   繁体   English

使用Jersey和Apache Tomcat用于Web Api的404错误

[英]404 Error using Jersey and Apache Tomcat for Web Api

I am trying to play around with Eclipse/Java and I am making a Dynamic Web Project in Eclipse. 我正在尝试使用Eclipse / Java,并且正在Eclipse中创建一个动态Web项目。 I have followed tutorials online and looked at several other Stackoverflow posts, but i'm still having the same problem. 我在网上关注了教程,并查看了其他一些Stackoverflow帖子,但是我仍然遇到同样的问题。

When I run my server and try to hit my endpoint /test , I get a 404 error. 当我运行服务器并尝试命中端点/test ,出现404错误。 Here is my code: 这是我的代码:

My Java Class: 我的Java课:

package TestPackage;

import javax.ws.rs.Path;
import javax.ws.rs.GET;

@Path("/test")
public class Test {
    @GET
    public String Hello() {
        return "hello";
    }
}

My Pom.xml: 我的Pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RestDemo</groupId>
  <artifactId>RestDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.25</version>
    </dependency>
  </dependencies>
</project>

I read that if you're using Jersey 2.+, you wouldn't have to fiddle around with your web.xml , so it's the standard format: 我读到,如果您使用的是Jersey 2. +,则不必摆弄web.xml ,因此它是标准格式:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>RestDemo</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>
</web-app>

So when I try to call http://localhost:8080/RestDemo/test , I get a 404 error. 因此,当我尝试调用http://localhost:8080/RestDemo/test ,出现404错误。 Any idea what's going on? 知道发生了什么吗?

In Servlet 2.5 environment, you have to explicitly declare the Jersey container Servlet in your Web application's web.xml deployment descriptor file. 在Servlet 2.5环境中,您必须在Web应用程序的web.xml部署描述符文件中显式声明Jersey容器Servlet。

As per https://jersey.java.net/documentation/latest/user-guide.html#deployment.servlet 根据https://jersey.java.net/documentation/latest/user-guide.html#deployment.servlet

Example 4.10. 示例4.10 Hooking up Jersey as a Servlet 将Jersey连接为Servlet

<web-app>
    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            ...
        </init-param>
    </servlet>
    ...
    <servlet-mapping>
        <servlet-name>MyApplication</servlet-name>
        <url-pattern>/myApp/*</url-pattern>
    </servlet-mapping>
    ...
</web-app>

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

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