【问题标题】:Handling multiple 400 errors on one functionality在一项功能上处理多个 400 错误
【发布时间】:2019-08-16 09:35:28
【问题描述】:

您好,我需要在一个功能下处理多个 400 错误,但我似乎无法让它在 Angular 中工作。

下面的代码应该可以工作,但我无法从 Angular 的 HttpErrorResponse 中的响应中获取 LOCKED_ERROR 部分。

BadRequestException.java

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class BadRequestException extends ApiException {
    private static final long serialVersionUID = 4043436953989551177L;

    public BadRequestException(String errorCode, String message) {
        super(HttpStatus.BAD_REQUEST, errorCode, message);
    }
    public BadRequestException(String message) {
        super(HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST.toString(), message);
    }
}

ShiftWorkerLockedException.java

public class ShiftWorkerLockedException extends BadRequestException {
    public ShiftWorkerLockedException() { super("LOCKED_ERROR", "Shift worker is locked."); }
}

这是角码:

this.shiftService.assignJobToWorker(worker,
      job,
      startTime.format(),
      endTime.format()).subscribe(
        (data) => {
        this.toastr.success('Töö määratud kasutajale ' + worker.user.name + ' algusega ' + startTime.format('HH:mm'));
        this.refreshShiftWorkers.emit();
      }, err => {
        console.log(err);
        this.refreshShiftWorkers.emit();
      }
    );

这是没有自定义 errorCode 的错误响应:

error: "Bad Request"
message: "Shift worker is locked."
path: "/shift-jobs/101/worker"
status: 400
timestamp: "2019-08-16T09:32:30.627+0000"

我想使用烤面包机打印不同语言的错误,具体取决于自定义状态码,但我无法得到正确的响应,该怎么办?

【问题讨论】:

    标签: angular spring spring-boot


    【解决方案1】:

    我建议您使用来自

    的 @ControllerAdvice 注释

    springframework.web.bind.annotation

    包。它允许您定义一个可以拦截您的异常的类。它看起来像这样:

            // If you want to add more types of exceptions to handle, you can add 
            // extra methods like this one in this class
            @ControllerAdvice
            public class ControllerExceptionHandler {
    
                // With this annotation you specify the type of class that you will handle
                // and you add your custom response
                @ExceptionHandler(BadRequestException.class)
                public @ResponseBody
                ResponseEntity<CustomErrorResponse> handleApiException(ApiException exception) {
                    return new ResponseEntity<>(new CustomErrorResponse(exception.getMessage()), exception.getStatus());
                }
    }
    

    因此,您可以做的是创建一个自定义类,其中包含您想要的响应中的任何内容,这样您就可以在 Angular 端访问包含在该类中的数据作为对象。类似的东西:

    @NoArgsConstructor
    class CustomErrorResponse implements Serializable {
    
        private static final long serialVersionUID = -6883129706029524672L;
    
        // Use here whatever you need, in this case is a set of Strings
        @Getter
        @Setter
        private Set<String> messages = new HashSet<>();
    
        ErrorResponse(Set<String> messages) {
            this.messages.addAll(messages);
        }
    
        ErrorResponse(String message) {
            messages.add(message);
        }
    }
    

    关于这东西的官方文档真的很好,所以我建议你看看它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 2014-09-22
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      相关资源
      最近更新 更多