关于java:RestControllerAdvice在Spring Boot 2.3中未调用

RestControllerAdvice not called with Spring Boot 2.3

我想将我的应用程序从Spring Boot 2.2.8更新到2.3.1
当我在更新后运行应用程序时,我的globalExceptionHandler将无法正常工作。
我的情况是在请求中包含无效的JSON-Body时处理错误。

应用程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.schaerer.coffeelink.remote;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.EnableKafka;

@Slf4j
@SpringBootApplication
@EnableKafka
public class Application {

  public static void main(final String[] args) { //NOSONAR
    SpringApplication.run(Application.class, args);
    log.info("Started application. Swagger available at http://localhost:8080/swagger-ui.html");
  }
}

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.schaerer.coffeelink.remote.boundary.v1;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.schaerer.coffeelink.remote.boundary.v1.dto.*;
import com.schaerer.coffeelink.remote.controller.v1.ActionController;
import com.schaerer.coffeelink.remote.controller.v1.BidiCommandController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@Validated
@RequestMapping(value ="v1/remote", produces = MediaType.APPLICATION_JSON_VALUE)
@Tag(name ="remote-bidi-command")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BidiCommandResource {

  private final BidiCommandController bidiCommandController;
  private final ActionController actionController;
  private final ObjectMapper mapper;

  @ExceptionHandler({IllegalArgumentException.class})
  public ResponseEntity<Object> handleException(final IllegalArgumentException illegalArgumentException) {
    return new ResponseEntity<>(illegalArgumentException.getMessage(), HttpStatus.BAD_REQUEST);
  }

  @PostMapping
  @Operation(summary ="apply bidi command 2.0")
  public BidiCommandReturnDto applyBidiCommand(@Parameter(name ="applyBidiCommand")
                                                         @Validated({ActionDto.CreateValidation.class, BidiCommandDto.CreateValidation.class})
                                           @RequestBody final BidiCommandDto bidiCommandDto) {
        return bidiCommandController.applyBidiCommand(bidiCommandDto);
  }
}

ExceptionHandler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.schaerer.coffeelink.remote.boundary.v1;

import com.fasterxml.jackson.databind.JsonMappingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler(value = {JsonMappingException.class})
  public ResponseEntity<Object> handleJacksonError(final JsonMappingException ex, final WebRequest request) {
    log.error("Cannot parse request. {}", request.getDescription(true), ex);
    return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, null);
  }
}

自更新以来,我没有更改任何代码。响应代码与之前的400错误请求相同,但响应主体为空,并且未调用我的处理程序。

日志:

13:13:53.987 [http-nio-8080-exec-3] TRACE org.hibernate.internal.SessionImpl - Opened Session [6570b352-8067-49d2-bc46-c503917f4c8d] at timestamp: 1595502833972
13:13:53.991 [http-nio-8080-exec-3] TRACE o.s.t.s.TransactionSynchronizationManager - Bound value [org.springframework.orm.jpa.EntityManagerHolder@72dd1712] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@38c55a8a] to thread [http-nio-8080-exec-3]
13:13:54.011 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.ServletInvocableHandlerMethod - Could not resolve parameter [0] in public com.schaerer.coffeelink.remote.boundary.v1.dto.BidiCommandReturnDto com.schaerer.coffeelink.remote.boundary.v1.BidiCommandResource.applyBidiCommand(com.schaerer.coffeelink.remote.boundary.v1.dto.BidiCommandDto): JSON parse error: Unexpected character ('' (code 92)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('' (code 92)): was expecting double-quote to start field name
at [Source: (PushbackInputStream); line: 8, column: 54] (through reference chain: com.schaerer.coffeelink.remote.boundary.v1.dto.BidiCommandDto["parameters"])
13:13:54.012 [http-nio-8080-exec-3] TRACE o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'globalExceptionHandler'
13:13:54.012 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Using @ExceptionHandler com.schaerer.coffeelink.remote.boundary.v1.GlobalExceptionHandler#handleException(Exception, WebRequest)
13:13:54.013 [http-nio-8080-exec-3] TRACE o.s.w.s.m.m.a.ServletInvocableHandlerMethod - Arguments: [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('' (code 92)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('' (code 92)): was expecting double-quote to start field name
at [Source: (PushbackInputStream); line: 8, column: 54] (through reference chain: com.schaerer.coffeelink.remote.boundary.v1.dto.BidiCommandDto["parameters"]), ServletWebRequest: uri=/v1/remote/;client=0:0:0:0:0:0:0:1]
13:13:54.015 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.HttpEntityMethodProcessor - No match for [/], supported: []
13:13:54.016 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('' (code 92)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('' (code 92)): was expecting double-quote to start field name
at [Source: (PushbackInputStream); line: 8, column: 54] (through reference chain: com.schaerer.coffeelink.remote.boundary.v1.dto.BidiCommandDto["parameters"])]
13:13:54.016 [http-nio-8080-exec-3] TRACE o.s.web.servlet.DispatcherServlet - No view rendering, null ModelAndView returned.

有什么想法吗?


当我删除ResponseEntityExceptionHandler扩展时,我已经找到了解决方案。
这是我的解决方案:

1
2
3
4
5
6
7
8
9
10
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

  @ExceptionHandler(value = {HttpMessageNotReadableException.class})
  public ResponseEntity<Object> handleJacksonError(final JsonMappingException ex, final WebRequest request) {
    log.error("Cannot parse request. {}", request.getDescription(true), ex);
    return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
  }
}