这种情况是我需要MissingServletRequestParameterException的响应为纯文本,而在其他情况下,所有内容都应以json返回。
这就是我在控制器类中所拥有的:
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public ResponseEntity<String> handleMissingParams(MissingServletRequestParameterException ex) {
String error = "<html><head><title>Error</title></head><body>"+ ex.getMessage() +"</body></html>";
// return ResponseEntity.badRequest()
// .header(MediaType.TEXT_PLAIN_VALUE)
// .body(error);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity(error, httpHeaders, HttpStatus.BAD_REQUEST);
}
@RequestMapping(method = RequestMethod.GET, path = "/reconciliation/api/getLoginByIntermediaAgent")
public ResponseEntity getLoginsByIA(@RequestParam("ia") String ia) {
return aaaApiStub.getLoginsByIA(ia);
}
在评论部分,您也可以看到我尝试过的内容。
现在返回的内容:
"<html><head><title>Error</title></head><body>Required String parameter 'ia' is not present</body></html>"
它应该返回什么:
<html><head><title>Error</title></head><body>Required String parameter 'ia' is not present</body></html>