Spring ResponseEntity 并转发
Spring ResponseEntity and forward
我的 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 如何为重定向执行此操作?
您需要如下所示设置 Location
header,以便将请求重定向到另一个 URL,如下所示:
@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);
}
}
我的 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 如何为重定向执行此操作?
您需要如下所示设置 Location
header,以便将请求重定向到另一个 URL,如下所示:
@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);
}
}