简体   繁体   English

运行 REST API 时未找到 Tomcat 错误 404

[英]Tomcat error 404 not found while running a REST API

I am trying to implement a very simple REST API in Eclipse.我正在尝试在 Eclipse 中实现一个非常简单的 REST API。

Following is my project Structure: project Structure以下是我的项目结构:项目结构

Hello.java:您好。java:

package test;

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

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

    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayHelloXML() {
        String responseString = "<? xml version='1.0' ?>" + 
                "<hello> Hello World XML</hello>";
        
        return responseString;
    }
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String sayHelloJSON() {
        String responseString = null;
        return responseString;
    }
    
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHelloHTML() {
        String responseString = "<h1> Hello World HTML</h1>";
        return responseString;
    }
}

web.xml: 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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>JavaApiEdu</display-name>
  
  <servlet>
    <servlet-name>JAVA API</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer.class</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>test</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>JAVA API</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

To Run, I right-click on the project and choose Run on Server which is Tomcat 8.5.要运行,我右键单击项目并选择在服务器上运行,即 Tomcat 8.5。 I am unable to access this and see this page: Error我无法访问此页面并看到此页面:错误

It seems like you have to specify Path for each @GET .似乎您必须为每个@GET指定Path Eg,例如,

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

    @Path("xml")
    @GET
    @Produces(MediaType.TEXT_XML)
    ...

Then browse to http://localhost:8080/test/rest/hello/xml ?然后浏览到http://localhost:8080/test/rest/hello/xml

After struggling a lot there are generally small things that solve the issues.在经历了很多挣扎之后,通常有一些小事情可以解决问题。

I did two things:我做了两件事:

  1. I removed the.class from this servlet class name in my web.xml and it started working: web.xml I removed the.class from this servlet class name in my web.xml and it started working: web.xml
  2. I also gave a path to all my methods as GyuHyeon suggested.正如 GyuHyeon 建议的那样,我还给出了我所有方法的路径。 Although if I don't do this then also it's working after doing step 1. It will just go to one of the methods as opposed to a specific one.虽然如果我不这样做,那么它在执行第 1 步之后也可以工作。它只会 go 使用其中一种方法,而不是特定的方法。 Giving path to each method is a good step to follow.为每种方法提供路径是一个很好的步骤。

Note: to access the API the project name should be in the URL and not the package, so in my case, this worked: http://localhost:8080/JavaApiEdu1/hello/html Note: to access the API the project name should be in the URL and not the package, so in my case, this worked: http://localhost:8080/JavaApiEdu1/hello/html

Thanks @GyuHyeonChoi & @tgdavies for replying.感谢@GyuHyeonChoi 和@tgdavies 的回复。

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

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