我有两个休息控制器。两者都是后映射端点。
在第一个控制器 (Controller1) 使我需要的所有东西都成为一个对象之后,我想调用/重定向第二个控制器以便继续,然后从它那里得到响应。
@RestController
public class Controller1{
@PostMapping("/endpoint1")
public ReponseEntity<?> doWhatController1HasToDo(@RequestBody Object request){
//some processing
//here i would like to call second controller
}
}
@RestController
public class Controller2{
@PostMapping("/endpoint2")
public ReponseEntity<?> doWhatController2HasToDo(@RequestBody Object request){
//some processing
return new ResponseEntity<>(JSON, HttpStatus.OK);
}
}
我尝试过使用 RestTemplate,但总是出现 405 错误。我在某处读过,这是因为多部分
private ResponseEntity forwardUsingRestTemplate(HttpServletRequest httpServletRequest, Object object) throws MalformedURLException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity req = new HttpEntity(object, headers);
RestTemplate template = new RestTemplate();
ResponseEntity<TdmResponse> response = template.exchange(getBaseUrl(httpServletRequest) + "/endpoint2", HttpMethod.POST, req, TdmResponse.class);
}
问题是,我如何调用第二个端点?
慕慕森
Cats萌萌
浮云间
相关分类