我是一个SpringBoot应用程序有一个REST控制器,可以处理几种情况,其中一种情况必须转发到另一个控制器。
@PutMapping(
value = "/rest/endpoint",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<CustomObject> doPut(@RequestBody myDataToBeHandled) {
if(caseAHolds(myDataToBeHandled){
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
else if(caseBHolds(myDataToBeHandled){
return new ResponseEntity<>(null, HttpStatus.OK);
}
else if(caseCHolds(myDataToBeHandled){
// Redirect here
}
}
我看过an example如何为重定向做这件事?
答案 0 :(得分:0)
您需要设置Location
标头,如下所示,以便将请求重定向到另一个网址,如下所示:
@PutMapping(
value = "/rest/endpoint",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<CustomObject> doPut(@RequestBody myDataToBeHandled) {
if(caseAHolds(myDataToBeHandled){
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
else if(caseBHolds(myDataToBeHandled){
return new ResponseEntity<>(null, HttpStatus.OK);
}
else if(caseCHolds(myDataToBeHandled){
// Redirect here
HttpHeaders headers = new HttpHeaders();
headers.add("Location", "ADD_URL_HERE");
return new ResponseEntity<CustomObject>(headers, HttpStatus. OK);
}
}