使用JSON的RestTemplate.exchange

时间:2017-11-26 20:33:41

标签: java json rest resttemplate

我有两个服务(一个服务调用一个端点localhost:7000/create来发送一个json,并且想要一个json)

被叫服务是这样的(数据是pojo类):

@RequestMapping(value="/create",consumes="application/json",produces = "application/json",method = RequestMethod.POST)
@ResponseBody
public Data responseForPayload(@RequestBody String data) {
    Data data= new Data();
    data.setAccountId("45");
    return data;
}

并且调用服务restTemplate调用是这样的(在:9000中运行):

    String ResourceUrl = "http://localhost:7000/create";  
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    List list= new ArrayList();
    list.add(MediaType.APPLICATION_JSON);
    HttpEntity httpEntity = new HttpEntity(downstreamPayload, headers);
    ResponseEntity<JSONObject> response = restTemplate.exchange(ResourceUrl, 
     HttpMethod.POST, httpEntity, JSONObject.class);

但我得到响应{},当我使用字符串而不是JSON时,它工作正常。我使用 postma n来调用 127.0.0.1:7000/create,它可以正常返回预期的json

这里我的错误是什么? 谢谢

0 个答案:

没有答案
相关问题