【问题标题】:Writing to JSONobject through Restful Webservice通过 Restful Webservice 写入 JSONobject
【发布时间】: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


    【解决方案1】:

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

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

    How to send a PUT/DELETE request in jQuery?

    你可以使用ajax方法:

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

    【讨论】:

    • 那么我如何访问 Put 方法?谢谢!
    • @TuấnPhạm 一种选择是 jQuery,还有 AngularJS 框架。如果你使用 POST 而不是 PUT,你可以使用 HTML 表单来提交请求
    猜你喜欢
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多