使用problem-spring-webflux进行ApiException封装

java 8个月前 will
530 0

使用problem-spring-webflux进行ApiException封装

<dependency>
            <groupId>org.zalando</groupId>
            <artifactId>problem-spring-webflux</artifactId>
            <version>0.25.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

添加配置项


import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.info.Info; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.server.WebExceptionHandler; import org.zalando.problem.ProblemModule; import org.zalando.problem.spring.webflux.advice.ProblemExceptionHandler; import org.zalando.problem.spring.webflux.advice.ProblemHandling; import org.zalando.problem.violations.ConstraintViolationProblemModule; @OpenAPIDefinition(info = @Info(title = "Minlia Api Documentation", version = "v1.0.0", description = "Minlia Api Documentation")) @Configuration class ExceptionHandlingConfig { @ControllerAdvice public static class ExceptionHandler implements ProblemHandling {//, ApiExceptionAdviceTrait { } @Bean public ObjectMapper objectMapper() { return new ObjectMapper().registerModules(new ProblemModule(), new ConstraintViolationProblemModule()); } @Bean public ProblemHandling problemHandling() { return new ExceptionHandler(); } @Bean public ProblemModule problemModule() { return new ProblemModule(); } @Bean public ConstraintViolationProblemModule constraintViolationProblemModule() { return new ConstraintViolationProblemModule(); } @Bean @Order(-2) // The handler must have precedence over WebFluxResponseStatusExceptionHandler and Spring Boot's ErrorWebExceptionHandler // public WebExceptionHandler problemExceptionHandler(ObjectMapper mapper, ProblemHandling problemHandling) { public WebExceptionHandler problemExceptionHandler( ) { return new ProblemExceptionHandler(objectMapper(), problemHandling()); } }
版权声明:will 发表于 2020-03-23 9:44:46。
转载请注明:使用problem-spring-webflux进行ApiException封装 | 敏利雅

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...