Spring Boot控制器转发一般json

时间:2017-02-09 14:40:25

标签: json spring

在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

1 个答案:

答案 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