Swagger 巢狀物件、巢狀json資料返回的寫法
但是生成的文件並不能滿足我們日常專案中的需求,舉個例子:
我們在專案中需要返回這種資料
{ "code": 200, "msg": "string", "result": { "currentPage": 0, "list": [ { "address": "string", "createTime": "2018-09-21T11:38:15.781Z", "description": "string", "id": 0, "name": "string" } ], "pageSize": 0, "totalCount": 0, "totalPage": 0 }, "version": "string" }
從上面的資料可以分解為三個物件
首先是外層的通用返回物件
package com.ktnw.management.util; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; @ApiModel("api介面通用返回物件") public class ResultUtil implements Serializable{ @ApiModelProperty(value = "返回碼",dataType = "int") private int code; @ApiModelProperty(value = "錯誤資訊",dataType = "String") private String msg; @ApiModelProperty(value = "api版本",dataType = "String") private String version; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } }
第二層就是分頁物件(這裡我需要用到java中的泛型)
package com.ktnw.management.util; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; import java.util.List; @ApiModel("分頁物件") public class PageUtil<T> implements Serializable { @ApiModelProperty(value = "頁碼",dataType = "int") private int currentPage; @ApiModelProperty(value = "頁面記錄數",dataType = "int") private int pageSize; @ApiModelProperty(value = "總頁數",dataType = "int") private int totalPage; @ApiModelProperty(value = "總記錄數",dataType = "long") private long totalCount; @ApiModelProperty(value = "分頁記錄",dataType = "List") private List<T> list; public PageUtil() { super(); } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public long getTotalCount() { return totalCount; } public void setTotalCount(long totalCount) { this.totalCount = totalCount; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }
第三層就是我們需要使用到的引數物件
package com.ktnw.management.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.Date;
@ApiModel("demo物件資訊")
public class Demo implements Serializable {
@ApiModelProperty(value = "demo id",dataType = "int")
private int id;
@ApiModelProperty(value = "名稱",dataType = "String")
private String name;
@ApiModelProperty(value = "描述",dataType = "String")
private String description;
@ApiModelProperty(value = "地址",dataType = "String")
private String address;
@ApiModelProperty(value = "建立時間",dataType = "Date")
private Date createTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
光是這樣這樣是不能返回上面的json資料
還需要宣告一個專門的返回類(這裡需要用到java的繼承)
package com.ktnw.management.models;
import com.ktnw.management.entity.Demo;
import com.ktnw.management.util.PageUtil;
import com.ktnw.management.util.ResultUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "demo分頁資訊",parent = ResultUtil.class)
public class DemoPageResult extends ResultUtil {
@ApiModelProperty(value = "資料列表",dataType = "List")
private PageUtil<Demo> result;
public PageUtil<Demo> getResult() {
return result;
}
public void setResult(PageUtil<Demo> result) {
this.result = result;
}
}
請求處理完成之後,我們直接返回 DemoPageResult 就行了
相關推薦
Swagger 巢狀物件、巢狀json資料返回的寫法
但是生成的文件並不能滿足我們日常專案中的需求,舉個例子: 我們在專案中需要返回這種資料 { "code": 200, "msg": "string", "result": { "currentPage": 0, "list": [
做一次面向物件的體操:將JSON字串轉換為巢狀物件的一種方法
能不能把這個JSON串轉成相應的物件,更易於使用呢? 為了方便講解,這裡重複寫下JSON串。 { “item:s_id:18006666”: “1024”, “item:s_id:18008888”: “1024”, “item:g_id:1800
用proxy寫一個遞迴來監聽巢狀物件甚至JSON
其實那個監聽方式有個大bug,當不停的update 監聽物件以後就會導致記憶體洩漏。因為每次在呼叫get的時候都會生成一個新的proxy,後來才知道是多傻。 經過我一天時間的思考,用了一個遞迴的方法去解決巢狀物件的監聽問題。 下面是程式碼: //傳
@NoNull、@NotEmpty等註解無效,以及巢狀物件屬性驗證無效問題
@Validated註解: 在進行請求引數的驗證時,需要在controller方法的需要驗證的引數前面加上該註解,否則Form中的驗證註解不起作用。 但是如果是內層物件需要驗證的話,需要在裡面物件前也加上@valid,這樣,無論巢狀多少,都可以驗證(包括物件泛型)。
SparkSql處理巢狀json資料
一、資料準備: { "dc_id": "dc-101", "source": { "sensor-igauge": { "id": 10, "ip": "68.28.91.22", "description": "Sensor attached to the
@Validated註解無效,以及巢狀物件屬性的@NotBlank無效問題
最近在做的公司專案是一個表單系統,剛好要用到@Validated註解來對錶單欄位進行驗證。第一次使用遇到了一些坑,問了同事之後,總算是掌握了。 首先,在dto實體你想要做驗證的屬性前,加上@NotBlank/NotNull/NotEmpty註解: 他們的區別如下: @NotBlank:只用在Str
Java解析post請求兩層巢狀json資料
案例: 請求資料: 程式碼段: @RestController @RequestMapping("test") public class testDemo { @PostMapping("demo") public void test(
PLSQL 解析巢狀json資料
1:解析如下json格式資料,巢狀部分:TEST1,TEST2 { "Order": [{"TEST1":{"TEST2":{ "orderType": "LSCK", "amount": "376.00", "orderId": "18090501706539",
Gson教程二(譯):巢狀物件的序列化和反序列化
該文章翻譯自Gson Tutorial Series系列教程。該篇主要闡述瞭如何使用Gson對映巢狀類。 巢狀物件的序列化 我們希望通過實際的例子來闡述功能,因此讓我們來擴充套件我們的UserSimple模型。在之前的釋出中,user模型僅僅有一
用fastjson解析巢狀json資料
json資料類似: { "code": 0, "data": { "city": { "cityId": 284609, "counname": "中國", "name": "東城區", "pname": "
提取多層巢狀Json資料
在.net 2.0中提取這樣的json {"name":"lily","age":23,"addr":{"city":guangzhou,"province":guangdong}} 引用名稱空間 using Newtonsoft.Json; using Newtonsoft
BootStrapTable(一) -- 怎麼解析巢狀Json資料
bootStrapTable只支援{“id”:”123”,”name”,”tom”}這種型別的資料解析,如果後臺傳入複雜 JSON 資料 , 他是不支援的 , 這時候就需要把獲取的資料分解 . 例如 我的 JSON 資料的格式是 {"rows":[{
Elasticsearch 更新 nested 巢狀物件 陣列中某個物件的某些欄位的值
在業務中遇到需要更新ES中某個表格裡面一個欄位中的值,這個欄位是一個nested巢狀物件陣列(二級表),需要更新裡面某個物件的某1-2個字端的值,網上搜索了一下,最後找到了一個解決辦法,運用的是es中自帶的指令碼。 { "script": { "lang": "pa
elasticsearch 巢狀物件查詢
// 建立巢狀物件mapping PUT /earth_index { "mappings": { "earthblog": { "properties": { "title":{ "type":"string"},
android 多重資料巢狀JSON資料解析
每一個錯誤都是一次提升自我的機會。後臺返回給了我這樣一個數據集合兩個集合資料,而且第二個集合資料裡還是0.1.2反正第一次解析我是沒有搞出來這個東西(PS:技術群裡讓我直接把後臺祭天)沒有辦法,食君之祿,為君分憂,做吧,那麼怎麼做呢...我用的是JSON解析的方法第一次:OJ
多層巢狀物件獲取底層物件屬性
最近在做一個需求,對業務層的查詢引數及返回值做校驗,檢視其是否是合法的值,使用AOP做。後來發現業務方法的返回值有多重巢狀,有Map,List,Set,Page,自定義包裝類等等,且包裝層還巢狀層,如Map< ?,List>,Page< Lis
使用HttpClient模擬訪問帶有巢狀物件引數的介面
專案中涉及到外部服務呼叫時,會使用到postman來模擬測試,例如一個介面如下: @RequestMapping("/test") public void test(@RequestBody User user){ } User類的屬性包括id、name
與左側的 圓括號“(”(位於“e:\大二上課程課件、作業和答案\資料結構\chapter4\sqstack\sqstack\mysqstack.h(23)”)匹配之前遇到檔案結束
錯誤原因是“某對圓括號只打了左括號而沒有右括號”,debug方法是:直接根據報錯提示:“與左側的 圓括號“(”(位於“e:\大二上課程課件、作業和答案\資料結構\chapter4\sqstack\sqstack\mysqstack.h(23)”)匹配之前遇到檔案結束”,找到對應的檔案和對應的行號,這裡是mys
Ajax(form表單檔案上傳、請求頭之contentType、Ajax傳遞json資料)
form表單檔案上傳 上菜 file_put.html <form action="" method="post" enctype="multipart/form-data"> {# 這裡必須要請求頭格式才能把上傳檔案的物件傳過去 enctype="multipart/form-
Android 整合okhttp3、listview解析json資料
上上篇文章寫了個Android的模擬介面,終於派上用場了 現在我們將接口裡面的json資料通過ListView顯示出來,效果如下: 先講一下ListView吧, 講ListView之前,肯定要講Adapter,在Android應用程式中,採用資料和顯示分開實現的資料處理方式,由於資料來