ソースを参照

支持 json 请求响应 json 格式数据

James 3 年 前
コミット
62f04a327b

+ 90 - 22
src/main/java/com/jfinal/render/ErrorRender.java

@@ -17,63 +17,131 @@
 package com.jfinal.render;
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.Map;
 import com.jfinal.core.Const;
+import com.jfinal.kit.Okv;
 
 /**
  * ErrorRender.
  */
 public class ErrorRender extends Render {
 	
-	protected static final String contentType = "text/html; charset=" + getEncoding();
+	protected static final String contentTypeHtml = "text/html; charset=" + getEncoding();
+	protected static final String contentTypeJson = "application/json; charset=" + getEncoding();
 	
 	protected static final String version = "<center><a href='https://gitee.com/jfinal/jfinal' target='_blank'><b>Powered by JFinal " + Const.JFINAL_VERSION + "</b></a></center>";
 	
 	protected static final byte[] html404 = ("<html><head><title>404 Not Found</title></head><body bgcolor='white'><center><h1>404 Not Found</h1></center><hr>" + version + "</body></html>").getBytes();
 	protected static final byte[] 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>").getBytes();
-	
 	protected static final byte[] html400 = ("<html><head><title>400 Bad Request</title></head><body bgcolor='white'><center><h1>400 Bad Request</h1></center><hr>" + version + "</body></html>").getBytes();
 	protected static final byte[] html401 = ("<html><head><title>401 Unauthorized</title></head><body bgcolor='white'><center><h1>401 Unauthorized</h1></center><hr>" + version + "</body></html>").getBytes();
 	protected static final byte[] html403 = ("<html><head><title>403 Forbidden</title></head><body bgcolor='white'><center><h1>403 Forbidden</h1></center><hr>" + version + "</body></html>").getBytes();
 	
+	protected static final byte[] json404 = Okv.of("state", "fail").set("msg", "404 Not Found").toJson().getBytes();
+	protected static final byte[] json500 = Okv.of("state", "fail").set("msg", "500 Internal Server Error").toJson().getBytes();
+	protected static final byte[] json400 = Okv.of("state", "fail").set("msg", "400 Bad Request").toJson().getBytes();
+	protected static final byte[] json401 = Okv.of("state", "fail").set("msg", "401 Unauthorized").toJson().getBytes();
+	protected static final byte[] json403 = Okv.of("state", "fail").set("msg", "403 Forbidden").toJson().getBytes();
+	
+	protected static final Map<Integer, byte[]> errorHtmlMap = new HashMap<>();
+	protected static final Map<Integer, byte[]> errorJsonMap = new HashMap<>();
+	
+	protected static final Map<Integer, String> errorViewMap = new HashMap<Integer, String>();
+	
+	static {
+		errorHtmlMap.put(404, html404);
+		errorHtmlMap.put(500, html500);
+		errorHtmlMap.put(400, html400);
+		errorHtmlMap.put(401, html401);
+		errorHtmlMap.put(403, html403);
+		
+		errorJsonMap.put(404, json404);
+		errorJsonMap.put(500, json500);
+		errorJsonMap.put(400, json400);
+		errorJsonMap.put(401, json401);
+		errorJsonMap.put(403, json403);
+	}
+	
 	protected int errorCode;
+	protected String viewOrJson;
 	
-	public ErrorRender(int errorCode, String view) {
+	public ErrorRender(int errorCode, String viewOrJson) {
 		this.errorCode = errorCode;
-		this.view = view;
+		this.viewOrJson = viewOrJson;
+	}
+	
+	public ErrorRender(int errorCode) {
+		this.errorCode = errorCode;
+	}
+	
+	/**
+	 * 设置异常发生时响应的错误页面
+	 */
+	public static void setErrorView(int errorCode, String errorView) {
+		errorViewMap.put(errorCode, errorView);
+	}
+	
+	public static String getErrorView(int errorCode) {
+		return errorViewMap.get(errorCode);
+	}
+	
+	/**
+	 * 设置异常发生时响应的 html 内容
+	 */
+	public static void setErrorHtmlContent(int errorCode, String htmlContent) {
+		try {
+			errorHtmlMap.put(errorCode, htmlContent.getBytes(getEncoding()));
+		} catch (UnsupportedEncodingException e) {
+			throw new RuntimeException(e);
+		}
+	}
+	
+	/**
+	 * 设置异常发生时响应的 json 内容
+	 */
+	public static void setErrorJsonContent(int errorCode, String jsonContent) {
+		try {
+			errorJsonMap.put(errorCode, jsonContent.getBytes(getEncoding()));
+		} catch (UnsupportedEncodingException e) {
+			throw new RuntimeException(e);
+		}
 	}
 	
 	public void render() {
 		response.setStatus(getErrorCode());	// HttpServletResponse.SC_XXX_XXX
 		
-		// render with view
-		String view = getView();
-		if (view != null) {
-			RenderManager.me().getRenderFactory().getRender(view).setContext(request, response).render();
+		String ct = request.getContentType();
+		boolean isJsonContentType = ct != null && ct.indexOf("json") != -1;
+		
+		// render with viewOrJson
+		if (viewOrJson != null) {
+			if (isJsonContentType) {
+				RenderManager.me().getRenderFactory().getJsonRender(viewOrJson).setContext(request, response).render();
+			} else {
+				RenderManager.me().getRenderFactory().getRender(viewOrJson).setContext(request, response).render();
+			}
 			return;
 		}
 		
 		// render with html content
 		try {
-			response.setContentType(contentType);
-			response.getOutputStream().write(getErrorHtml());
+			response.setContentType(isJsonContentType ? contentTypeJson : contentTypeHtml);
+			response.getOutputStream().write(isJsonContentType ? getErrorJson() : getErrorHtml());
 		} catch (IOException e) {
 			throw new RenderException(e);
 		}
 	}
 	
 	public byte[] getErrorHtml() {
-		int errorCode = getErrorCode();
-		if (errorCode == 404)
-			return html404;
-		if (errorCode == 500)
-			return html500;
-		if (errorCode == 400)
-			return html400;
-		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>").getBytes();
+		byte[] ret = errorHtmlMap.get(getErrorCode());
+		return ret != null ? ret : ("<html><head><title>" + errorCode + " Error</title></head><body bgcolor='white'><center><h1>" + errorCode + " Error</h1></center><hr>" + version + "</body></html>").getBytes();
+	}
+	
+	public byte[] getErrorJson() {
+		byte[] ret = errorJsonMap.get(getErrorCode());
+		return ret != null ? ret : Okv.of("state", "fail").set("msg", errorCode + " Error").toJson().getBytes();
 	}
 	
 	public int getErrorCode() {

+ 1 - 1
src/main/java/com/jfinal/render/IRenderFactory.java

@@ -57,7 +57,7 @@ public interface IRenderFactory {
 	
 	public Render getDefaultRender(String view);
 	
-	public Render getErrorRender(int errorCode, String view);
+	public Render getErrorRender(int errorCode, String viewOrJson);
 	
 	public Render getErrorRender(int errorCode);
 	

+ 6 - 3
src/main/java/com/jfinal/render/RenderFactory.java

@@ -112,12 +112,15 @@ public class RenderFactory implements IRenderFactory {
 		return getRender(view + constants.getViewExtension());
 	}
 	
-	public Render getErrorRender(int errorCode, String view) {
-		return new ErrorRender(errorCode, view);
+	public Render getErrorRender(int errorCode, String viewOrJson) {
+		return new ErrorRender(errorCode, viewOrJson);
 	}
 	
 	public Render getErrorRender(int errorCode) {
-		return new ErrorRender(errorCode, constants.getErrorView(errorCode));
+		// 支持返回 json 数据之后,需要在 ErrorRender.render() 方法内判断 contentType 后才能确定返回 json 还是 html 页面
+		// 而在此处 ErrorRender.getErrorView(errorCode) 是无法知道 contentType 信息的,故去掉 errorView 参数
+		// return new ErrorRender(errorCode, ErrorRender.getErrorView(errorCode));
+		return new ErrorRender(errorCode);
 	}
 	
 	public Render getFileRender(String fileName) {