SpringMVC拦截器返回值不能自定义的解决办法
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestContent = getRequestBody(request);
("requestContent:{}", requestContent);
LoginParam param = (requestContent, );
("param:{}", param);
if ((())) {
returnErrorMessage(response, "请输入验证码");
return false;
}
//验证码是否验证通过
...
boolean validPass = isValidUser(request, param);
if (validPass) {
return true;
}
returnErrorMessage(response, "用户名或密码错误,请重新尝试。");
("Error");
return false;
}
private void returnErrorMessage(HttpServletResponse response, String errorMessage) throws IOException {
Result rst = new Result();
("0");
(errorMessage);
("application/json");
//Get the printwriter object from response to write the required json object to the output stream
PrintWriter out = ();
//Assuming your json object is **jsonObject**, perform the following, it will return your json object
ObjectMapper mapper = new ObjectMapper();
String jsonOfRST = "{\"result\":" + (rst) + ",\"data\":null}";
(jsonOfRST);
();
}