2019独角兽企业重金招聘Python工程师标准>>>
项目使用的json解析框架是fastjson
自定义的返回对象如下:
package com.test;public class ResultObj {private boolean result;private int code=200;private String msg;private Object data;private PagerResult pager;private String authCode;private String imgServer;public boolean isResult() {return result;}public void setResult(boolean result) {this.result = result;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public PagerResult getPager() {return pager;}public void setPager(PagerResult pager) {this.pager = pager;}public String getAuthCode() {return authCode;}public void setAuthCode(String authCode) {this.authCode = authCode;}public String getImgServer() {return imgServer;}public void setImgServer(String imgServer) {this.imgServer = imgServer;}/*** 自定义错误信息返回* @param code* @param msg* @return*/public static ResultObj errorWithInfo(int code,String msg){return errorWithInfo(code, msg, null);}/*** 自定义错误信息返回* @param code* @param msg* @return*/public static ResultObj errorWithInfo(int code,String msg,Object data){ResultObj resultObj=new ResultObj();resultObj.setResult(false);resultObj.setCode(code);resultObj.setMsg(msg);resultObj.setData(data);return resultObj;} }
package com.test;public class PagerResult {/**每页显示*/private int pageSize = 20;/**页码*/private int pageNo = 1;/**开始数*/private int start = 0;/**总条数*/private int totalRows = 0;/**总页码*/private int pageCount = 0;public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getPageNo() {return pageNo;}public void setPageNo(int pageNo) {this.pageNo = pageNo;}public int getStart() {return start;}public void setStart(int start) {this.start = start;}public int getTotalRows() {return totalRows;}public void setTotalRows(int totalRows) {this.totalRows = totalRows;}public int getPageCount() {return pageCount;}public void setPageCount(int pageCount) {this.pageCount = pageCount;}/*** 自定义错误信息返回* @param code* @param msg* @return*/public static ResultObj errorWithInfo(int code,String msg){return errorWithInfo(code, msg, null);} }
web.xml配置错误返回页面
<error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>405</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page>
error.jsp的内容
<%@ page language="java" contentType="text/json; charset=UTF-8"pageEncoding="UTF-8"%><%@page import=" com.alibaba.fastjson.JSONObject"%><%@page import="com.alibaba.fastjson.serializer.SerializerFeature"%> <%@page import="com.test.ResultObj"%> <%@page import="com.test.TipUtil"%>
<%response.setHeader("Access-Control-Allow-Origin","*");
%>
<%out.println(JSONObject.toJSONString(ResultObj.errorWithInfo(TipUtil.ERROR_CODE, "请求有误,请重试!"),SerializerFeature.WriteMapNullValue));
%>