【发布时间】:2017-05-14 13:37:35
【问题描述】:
我正在学习如何使用 Restful Webservice,我真的不知道我的方法是好还是完全错误,所以请多多包涵
我有一个项目结构,如下所示:
我想通过调用正确的 URL,将字符串相应地保存在 Datum.Json
这是我的 WebService Java 类:
package Rest;
@Path("/calendar")
public class CalendarTest {
public List<Date> dates;
@GET
@Path("/dates/get/")
@Produces(MediaType.APPLICATION_XML)
public List<Date> getUsers(){
return dates;
}
@PUT
@Path("/dates/put/{param1}+{param2}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void updateDate(@PathParam("param1") String city, @PathParam("param2") String date) throws IOException {
JSONObject obj = new JSONObject();
obj.put("City", city);
obj.put("Date", date);
try (FileWriter file = new FileWriter("/RestTest/Datum.json")) {
file.write(obj.toJSONString());
System.out.println("Successfully Copied JSON Object to File...");
System.out.println("\nJSON Object: " + obj);
}
}
}
我测试了本地主机,它工作正常(我可以用本地主机打开 form.html)
我的web.xml 文件:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>Rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<security-constraint>
</security-constraint>
</web-app>
但是当我尝试 URL http://localhost:8080/RestTest/rest/calendar/dates/put/Berlin+20-12-2019
它说该方法不允许。
谁能给我解释一下为什么?
【问题讨论】:
标签: java web-services rest jersey