抛出自定义异常时“找不到可接受的表示”

时间:2017-01-11 15:32:28

标签: java spring-boot

使用spring boot版本1.3.3.RELEASE ...

我有这样的资源方法:

@RequestMapping(value="/{id}", method=RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody ResponseEntity<byte[]> getImage(@NotNull @PathVariable("id") String imageId) {
    // get image from database and return byte[]
    if(image == null) {
        throw new ResourceNotFoundException("...");
    }
}

ResourceNotFoundException是这样一个简单的类:

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    public ResourceNotFoundException() {
    }

    public ResourceNotFoundException(String message) {
        super(message);
    }

    public ResourceNotFoundException(String message, Throwable cause) {
        super(message, cause);
    }
}

这种方法适用于所有其他资源,但我认为它与所需的byte[]响应机构或它正在生成APPLICATION_OCTET_STREAM_VALUE的事实相混淆。每当代码到达异常时,我在我的日志中都有这个(并且客户端实际上接收的是500而不是404):

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:251) ~[spring-webmvc-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
 ...

在这种情况下,我有什么办法可以使用自定义异常吗?我想我可以使用ResponseEntity构建器,但保持一致会很好。

提前致谢。

0 个答案:

没有答案
相关问题