Java &引用;找不到可接受的陈述”;抛出自定义异常时
正在使用spring启动版本1.3.3。发布 我有这样一个资源方法:Java &引用;找不到可接受的陈述”;抛出自定义异常时,java,spring-boot,Java,Spring Boot,正在使用spring启动版本1.3.3。发布 我有这样一个资源方法: @RequestMapping(value="/{id}", method=RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public @ResponseBody ResponseEntity<byte[]> getImage(@NotNull @PathVariable("id") String imageId) {
@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("...");
}
}
@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\u OCTET\u STREAM\u VALUE
。每当代码出现异常时,我的日志中都会出现这个异常(客户端实际上收到的是500而不是404):
在这种情况下,有没有办法使用自定义异常?我想我可以使用ResponseEntity
构建器,但最好保持一致
提前谢谢
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]
...