Ajax跨域呼叫後臺Restful介面時的JSON轉換方法
阿新 • • 發佈:2018-12-31
類:AjaxExchange
作用:將後臺取到的資料以JSON或HTML的形式返回
package org.sun.com; import java.io.OutputStreamWriter; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; public class AjaxExchange { // 轉換成HTML形式返回,引數為String型別 public static void printData(HttpServletResponse response, String msg) { try { // 跨域配置 response.setHeader("Access-Control-Allow-Origin","*"); response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Max-Age", "3600"); // 保持跨域Ajax時的Cookie response.setHeader("Access-Control-Allow-Headers", "x-auth-token, x-requested-with,Authorization,Origin, Accept, Content-Type,x-xsrf-token"); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")); out.println(msg); out.close(); } catch (Exception e) { e.printStackTrace(); } } // 將任意型別的item轉換成JSON並返回 public static void printDataJason(HttpServletResponse response, Object item) { try { // 跨域配置 response.setHeader("Access-Control-Allow-Origin","*"); response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Max-Age", "3600"); // 保持跨域Ajax時的Cookie response.setHeader("Access-Control-Allow-Headers", "x-auth-token, x-requested-with,Authorization,Origin, Accept, Content-Type,x-xsrf-token"); response.setContentType("text/json;charset=utf-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")); // currentUser裡面有迴圈導致json處理不了 JsonConfig jsonConfig = new JsonConfig(); //建立配置檔案 jsonConfig.setIgnoreDefaultExcludes(false); //設定預設忽略 jsonConfig.setExcludes(new String[]{"currentUser"}); JSONArray jsonArr = JSONArray.fromObject(item, jsonConfig); out.println(jsonArr); out.close(); } catch (Exception e) { e.printStackTrace(); } } // 帶JsonConfig的JSON轉換 public static void printDataJason(HttpServletResponse response, Object item, JsonConfig jsonConfig) { try { // 跨域配置 response.setHeader("Access-Control-Allow-Origin","*"); response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Max-Age", "3600"); // 保持跨域Ajax時的Cookie response.setHeader("Access-Control-Allow-Headers", "x-auth-token, x-requested-with,Authorization,Origin, Accept, Content-Type,x-xsrf-token"); response.setContentType("text/json;charset=utf-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")); JSONArray jsonArr = JSONArray.fromObject(item, jsonConfig); out.println(jsonArr); out.close(); } catch (Exception e) { e.printStackTrace(); } } }
這裡第二個方法和第三個方法很像,區別在於第三個方法多了個JsonConfig的引數,這是用來設定Json轉換時的一些引數的。
我在返回資料轉Json的時候到這樣的問題:
我想返回的引數是型別A,但是型別A繼承了型別B,型別B裡面有些資料是型別C,型別C裡面有些型別是型別D
結果變成了這樣A→B→C→D→...
JSONArray轉換的時候,會根據這個順序迴圈,把型別ABCD...裡面的資料都JSON化。
如果A→B→C→D→C時,C和D產生迴圈的話,JSON就無法轉換了。
我找到了發生型別迴圈出錯的引數並在方法2中寫死了。也可以在外面設定出錯的引數並傳到方法3中。 呼叫方法如下:// currentUser裡面有迴圈導致json處理不了 JsonConfig jsonConfig = new JsonConfig(); //建立配置檔案 jsonConfig.setIgnoreDefaultExcludes(false); //設定預設忽略 jsonConfig.setExcludes(new String[]{"currentUser"});
@RequestMapping(value = "/restful/getQuestion") public void getQuestion(HttpServletRequest request, HttpServletResponse response) { RestfulResult restfulResult = new RestfulResult(); try{ // 將資料取出放入restfulResult ... restfulResult.setData(...); } catch(Exception ex) { restfulResult.setResult("Error"); restfulResult.setMessage(ex.getMessage()); } AjaxExchange.printDataJason(response, restfulResult); }
追加:
也可以在Entity定義的時候,加上@jsonignore
json會在轉換的時候自動忽略此欄位