gpt4 book ai didi

java - Spring Boot 1.4.3 和 @ControllerAdvice

转载 作者:行者123 更新时间:2023-12-02 12:05:13 26 4
gpt4 key购买 nike

我有一个带有 ControllerAdvice 的 spring 4 应用程序,该应用程序扩展了 ResponseEntityExceptionHandler,当异常出现时它可以正常工作。

@ControllerAdvice
@Slf4j
public class ExceptionManager extends ResponseEntityExceptionHandler {

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request) {
final ValidationErrors errors = getErrors(ex.getBindingResult());
headers.setContentType(MediaType.APPLICATION_JSON);
return handleExceptionInternal(ex, errors, headers, HttpStatus.BAD_REQUEST, request);
}


@Override
protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
final ValidationErrors errors = getErrors(ex.getBindingResult());
headers.setContentType(MediaType.APPLICATION_JSON);
return handleExceptionInternal(ex, errors, headers, HttpStatus.BAD_REQUEST, request);
}

@ExceptionHandler(ValidationException.class)
public ResponseEntity handleValidationException(ValidationException ex) {
log.warn("Validation error", ex);
return ex.toResponseEntity();
}

@ExceptionHandler(HttpClientErrorException.class)
public ResponseEntity handleHttpClientErrorException(HttpClientErrorException ex) {
log.error(ex.getMessage());
return new ResponseEntity(ex.getResponseBodyAsString(), ex.getStatusCode());
}

@ExceptionHandler({Exception.class, RuntimeException.class})
protected ResponseEntity<Object> handleRootException(Exception ex, WebRequest request) {
return handleException(request, ex);
}

...
}

但是,在解码器异常的情况下,例如

Exception received: 
org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to [class com.company.etc.v1.PojoResponse]: null; nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 28; Attribute name "entResponse" associated with an element type "assetDocum" must be followed by the ' = ' character.]
at org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.readFromSource(Jaxb2RootElementHttpMessageConverter.java:149)
at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.readInternal(AbstractXmlHttpMessageConverter.java:61)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:193)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:104)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:917)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:901)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
at com.company.service.impl.SearchAsset.searchAssets(SearchAsset.java:42)
... 57 common frames omitted
Caused by: javax.xml.bind.UnmarshalException: null
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:123)
at org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.readFromSource(Jaxb2RootElementHttpMessageConverter.java:133)
... 127 common frames omitted
Caused by: org.xml.sax.SAXParseException: Attribute name "entResponse" associated with an element type "assetDocum" must be followed by the ' = ' character.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
... 131 common frames omitted

异常没有被捕获!当 @RestController 使用调用外部端点的服务时,它会发生在特定情况下。在我的例子中,外部服务返回一个 XML,如果它被破坏(如上面的异常),那么 SAXParseException 不会被捕获并自动转换为带有空正文的 400 Bad Request Spring 。它也根本没有被记录。

如果在我的 RestController 中添加一个方法 @ExceptionHandler(Throwable.class),则会调用该方法,并且我可以控制发回的错误。

这正常吗?我认为 ControllerAdvice 可以集中所有异常处理,从而无需在每个 Controller 中都有 ExceptionHandler。

最佳答案

刚刚找到解决方案:删除 extends ResponseEntityExceptionHandler。如果您 @ControllerAdvice 不扩展任何内容,看起来效果会更好。

关于java - Spring Boot 1.4.3 和 @ControllerAdvice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46949275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com