在Spring Boot控制器中,我正在接收json,并希望“转发”它而不进行任何处理:
@RequestMapping(value = "/forward", method = RequestMethod.POST)
public void abc(@RequestBody GeneralJsonRepresentation json, HttpServletRequest request) {
restTemplate.postForEntity(endpoint, json, Object.class)
}
是否可以实现此目的,例如使用GeneralJsonRepresentation
的实现,假设控制器不知道json格式并且接收的内容类型是application/json
?
答案 0 :(得分:1)
如果您只使用String
,则可能甚至不需要@RequestMapping(path="/forward", method = RequestMethod.POST)
public ResponseEntity<String> forward(@RequestBody String postData) {
// maybe needed configuration
final RestTemplate restTemplate = new RestTemplateBuilder().basicAuthorization("user", "password").build();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(postData, headers);
final String targetUrl = "http://targethost/endpoint";
final ResponseEntity<String> response = restTemplate.postForEntity(targetUrl, entity, String.class);
return ResponseEntity.created(...).build();
}
。
我创建了一个小型工作片段:
_trial_identifier