Commit de6f8ba0 by 王能兵

Merge branch 'dev' into 'master'

Dev

See merge request !3
2 parents 5be3f346 96696ad6
...@@ -67,9 +67,12 @@ public class FeignDecoderForR extends FeignDecoder<R> { ...@@ -67,9 +67,12 @@ public class FeignDecoderForR extends FeignDecoder<R> {
PAGE_HELPER_PAGE_INFO(PageInfo.class) { PAGE_HELPER_PAGE_INFO(PageInfo.class) {
@Override @Override
public Object parse(R resp, Type type) { public Object parse(R resp, Type type) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type itemType = parameterizedType.getActualTypeArguments()[0];
JSONObject dataObj = JSON.parseObject(FeignParserForR.extractDataJson(resp)); JSONObject dataObj = JSON.parseObject(FeignParserForR.extractDataJson(resp));
// 先创造出 PageInfo 对象,再填充数据 // 先创造出 PageInfo 对象,再填充数据
PageInfo<?> pageInfo = PageInfo.of(dataObj.getJSONArray("list")); PageInfo<?> pageInfo = PageInfo.of(dataObj.getJSONArray("list")
.toJavaList((Class<?>) itemType));
pageInfo.setPages(dataObj.getInteger("totalPages")); pageInfo.setPages(dataObj.getInteger("totalPages"));
pageInfo.setTotal(dataObj.getLong("totalElements")); pageInfo.setTotal(dataObj.getLong("totalElements"));
return pageInfo; return pageInfo;
...@@ -83,11 +86,11 @@ public class FeignDecoderForR extends FeignDecoder<R> { ...@@ -83,11 +86,11 @@ public class FeignDecoderForR extends FeignDecoder<R> {
@Override @Override
public Object parse(R resp, Type type) { public Object parse(R resp, Type type) {
ParameterizedType parameterizedType = (ParameterizedType) type; ParameterizedType parameterizedType = (ParameterizedType) type;
Type itemType = parameterizedType.getActualTypeArguments()[0];
JSONObject dataObj = JSON.parseObject(FeignParserForR.extractDataJson(resp)); JSONObject dataObj = JSON.parseObject(FeignParserForR.extractDataJson(resp));
// 解析出 Page 对象,再填充数据 // 解析出 Page 对象,再填充数据
Page<?> page = dataObj.getJSONArray("page").toJavaObject(type); Page<?> page = dataObj.getJSONArray("page").toJavaObject(type);
page.addAll(dataObj.getJSONArray("items").toJavaObject( page.addAll(dataObj.getJSONArray("items").toJavaList((Class) itemType));
parameterizedType.getActualTypeArguments()[0]));
return page; return page;
} }
}, },
......
package com.glsx.admin.config;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Map;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.context.annotation.Configuration;
/**
* <p>Description: 用于配置 jackson 序列化和反序列化
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-05-14
*/
@Configuration
public class JacksonMapperConfig {
@Autowired(required = false)
private ObjectMapper objectMapper;
@Autowired(required = false)
private JacksonProperties jacksonProperties;
@PostConstruct
public void configJackson() {
if (objectMapper != null && jacksonProperties != null) {
this.configSerialization(objectMapper, jacksonProperties);
this.configDeserialization(objectMapper, jacksonProperties);
this.configOthers(objectMapper, jacksonProperties);
}
}
private void configSerialization(ObjectMapper objectMapper, JacksonProperties properties) {
Map<SerializationFeature, Boolean> serialization = properties.getSerialization();
if (serialization != null) {
serialization.forEach(objectMapper::configure);
}
}
private void configDeserialization(ObjectMapper objectMapper, JacksonProperties properties) {
Map<DeserializationFeature, Boolean> deserialization = properties.getDeserialization();
if (deserialization != null) {
deserialization.forEach(objectMapper::configure);
}
}
private void configOthers(ObjectMapper objectMapper, JacksonProperties properties) {
if (properties.getDateFormat() != null) {
objectMapper.setDateFormat(new SimpleDateFormat(properties.getDateFormat()));
}
if (properties.getTimeZone() != null) {
objectMapper.setTimeZone(properties.getTimeZone());
}
if (properties.getLocale() != null) {
objectMapper.setLocale(properties.getLocale());
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!