gpt4 book ai didi

java - @ControllerAdvice 不返回 json 响应

转载 作者:行者123 更新时间:2023-12-01 07:19:49 24 4
gpt4 key购买 nike

每当我的 Controller 抛出异常时,它都不会以 json 格式返回响应。

它给出了Whitelabel错误页面

This application has no explicit mapping for /error, so you are seeing this as a fallback.

我的 Controller 是:

@RequestMapping(value = GENERATE_IMAGE, method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public byte[] get(@PathVariable("input") String input) throws IOException {
if (true) {
throw new BusinessValidationFailureException("sdfsdfsd");
}
ByteArrayInputStream inputStream = service.generate(input);
return ByteStreams.toByteArray(inputStream);
}

我的 Controller 建议是:

@ControllerAdvice
public class WickesGlobalExceptionMapper extends ResponseEntityExceptionHandler {

@ExceptionHandler(BusinessValidationFailureException.class)
@ResponseBody
public ResponseEntity handleBusinessException(BusinessValidationFailureException ex, WebRequest request) {

ErrorResource error = new ErrorResource("InvalidRequest", ex.getMessage(), null);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return handleExceptionInternal(ex, error, headers, HttpStatus.UNPROCESSABLE_ENTITY, request);
}
}

我尝试了一些其他的可能性,但我一直得到相同的错误页面。

我在服务器中遇到以下异常:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:259) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:132) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:384) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:59) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:74) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]

最佳答案

您没有显示handleExceptionInternal方法实现,这确实返回一个对象吗?

编辑:我对 @RestControllerAdvice 的看法是错误的,已将其从答案中删除。

假设您想要返回一个 ErrorData 类型的对象(这在您的帖子中并不清楚)。所以你可以使用:

@ExceptionHandler(BusinessValidationFailureException.class)
@ResponseBody
public ResponseEntity<ErrorData> handleBusinessException(BusinessValidationFailureException e) {
ErrorData errorData = createYourErrorData();
return new ResponseEntity(errorData, HttpStatus.UNPROCESSABLE_ENTITY);
}

我刚刚尝试了此操作,并获得了正确的状态代码以及 JSON 中的数据对象。

关于java - @ControllerAdvice 不返回 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42983228/

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