1.需求分析
接口中間處理,調取第三方數據返回給前端,由於並行開發,提供給前端的字段名稱類型與第三方給的有差異
2.處理
利用jackson註解(2.9)
閱讀源碼發現 @JsonProperties 會同時改變序列化跟反序列化參數
@JsonSetter 只改變反序列化值
3.代碼
需要修改的類
@Data
public class VideoDO implements Serializable {
private List<A> list;
@Data
public static class A implements Serializable {
private Integer id;
@JsonSetter("name")
private String title;
@JsonSetter("coverImage")
private String image;
private Integer studyNumber;
@JsonSetter("updateTime")
private Long time;
private Integer isOfficial;
@JsonSerialize(using = ListSerializer.class)
private List tags;
}
}
@Data
public class BaseModel<T> implements Serializable {
private Integer code;
private String message;
private T data;
}
調用
BaseModel<VideoDO> baseModel =counselorFeign.findVideoList(0,0,0,0,10);
第三方給予數據格式
{
"code": 0,
"message": "操作成功",
"data": {
"list": [
{
"id": 55,
"name": "客戶不爲自己代言誰之過?",
"tags": [
{
"id": 261,
"name": "客戶體驗"
},
{
"id": 262,
"name": "服務質量"
},
{
"id": 263,
"name": "客戶分析"
}
],
"coverImage": "http://ks3-cn01/19/2018011915524211203013.jpg",
"duration": "00:12:28",
"studyNumber": 143,
"averageScore": 5,
"type": 1,
"updateTime": 1531381162000,
"createTime": 1516348364000,
"category": 4,
"isOfficial": 1,
"introduce": null
},
{
"id": 54,
"name": "如何快速與客戶建立信任?",
"tags": [
{
"id": 256,
"name": "客戶需求"
},
{
"id": 257,
"name": "簽單指導"
},
{
"id": 258,
"name": "溝通技巧"
}
],
"coverImage": "http://ks3-cn/2018031309305692103013.jpg",
"duration": "00:18:30",
"studyNumber": 264,
"averageScore": 5,
"type": 1,
"updateTime": 1531381162000,
"createTime": 1516348164000,
"category": 4,
"isOfficial": 1,
"introduce": null
},
{
"id": 48,
"name": "U啊啊啊",
"tags": [],
"coverImage": "http://ks3-cn-2018/01/09/2018010913534786803013.jpg",
"duration": "00:11:58",
"studyNumber": 63,
"averageScore": 0,
"type": 2,
"updateTime": 1535012114000,
"createTime": 1515477231000,
"category": 1,
"isOfficial": 1,
"introduce": null
}
]
}
}
返回數據
[
{
"id": 54,
"title": "如何快速與客戶建立信任?",
"image": "http://ks3-cn-/2018/03/13/jpg/2018031309305692103013.jpg",
"studyNumber": 264,
"time": 1531381162000,
"isOfficial": 1,
"tags": [
"客戶需求",
"簽單指導",
"溝通技巧"
]
},
{
"id": 56,
"title": "電詢和麪詢、微信溝通的區別",
"image": "http://ks3-cn-/2018/01/19/2018011916021250603013.jpg",
"studyNumber": 301,
"time": 1531381162000,
"isOfficial": 1,
"tags": [
"現場諮詢",
"電話溝通",
"微信溝通"
]
},
{
"id": 55,
"title": "客戶不爲自己代言誰之過?",
"image": "http://ks3-cn-/2018/01/19/2018011915524211203013.jpg",
"studyNumber": 143,
"time": 1531381162000,
"isOfficial": 1,
"tags": [
"客戶體驗",
"服務質量",
"客戶分析"
]
},
{
"id": 48,
"title": "UNB-時訓",
"image": "http://ks3-cn-i/2018/01/09/2018010913534786803013.jpg",
"studyNumber": 63,
"time": 1535012114000,
"isOfficial": 1,
"tags": []
}
]
補充一個自定義的序列化規則(map轉list)這裏如果是mybatis直接映射就可以了。
public class ListSerializer extends JsonSerializer<List<Map>> {
@Override
public void serialize(List<Map> list, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartArray();
list.forEach((k)->{
try {
jsonGenerator.writeString(k.get("name").toString());
} catch (IOException e) {
e.printStackTrace();
}
});
jsonGenerator.writeEndArray();
}
}
附加個mybatis映射
<select id="findVideoInfoByVideoId" resultMap="videoInfo">
SELECT
v.id,
v.image,
v.title,
v.time,
(v.real_num+v.show_num) studyNumber,
v.describe,
v.speaker,
v.score,
vt.NAME,
vu.video_url videoUrl,
vu.video_sort videoSort,
vu.video_name videoName,
vu.video_duration videoDuration
FROM video v
LEFT JOIN video_tag_relation vtr ON vtr.video_id = v.id
LEFT JOIN video_tag vt ON vtr.tag_id = vt.id
LEFT JOIN video_url vu ON vu.video_id= v.id
WHERE v.id=#{videoId}
</select>
<resultMap id="videoInfo" type="java.util.HashMap">
<id property="id" column="id"></id>
<result property="image" column="image"></result>
<result property="title" column="title"></result>
<result property="time" column="time"></result>
<result property="describe" column="describe"></result>
<result property="speaker" column="speaker"></result>
<result property="score" column="score"></result>
<result property="studyNumber" column="studyNumber"></result>
<collection property="tags" javaType="java.util.List" ofType="java.lang.String">
<result property="name" column="name"></result>
</collection>
<collection property="urls" javaType="java.util.List" ofType="java.util.Map">
<result property="videoUrl" column="videoUrl"></result>
<result property="videoName" column="videoName"></result>
<result property="videoSort" column="videoSort"></result>
<result property="videoDuration" column="videoDuration"></result>
</collection>
</resultMap>
javaType="java.util.List" ofType="java.util.Map" 相當於 List<Map>數據結構