文章目錄
- @RestController自動回傳json
- @ResponseBody+@Controller 組合回傳json
- 在pom.xml 添加 阿里巴巴json jar包
- 封裝json在entiy的body回傳msg
- JsonResponseServlet
- controller
@RestController自動回傳json

/**
* json 三種實作方法
* 1 @RestController自動回傳json
*/
@GetMapping("/json")
public Student getjson() {
Student student = new Student("bennyrhys",158
);
return student;
}
@ResponseBody+@Controller 組合回傳json

//@RestController
@Controller
// 類名上方
@GetMapping("/json")
@ResponseBody
public Student getjson() {
Student student = new Student("bennyrhys",158
);
return student;
}
在pom.xml 添加 阿里巴巴json jar包

//@RestController
@Controller
// 類名上方
@GetMapping("/fastjson")
public void fastjson(HttpServletResponse response){
Student student = new Student("bennyrhys",158);
String data = JSON.toJSONString(student);
try {
sendJsonData(response, data);
} catch (IOException e) {
e.printStackTrace();
}
}
protected void sendJsonData(HttpServletResponse response, String data) throws IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(data);
out.flush();
out.close();
}
fastjson深入理解
fastJson對于json格式字串的決議主要用到了一下三個類:
JSON:fastJson的決議器,用于JSON格式字串與JSON物件及javaBean之間的轉換,
JSONObject:fastJson提供的json物件,
JSONArray:fastJson提供json陣列物件,
toJSONString() 和 parseObject() 方法來將 Java 物件與 JSON 相互轉換,呼叫toJSONString方 法即可將物件轉換成 JSON 字串,parseObject 方法則反過來將 JSON 字串轉換成物件,
允許轉換預先存在的無法修改的物件(只有class、無源代碼),
Java泛型的廣泛支持,
允許物件的自定義表示、允許自定義序列化類,
支持任意復雜物件(具有深厚的繼承層次和廣泛使用的泛型型別),
JSONObject 當成一個 Map<String,Object> 來看
JSONArray當成一個 List 來看
JSON.toString(Object)----> return String
JSON.parse(String)----->return Object
String 和 JsonObject 和 JsonArray之間轉化
https://www.cnblogs.com/ljangle/p/11047111.html
1、String轉JSONObject 或 JSONArray
JSONObject jSONObject = JSONObject.parseObject(String);
JSONArray jsonArray= JSONArray.parseArray(String);
2、JSONObject中的陣列提取為JSONArray
提取Result對應的陣列
JSONArray jsonArray= jsonObject.getJSONArray(“Result”);
3、JSONArray提取為JSONObject
JSONObject jsonObject = jsonArray.getJSONObject(0);
4、JSONObject獲取value
1、object.getString("key")
2、object.get("key")
JSONArray jsonArray= jsonObject.getJSONArray(“Result”);
JSONObject jsonObject = jsonArray.getJSONObject(0);
封裝json在entiy的body回傳msg
ResponseEntity可以定義回傳的HttpStatus(狀態碼)和HttpHeaders(訊息頭:請求頭和回應頭)
HttpStatus(狀態碼)https://blog.csdn.net/csdn1844295154/article/details/78980174
HttpHeaders(訊息頭:請求頭和回應頭)https://www.cnblogs.com/honghong87/articles/6941436.html
具體查看這些內容的用法可以參考https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Disposition
ResponseEntity回傳body
JsonResponseServlet
import java.io.Serializable;
public class JsonResponse<T> implements Serializable {
private Integer code;
private String msg;
private T result;
public JsonResponse() {
}
public JsonResponse(Integer code, String msg, T result) {
this.code = code;
this.msg = msg;
this.result = result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
public static class JsonResponseUtil {
public static JsonResponse<Object> ok() {
return ok(null);
}
public static <T> JsonResponse<T> ok(T result) {
return new JsonResponse<>(0, "", result);
}
public static JsonResponse<Object> error(Integer code) {
return error(code, "error!");
}
public static JsonResponse<Object> error(String msg) {
return error(-1, msg);
}
public static JsonResponse<Object> error(Integer code, String msg) {
return new JsonResponse<>(code, msg, null);
}
}
}
controller
return ResponseEntity.ok(JsonResponse.JsonResponseUtil.ok(source));
CSDN認證博客專家
分布式
Java
架構
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/213366.html
標籤:其他
下一篇:停車場系統
