我有一个与下列主计长的春季boot:
@ControllerAdvice
public class GlobalResponseStatusExceptionHandler {
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<ResponseStatusExceptionModel> handleBadRequestException(ResponseStatusException ex, WebRequest h) {
// if you want you can do some extra processing with message and status of an exception
// or you can return it without any processing like this:
String path = "";
if (h instanceof ServletWebRequest) {
ServletWebRequest servletWebRequest = (ServletWebRequest) h;
path = servletWebRequest.getRequest().getRequestURI();
}
return new ResponseEntity<>(new ResponseStatusExceptionModel(
ex.getStatusCode().value(),
ex.getReason(),
path
), ex.getStatusCode());
}
}
My ResponseStatusException 模式是:
public class ResponseStatusExceptionModel {
// Example output
// {"timestamp":"2023-12-15T23:25:11.778+00:00","status":403,"error":"Forbidden","message":"Access Denied","path":"/api/savedcontents"}
private String timestamp;
private int status;
private String error;
private String path;
public String getTimestamp() {
return timestamp;
}
public int getStatus() {
return status;
}
public String getError() {
return error;
}
public String getPath() {
return path;
}
public ResponseStatusExceptionModel(int status, String error, String path) {
this.timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.now().atZone(ZoneOffset.UTC)); // this is in iso8601
this.status = status;
this.error = error;
this.path = path;
}
}
I also have the following in my securityConfiguration:
request.requestMatchers("/error").permitAll();
以及我的申请。 注 我:
server:
error:
include-message: always
include-binding-errors: always
include-stacktrace: never
当我通过<代码>curl提出职位要求时 我看不到问题。 但是,当我通过我的浏览器提出请求时,我看不到错误的信息(只有地位法)。 为了展示错误,我需要做些什么? 请注意,当我向GET提出要求时,我能够看到网页的回复。