如何设置restTemplate参数类型?

时间:2014-02-09 07:25:10

标签: spring rest type-conversion resttemplate bad-request

这是我的代码

RestTemplate restTemplate = new RestTemplate();

    MultiValueMap<String, Object> vars = new LinkedMultiValueMap<String, Object>();
    vars.add("name", fileName);
    vars.add("parent", "43");
    vars.add("size_s3", size_s3);
    vars.add("size_ssg", size_ssg);
    vars.add("size", size);

    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization","token "+user.getToken());
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<?> entity = new HttpEntity<MultiValueMap<String, Object>>(vars, headers);
    result = restTemplate.exchange(RestInfo.restURL+"/files", HttpMethod.POST, entity, String.class).getBody();

httpClientErrorException是400 BAD REQUEST

{“size_s3”:[“输入一个整数。”],“父”:[“不正确的类型。预期的pk值,收到的列表。”],“大小”:[“输入一个整数。”] }

我认为参数类型问题。所以我试着像这样改变我的代码

vars.add(“size_s3”,Integer.parseInt(size_s3));

但结果相同......

如何设置restTemplate参数类型?

帮帮我PLZ!

1 个答案:

答案 0 :(得分:0)

RestTemplate restTemplate = new RestTemplate();

MultiValueMap<String, Object> vars = new LinkedMultiValueMap<String, Object>();
vars.add("name", fileName);
vars.add("parent", 43);
vars.add("size_s3", size_s3);
vars.add("size_ssg", size_ssg);
vars.add("size", size);

HttpHeaders headers = new HttpHeaders();
headers.set("Authorization","token "+user.getToken());
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<?> entity = new HttpEntity<Object>(vars, headers);
result = restTemplate.exchange(RestInfo.restURL+"/files", HttpMethod.POST, entity, String.class).getBody();

我将在您的代码中进行一些更改尝试