1

我在使用 Spring restTemplate 时遇到了一些问题。

根据我尝试访问的 Rest Service 的 API 文档(仅适用于 PHP)。POST 的正确格式是:

curl -d "data={\"client_email\":\"steve@martiancraft.com\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"}" http://www.setster.com/api/v2/company/7089/appointment?session_token=niab4ptf9mjjem41cooso389f3

得到回应:

{
"statusCode":0,
"statusDescription":"OK",
"data":{
    "status":2,
    "client_id":103352,
    "client_email":"steve@martiancraft.com",
    "client_name":"Steve Ryner",
    "company_id":"7089",
    "employee_id":9862,
    "location_id":"13832",
    "start_date":"2012-08-15 14:00",
    "end_date":"2012-08-15 15:00",
    "length":3600000,
    "note":"This is a test.",
    "service_id":20216,
    "type":"60 Min Swedish",
    "duration_padding":0,
    "repeat_type":0,
    "subservices":"",
    "timezone_dif":-18000,
    "price":"60",
    "custom_fields":"[]",
    "client_phone":"",
    "client_address":"",
    "payment_pending":0,
    "id":171302483
}

}

这是我的代码的重要部分:

RestTemplate restTemplate = new RestTemplate();

Map<String, String> data = new HashMap<String, String>();

        data.put("client_name", "Alexandre Moraes");
        data.put("client_email", "kalvinmoraes@gmail.com");
        data.put("client_phone", "98065867");
        data.put("employee_id", "0");
        data.put("location_id", "16675"); // Here i have to specify the "Location_ID" because there is more than just one
        data.put("start_date", "2013-05-03 09:15:00");
        data.put("service_id", "18499");

        String result = restTemplate.postForObject("http://setster.com/api/v2/company/6788/appointment/?session_token="+session_token, data, String.class);

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.println(result);

但是当我执行那个postForObject时,响应是一个400 Bad 请求,好像发送了错误的东西:

WARNING: POST request for "http://setster.com/api/v2/company/6788/appointment/?session_token=[censored]" resulted in 400 (Bad Request); invoking error handler
WARNING: StandardWrapperValve[setster]: PWC1406: Servlet.service() for servlet setster threw exception

也许我发送数据的格式是错误的。但我不知道如何管理这些信息。

任何人都知道我做错了什么?

4

2 回答 2

1

在您的 curl 请求中,您的 HTTP POST 的正文是:

data={\"client_email\":\"steve@martiancraft.com\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"}

因此,您的端点需要一个标准的 HTTP POST,其中包含一个名为 data 的字符串,以及您的序列化 JSON。RestTemplate 假设您要以这种方式提交:

{\"client_email\":\"steve@martiancraft.com\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"}

即,只是一个 JSON 对象。您将无法为此使用 RestTemplate。使用Spring 的HTTP 服务内置插件之一。

于 2013-04-02T15:51:45.733 回答
0

使用 Jackson lib - 将对象转换为 json 的简单方法:

ObjectMapper mapper = new ObjectMapper();

mapper.writeValue(resp.getWriter(), data);
于 2013-04-02T15:26:00.180 回答