我想在方法中选择运行时的响应媒体类型.
例如,以下代码:
@RequestMapping(value = "/getRecord",
produces = {"application/octet-stream", "application/json;charset=UTF-8" })
public byte[] getData(
@RequestParam(value="id", required=true) Integer id)
throws IOException
{
if (id == 1)
return createByteArray();
throw new MyDataException();
}
在此代码中,可能的响应类型实际上是2.
> byte [](按正常执行路径)
> MyDataException(由异常执行路径)
MyDataException稍后由异常处理程序处理,并转换为简单类.它可以转换为json响应.
首先,我认为如果我为@RequestMapping注释的产生选项提供2种响应类型,则消息转换器将根据实际返回对象转换2种类型.但事实并非如此.
在spring类org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor中,如果存在produce选项,则writeWithMessageConverters()方法在选择响应类型时忽略实际的返回对象类型.
如何让Spring根据实际的返回对象在运行时选择响应类型?
最佳答案 另一种可能性是两种所述解决方案之间的混合方法:
@ControllerAdvice
public class MyExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({ MyDataException.class })
protected ResponseEntity<Object> handleInvalidRequest(RuntimeException e, WebRequest request) {
MyDataExceptionire = (MyDataException) e;
List<FieldErrorResource> fieldErrorResources = new ArrayList<>();
List<FieldError> fieldErrors = ire.getErrors().getFieldErrors();
for (FieldError fieldError : fieldErrors) {
FieldErrorResource fieldErrorResource = new FieldErrorResource();
fieldErrorResource.setResource(fieldError.getObjectName());
fieldErrorResource.setField(fieldError.getField());
fieldErrorResource.setCode(fieldError.getCode());
fieldErrorResource.setMessage(fieldError.getDefaultMessage());
fieldErrorResources.add(fieldErrorResource);
}
ErrorResource error = new ErrorResource("MyDataException", ire.getMessage());
error.setFieldErrors(fieldErrorResources);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return handleExceptionInternal(e, error, headers, HttpStatus.UNPROCESSABLE_ENTITY, request);
}}
解决方案于this blog提出
编辑:
我还从博客中添加了FieldError和ErrorResource类,因为它将来可能会被删除:
ErrorResource:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ErrorResource {
private String code;
private String message;
private List<FieldErrorResource> fieldErrors;
public ErrorResource() { }
public ErrorResource(String code, String message) {
this.code = code;
this.message = message;
}
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public List<FieldErrorResource> getFieldErrors() { return fieldErrors; }
public void setFieldErrors(List<FieldErrorResource> fieldErrors) {
this.fieldErrors = fieldErrors;
}
}
FieldErrorResource:
@JsonIgnoreProperties(ignoreUnknown = true)
public class FieldErrorResource {
private String resource;
private String field;
private String code;
private String message;
public String getResource() { return resource; }
public void setResource(String resource) { this.resource = resource; }
public String getField() { return field; }
public void setField(String field) { this.field = field; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }}