resp.setCharacterEncoding("utf-8");resp.setContentType("application/json; charset=utf-8");PrintWriter writer = resp.getWriter();JSONObject o = new JSONObject();o.put("status", "success");writer.write(o.toString());
这点可以与异常处理相配合,当发生异常时,使用上述方法返回给前端指定信息
try {// 判断该请求是不是受保护的请求if(isProtectedUrl(request)) {// Options请求可以不用校验,其他诸如Get、Post等请求都要校验if(!request.getMethod().equals("OPTIONS"))// 校验是否带有有效tokenrequest = JwtUtil.validateTokenAndAddUserIdToHeader(request);}} catch (CustomException e) {response.setCharacterEncoding("utf-8");response.setContentType("application/json; charset=utf-8");PrintWriter writer =response.getWriter();JSONObject o = new JSONObject();o.put("code", e.getId());o.put("message", e.getMessage());writer.write(o.toString());return;}

