我需要使用restTemplate发送一个jsonArray,但是我不知道正确的方法。这是我的代码。有人可以告诉我问题出在哪里吗?
requestHandler:
@CrossOrigin
@RequestMapping(path = "/duplication", method = RequestMethod.GET)
public ResponseEntity<Boolean> checkDeviceDuplication(
@RequestHeader("token") String token,
@RequestBody Set<DeviceCheckDuplicationMessage> devicesToCheck
)
{
System.out.println("size:" + devicesToCheck.size());
}
restTemplate客户端:
String authorityValidUrl = "http://192.168.227.100:15004/duplication";
RestTemplate restTemplate = restTemplateBuilder.build();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkaXNwbGF5TmFtZSI6IuWuieaWh-mRqyIsInJvbGVzIjpbIlJPTEVfQ0hFQ0tBTkRGSVhNQU5BR0VSIiwiUk9MRV9QTEFORU1BTkFHRVIiLCJST0xFX1BST0pFQ1RNQU5BR0VSIiwiUk9MRV9URUNIU1RBVFVTTUFOQUdFUiIsIlJPTEVfQ0FUQUxPR1VFTUFOQUdFUiIsIlJPTEVfVFJJUE1BTkFHRVIiLCJST0xFX1VTRVIiXSwiZXhwIjoxNTQ1Mzk2MDc0LCJ1c2VybmFtZSI6ImFud3gifQ.tM2t7yLcg9fHr78KtCf0ocscy6nOAZioJ0V_zGERKaM");
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
Set<DeviceCheckDuplicationMessage> devicesToCheck = new HashSet<>();
devicesToCheck.add(new DeviceCheckDuplicationMessage("cn", "en", "model", "sn"));
devicesToCheck.add(new DeviceCheckDuplicationMessage("name_cn1", "name_en1", "model1", "sn1"));
String body = JSON.toJSONString(devicesToCheck);
HttpEntity<String> requestEntity = new HttpEntity<>(body, requestHeaders);
requestEntity.getBody();
ResponseEntity<Boolean> validResult = restTemplate.exchange(
new URI(authorityValidUrl),
HttpMethod.GET,
requestEntity,
Boolean.class
);
现在requestHandler告诉我缺少的requestBody。
但是我使用邮差请求具有相同的标头,并且jsonData可以。
答案 0 :(得分:0)
我尝试更改http方法以将对象发布并请求到对象。而且有效。
您无需显式设置请求的Content-Type标头。在大多数情况下,您可以找到基于源对象类型的兼容消息转换器,并且所选消息转换器会相应地设置内容类型。如有必要,可以使用交换方法显式提供Content-Type请求标头,从而影响选择哪种消息转换器。
和
在HttpMessageConverter的帮助下,传递到RestTemplate方法和从RestTemplate方法返回的对象与原始内容进行转换。
在POST上,输入对象被序列化到请求正文
在GET上,响应主体反序列化为输出对象
所以我将代码更改为此并且可以正常工作。
@Test
public void testValid() {
try {
String authorityValidUrl = "http://192.168.227.100:15004/duplication";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkaXNwbGF5TmFtZSI6IuWuieaWh-mRqyIsInJvbGVzIjpbIlJPTEVfQ0hFQ0tBTkRGSVhNQU5BR0VSIiwiUk9MRV9QTEFORU1BTkFHRVIiLCJST0xFX1BST0pFQ1RNQU5BR0VSIiwiUk9MRV9URUNIU1RBVFVTTUFOQUdFUiIsIlJPTEVfQ0FUQUxPR1VFTUFOQUdFUiIsIlJPTEVfVFJJUE1BTkFHRVIiLCJST0xFX1VTRVIiXSwiZXhwIjoxNTQ1NzE2NzQ1LCJ1c2VybmFtZSI6ImFud3gifQ.two6KeWnV2egnEk4QC3PuIPJfiRSSReNtKfzbE9qSQQ");
//requestHeaders.setContentType(MediaType.APPLICATION_JSON);
Set<DeviceCheckDuplicationMessage> devicesToCheck = new HashSet<>();
devicesToCheck.add(new DeviceCheckDuplicationMessage("cn", "en", "model", "sn"));
devicesToCheck.add(new DeviceCheckDuplicationMessage("name_cn1", "name_en1", "model1", "sn1"));
HttpEntity<Set<DeviceCheckDuplicationMessage>> requestEntity = new HttpEntity<>(devicesToCheck, requestHeaders);
ResponseEntity<Boolean> validResult = restTemplate.exchange(
new URI(authorityValidUrl),
HttpMethod.POST, //HttpMethod.GET,
requestEntity,
Boolean.class
);
boolean result = Boolean.valueOf(validResult.getBody());
System.out.println(result);
System.out.println(validResult.getStatusCodeValue());
}catch(Exception e) {
e.printStackTrace();
}
}