HttpMediaTypeNotAcceptException with text/plain message responses?
我有一个简单的Web服务,它将内容返回为或作为(取决于客户端的http标头)。json
plain text
accept
问题:如果在请求期间发生错误,Spring会以某种方式返回.这有点错误,因为spring也可以将错误写成纯错误文本,而且绝对应该保留错误状态:text/plain
406 Not Acceptable
400
@RestController
public class TestServlet {
@PostMapping(value = "/test", produces = {APPLICATION_JSON_VALUE, TEXT_PLAIN_VALUE, "text/csv"})
public Object post() {
throw new BadRequestException("bad req");
}
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException {
public BadRequestException(String msg) {
super(msg);
}
}
带有 :accept=application/json
{
"timestamp": "2018-07-30T14:26:02",
"status": 400,
"error": "Bad Request",
"message": "bad req",
"path": "/test"
}
但带有(或)显示带有状态的空响应。accept=text/csv
text/plain
406 Not Acceptable
我还注意到被调用了两次:第一次是我的例外,第二次是.很明显,我的自定义异常的呈现失败了,但为什么呢?DispatcherServlet.processDispatchResult()
BadRequest
HttpMediaTypeNotAcceptableException