@ControllerAdvice没有返回json响应

时间:2017-03-23 17:39:59

标签: java spring spring-boot

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

正在提供Whitelabel错误页面

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

我的控制器是:

@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);
}

我的控制器建议是:

@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]

2 个答案:

答案 0 :(得分:2)

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

修改:我对@RestControllerAdvice错了,将其从答案中移除。

假设您要返回ErrorData类型的对象(这在您的帖子中不明确)。所以你可以使用:

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

我只是尝试了这个,并将正确的stataus代码与JSON中的数据对象一起返回。

答案 1 :(得分:1)

由于控制器生成MediaType.IMAGE_PNG_VALUE,因此controlleradvice也应生成相同的mediatype。我从requestmapping中删除了产品,并在响应中设置了内容类型,并且它有效。

@RequestMapping(value = GENERATE_BARCODE, method = RequestMethod.GET)
public ResponseEntity get(@PathVariable("input") String barcodeInput) throws IOException {
    byte[] resonse = service.generate(barcodeInput);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_PNG);
    return new ResponseEntity(resonse, headers, HttpStatus.OK);
}