@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博主「瑞 新」的原創文章,依據CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/weixin_43469680/article/details/109615161