从MissingServletRequestParameterException的响应中删除双引号

时间:2020-03-11 14:03:00

标签: java json spring http exception

这种情况是我需要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>

0 个答案:

没有答案
相关问题