简体   繁体   English

Spring的服务不佳导致400错误请求

[英]Restful Service in Spring results in 400 Bad request

I'm having some problems using the Spring restTemplate. 我在使用Spring restTemplate时遇到一些问题。

According to API documentation (ONLY FOR PHP) of Rest Service that i'm trying to access. 根据我试图访问的Rest Service的API文档(仅适用于PHP)。 The correct format of POST are: 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

to get that response: 得到该响应:

{
"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
}

} }

Here's the important part of my code: 这是我的代码的重要部分:

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);

But when i execute that postForObject , the response are a 400 Bad request as if something had sent wrong: 但是,当我执行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

Maybe the format i was sending the data are wrong. 也许我发送数据的格式是错误的。 But i can't figure out how can i manage that information. 但是我不知道如何管理这些信息。

Anyone has any idea of what i'm doing wrong? 有人知道我在做什么错吗?

In your curl request, the body of your HTTP POST is: 在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.\"}

So your endpoint is expecting a standard HTTP POST with a single string called data, with your serialized JSON. 因此,您的端点希望使用带有数据序列号和序列化JSON的单个字符串的标准HTTP POST。 RestTemplate assumes you want to submit this way: 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.\"}

ie, just a JSON object. 即只是一个JSON对象。 You won't be able to use RestTemplate for this. 您将无法为此使用RestTemplate。 Use one of Spring's built-ins for HTTP services. 使用Spring的一种内置 HTTP服务。

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

ObjectMapper mapper = new ObjectMapper();

mapper.writeValue(resp.getWriter(), data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM