2017-02-09 86 views
0

在春季啓動控制器,我收到JSON和希望「向前」它沒有任何處理:春季啓動控制器轉發一般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

如果您只是使用String,您甚至可能不需要GeneralJsonRepresentation

我創建了一個小型工作片段:

@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(); 
}