spring boot 中是否有 zalando-problem 实现的好例子?

Sha*_*esh 3 java exception spring-boot

我们在 spring-boot 应用程序中使用 zalando-problem 进行异常处理。但看起来我们的问题处理程序从未被调用过。相反,spring boot 为所有异常返回 500 Internal server error。如果你能提供一些例子,它会很有帮助。我在 spring boot 中找不到 zalando-problem 实现的好例子

如果用户未登录,则代码抛出 SSOAuthenticationException 异常。

@Immutable
public class SSOAuthenticationException extends AbstractThrowableProblem {

    private final String errorMessage;

    public SSOAuthenticationException( final String errorMessage ) {
        super( ErrorConstants.SSO_CACHE_AUTHENTICATION_FAILED, errorMessage, Status.UNAUTHORIZED );
        this.errorMessage = errorMessage;
    }

    public String getErrorMessage(){
        return errorMessage;
    }
    @Override
    public String toString() {
        return "SSOAuthenticationException{}";
    }
}
Run Code Online (Sandbox Code Playgroud)

以及异常处理代码:

@ControllerAdvice
public class ExceptionTranslator implements ProblemHandling {
    @Override
    public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
        if (entity == null) {
            return entity;
        }
        Problem problem = entity.getBody();
        if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
            return entity;
        }
        ProblemBuilder builder = Problem.builder()
            .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
            .withStatus(problem.getStatus())
            .withTitle(problem.getTitle())
            .with("path", request.getNativeRequest(HttpServletRequest.class).getRequestURI());

        if (problem instanceof ConstraintViolationProblem) {
            builder
                .with("violations", ((ConstraintViolationProblem) problem).getViolations())
                .with("message", ErrorConstants.ERR_VALIDATION);
        } else {
            builder
                .withCause(((DefaultProblem) problem).getCause())
                .withDetail(problem.getDetail())
                .withInstance(problem.getInstance());
            problem.getParameters().forEach(builder::with);
            if (!problem.getParameters().containsKey("message") && problem.getStatus() != null) {
                builder.with("message", "error.http." + problem.getStatus().getStatusCode());
            }
        }
        return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
    }

    @ExceptionHandler(SSOAuthenticationException.class)
    @ResponseBody
    public ResponseEntity<Problem> handleUnAuthenticatedUser(SSOAuthenticationException ex, NativeWebRequest request) {
        Problem problem = Problem.builder()
            .withStatus(Status.UNAUTHORIZED)
            .with("message", ErrorConstants.SSO_CACHE_AUTHENTICATION_FAILED)
            .build();
        return create(ex, problem, request);
    }
}

Run Code Online (Sandbox Code Playgroud)

当我在调试器中运行时,我注意到从未调用异常处理程序。相反,代码认为没有注册处理程序(在 ServletInitialHandler.java 中,它转到用于未处理异常的 else 部分)并将状态代码更改为 INTERNAL_SERVER_ERROR。所以对于所有的异常,应用程序都会抛出错误 500。异常处理代码有什么问题?我们是否必须包含 AdviceTrait?我也试过了。但看起来这也不起作用。如果你能解释处理这个异常的正确方法和一个例子,它会有所帮助。谢谢

mth*_*mas 6

使用Problem Spring Web 0.25.2,我首先创建了一个新的AdviceTrait类似于现有的:

public interface CustomAdviceTrait extends AdviceTrait {

  @ExceptionHandler
  default ResponseEntity<Problem> handleCustomException(final CustomException exception, final NativeWebRequest request) {
    return create(Status.INTERNAL_SERVER_ERROR, exception, request);
  }
}
Run Code Online (Sandbox Code Playgroud)

如果需要,这也是可以转换为问题的地方。

然后,同样为了启用专用的内置建议特征,我通过在我的 ExceptionHandler 上实现相应的接口来启用它们:

@ControllerAdvice
public class FootlooseServiceExceptionHandler implements ProblemHandling, CustomAdviceTrait { }
Run Code Online (Sandbox Code Playgroud)