English 中文(简体)
主计长办公室为什么没有显示例外机构
原标题:Why does POST in ControllerAdvice not show the Exception body

我有一个与下列主计长的春季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提出要求时,我能够看到网页的回复。

问题回答

这是我个人使用的:

@RestControllerAdvice
public class ExceptionRestController {

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = { FeignException.class })
    public ExceptionResponse handleFeignException(FeignException exception) {
        return new ExceptionResponse(exception);
    }

}

因此,我认为你错过了@ResponseBody

我的问题不是在服务器的回复上,而是放在服务器代码上。 如果它没有200个地位法,则没有阅读答复。





相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签