java - 通过 Restful Webservice 写入 JSONobject

标签 java web-services rest jersey

我正在学习如何使用 Restful Webservice,我真的不知道我的方法是好的还是完全错误的,所以请多多包涵

我有一个项目结构,看起来像这样:

enter image description here

我想通过调用正确的 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

它说这个方法不允许。

谁能给我解释一下为什么?

最佳答案

这是因为当你在浏览器中输入一个 URL 时,它默认发送一个 HTTP GET 请求,所以它会抛出一个错误,因为你没有那个 URL 的 GET 请求处理程序,你只有一个处理程序PUT 请求。

您无法更改浏览器的默认请求类型。您需要做的是在您的前端/javascript 中使用类似 jQuery 的东西自己发送请求。

How to send a PUT/DELETE request in jQuery?

You could use the ajax method:

$.ajax({
    url: '/rest/calendar/dates/put/Berlin+20-12-2019',
    type: 'PUT',
    success: function(result) {
        // Do something with the result
    }
});

关于java - 通过 Restful Webservice 写入 JSONobject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43964512/

相关文章:

java - 替换字符串中的数字

web-services - Restful API - 处理大量数据

java - 如何修复此错误 "XYZ does not have a no-arg constructor"

web-services - RESTful API 响应状态码消歧

json - 如何使用 Advanced REST Client 或 Postman 测试 Express/node REST API 后端?

javascript - Angular JS : rest API is called with a delay

java - 无法实现组件的特定布局

java - android4.4设备恢复出厂设置后,系统app收不到BOOT_COMPLETED广播

java - 为什么 swagger ui 不显示我带注释的 REST 方法?

java - 如何取消代理 hibernate 对象