做服务端接口开发时会碰到,使用ajax请求接口,
出错时jfinal不会以json格式响应出错信息的情况。
使用以下方式解决:
1. 实现自定义ErrorRender类
public class ErrorRender extends Render {
/**
* 描述:contentType
*/
protected static String contentType = "text/html; charset=" + getEncoding();
/**
* 描述:
*/
protected static String jsonContentType = "application/json;charset=" + getEncoding();
/**
* 描述:
*/
protected static String json400 = "400 Bad Request";
/**
* 描述:
*/
protected static String json404 = "404 Not Found";
/**
* 描述:
*/
protected static String json500 = "500 Internal Server Error";
/**
* 描述:
*/
protected static String json401 = "401 Unauthorized";
/**
* 描述:
*/
protected static String json403 = "403 Forbidden";
/**
* 描述:version
*/
protected static String version = "<center><b>Powered by Evecom WEB </b></center>";
/**
* 描述:html400
*/
protected static String html400 = "<html><head><title>400 Bad Request</title></head>"
+ "<body bgcolor='white'><center><h1>400 Bad Request</h1></center><hr>"
+ version + "</body></html>";
/**
* 描述:html404
*/
protected static String html404 = "<html><head><title>404 Not Found</title></head>"
+ "<body bgcolor='white'><center><h1>404 Not Found</h1></center><hr>"
+ version + "</body></html>";
/**
* 描述:html500
*/
protected static String html500 = "<html><head><title>500 Internal Server Error</title></head>"
+ "<body bgcolor='white'><center><h1>500 Internal Server Error</h1></center><hr>"
+ version + "</body></html>";
/**
* 描述:html401
*/
protected static String html401 = "<html><head><title>401 Unauthorized</title></head>"
+ "<body bgcolor='white'><center><h1>401 Unauthorized</h1></center><hr>"+ version + "</body></html>";
/**
* 描述:html403
*/
protected static String html403 = "<html><head><title>403 Forbidden</title></head>"
+ "<body bgcolor='white'><center><h1>403 Forbidden</h1></center><hr>"
+ version + "</body></html>";
/**
* 描述:errorCode
*/
protected int errorCode;
public ErrorRender(int errorCode, String view) {
this.errorCode = errorCode;
this.view = view;
}
/**
* 描述:
* @author Robin Zhang
* @created 2016-5-18 下午3:28:28
* @see com.jfinal.render.Render#render()
*/
public void render() {
response.setStatus(getErrorCode()); // HttpServletResponse.SC_XXX_XXX
// render with view
String view = getView();
String xRequestedWith = request.getHeader("X-Requested-With");
String accept = request.getHeader("Accept")+"".toLowerCase();//application/json
boolean isAjax = ("XMLHttpRequest").equalsIgnoreCase(xRequestedWith);
PrintWriter writer = null;
try {
if (isAjax || accept.contains("application/json")) {
String errorJson = MessageFormat.format("'{'\"success\":false,\"code\":{0},\"msg\":\"{1}\"'}'",
getErrorCode(), getErrorJson());
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType(contentType);
writer = response.getWriter();
writer.write(errorJson);
writer.flush();
return;
} else
if (view != null) {
RenderManager.me().getRenderFactory().getRender(view).setContext(request, response).render();
return;
} else {
response.setContentType(contentType);
writer = response.getWriter();
writer.write(getErrorHtml());
writer.flush();
return;
}
} catch (IOException e) {
throw new RenderException(e);
}
}
/**
* 描述:
* @author Robin Zhang
* @created 2016-5-18 下午3:28:24
* @return
*/
public String getErrorHtml() {
int errorCode = getErrorCode();
if (errorCode == 400)
return html400;
if (errorCode == 404)
return html404;
if (errorCode == 500)
return html500;
if (errorCode == 401)
return html401;
if (errorCode == 403)
return html403;
return "<html><head><title>" + errorCode + " Error</title></head><body bgcolor='white'><center><h1>"
+ errorCode + " Error</h1></center><hr>" + version + "</body></html>";
}
/**
* 描述:
* @author Robin Zhang
* @created 2016-5-18 下午3:28:16
* @return
*/
public String getErrorJson() {
int errorCode = getErrorCode();
if (errorCode == 400)
return json400;
if (errorCode == 404)
return json404;
if (errorCode == 500)
return json500;
if (errorCode == 401)
return json401;
if (errorCode == 403)
return json403;
return errorCode + " Error";
}
/**
* 描述:
* @author Robin Zhang
* @created 2016-5-18 下午3:28:19
* @return
*/
public int getErrorCode() {
return errorCode;
}
}
2.继承RenderFactory类,重写getErrorRender的两个方法。
public class BaseRenderFactory extends RenderFactory {
public Render getErrorRender(int errorCode, String view) {
return new ErrorRender(errorCode, view);
}
public Render getErrorRender(int errorCode) {
return new ErrorRender(errorCode, constants.getErrorView(errorCode));
}
}
3.在config.java中设置c.setRenderFactory(new BaseRenderFactory());