【发布时间】:2017-01-20 07:05:19
【问题描述】:
我想让这个网址起作用
http://localhost:8080/HomeController/students/getAllStudents
我得到这个错误:
请求的资源不可用
这是我的代码:
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
【问题讨论】:
-
你的java代码能编译吗?也许我错了,但 java 代码中缺少右括号
}。该方法需要在第二个开始之前关闭。 -
您是否尝试将 url-pattern 设置为
/api? -
您是否缺少 URL 中的上下文? localhost:8080/StudentsService/api/students/getAllStudents 有什么作用吗?或者可能更简单,localhost:8080/StudentsService