[英]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:我做了两件事:
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.