java尝试执行get时请求的资源不可用
我想让这个url正常工作
http://localhost:8080/HomeController/students/getAllStudents
我得到了这个错误:
The requested resource is not available
这是我的密码:
package controller;
@Path("/students")
public class HomeController {
@Path("/add")
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public static String add(Student student) throws Exception {
@Path("/getAllStudents")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getAllStudents() throws Exception {
return "dasda";
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>StudentsService</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>
<servlet>
<servlet-name>
Jersey REST Service
</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>
com.sun.jersey.config.property.packages
</param-name>
<param-value>
controller
</param-value>
</init-param>
<init-param>
<param-name>
com.sun.jersey.api.json.POJOMappingFeature
</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/HomeController/*</url-pattern>
</servlet-mapping>
</web-app>
在第一个链接工作后,我希望这个链接也能工作
http://localhost:8080/students/getAllStudents
# 1 楼答案
@Path应该包含,但结尾不能有斜杠,这使得路径类似于/students/。 此外,控制器包还没有映射到Jersey包,它应该在其中找到控制器。 请尝试上面发布的更正代码段