如何修复JavaRESTAPI?保持404';s
我按照本教程()使用tomcat在java中创建了一个简单的RESTAPI,但我一直遇到404错误,我看不出哪里出了问题 我猜错误在我的配置中的某些地方 web.xml如何修复JavaRESTAPI?保持404';s,java,rest,api,maven,web-applications,Java,Rest,Api,Maven,Web Applications,我按照本教程()使用tomcat在java中创建了一个简单的RESTAPI,但我一直遇到404错误,我看不出哪里出了问题 我猜错误在我的配置中的某些地方 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Jersey Web Application</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.demo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我的项目结构
将资源添加到WEB-INF
Tomcat配置
404来自本地主机:8080/rest/myresource
因为MyResource.java驻留在演示包中。您需要更改web.xml中的包映射 (jersey.config.server.provider.packages的参数值) 更新的web.xml
Jersey Web应用程序
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
演示
1.
Jersey Web应用程序
/休息/*
应用程序的上下文根是什么?按上下文根指的是本地主机上下文吗?如果您查看tomcat配置图像,它应该是http://localhost:8080/
和应用程序上下文/
使用@Path(“/myresource”)而不是@Path(“myresource”)@Path(/myresource”)
无法帮助@Nitikachange com.demo进行演示,因为myresource.java驻留在演示中
<?xml version="1.0" encoding="UTF-8"?>
<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>com.demo</groupId>
<artifactId>SimpleJersey</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.23.2</jersey.version>
</properties>
</project>
package demo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Created by marom on 27/09/16.
*/
@Path("myresource")
public class MyResource {
/**
* 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!";
}
}