如何在 Mono<ResponseEntity> 中正确地 return HttpStatus?
How to properly return HttpStatus in Mono<ResponseEntity>?
我有一个 api,其中 return 是一些 HttpStatus
代码。并根据代码,看门人将执行一些操作。以下只是 API.
的骨架
@GetMapping("/globalretry")
public Mono<ResponseEntity> testGlobalRetryFilter(@RequestParam(name = "code") int code) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("code", code);
switch (code) {
case 200:
map.put("status", "SUCCESS");
break;
case 504:
map.put("status", "RETRY: GATEWAY_TIMEOUT");
break;
default:
map.put("status", "BAD_REQUEST");
break;
}
return Mono.just(new ResponseEntity(map, HttpStatus.valueOf(code)));
}
现在的问题是,如果我以这种方式 return 响应代码,那么 spring 无法识别来自 Mono<ResponseEntity>
的状态代码。任何人都可以帮助我如何 return statuscode
从而 spring 可以识别响应的状态代码
您可以创建自己的对象加上您必须添加的构造 HTTP_STATUS
示例:
new ResponseEntity<>(new ErrorResponse(HttpStatus.BAD_REQUEST, LOGIN_NOT_UNIQUE_MSG), HttpStatus.BAD_REQUEST);
public class ErrorResponse {
private HttpStatus status;
private int code;
private String message;
public ErrorResponse(HttpStatus status, String message) {
this.message = message;
this.status = status;
this.code = status.value();
}
}
我有一个 api,其中 return 是一些 HttpStatus
代码。并根据代码,看门人将执行一些操作。以下只是 API.
@GetMapping("/globalretry")
public Mono<ResponseEntity> testGlobalRetryFilter(@RequestParam(name = "code") int code) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("code", code);
switch (code) {
case 200:
map.put("status", "SUCCESS");
break;
case 504:
map.put("status", "RETRY: GATEWAY_TIMEOUT");
break;
default:
map.put("status", "BAD_REQUEST");
break;
}
return Mono.just(new ResponseEntity(map, HttpStatus.valueOf(code)));
}
现在的问题是,如果我以这种方式 return 响应代码,那么 spring 无法识别来自 Mono<ResponseEntity>
的状态代码。任何人都可以帮助我如何 return statuscode
从而 spring 可以识别响应的状态代码
您可以创建自己的对象加上您必须添加的构造 HTTP_STATUS 示例:
new ResponseEntity<>(new ErrorResponse(HttpStatus.BAD_REQUEST, LOGIN_NOT_UNIQUE_MSG), HttpStatus.BAD_REQUEST);
public class ErrorResponse {
private HttpStatus status;
private int code;
private String message;
public ErrorResponse(HttpStatus status, String message) {
this.message = message;
this.status = status;
this.code = status.value();
}
}