统一处理Gson序列化的json串中数值类型参数,否则会所有数值类型都被转为Double类型
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.nis.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -18,10 +20,80 @@ import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.internal.LinkedTreeMap;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import com.nis.exceptions.MaatConvertException;
|
||||
import com.nis.util.httpclient.ClientUtil;
|
||||
|
||||
public class ConfigServiceUtil {
|
||||
private static Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(new TypeToken<Map>() {}.getType(), new MapTypeAdapter())
|
||||
.registerTypeAdapter(new TypeToken<List>() {}.getType(), new MapTypeAdapter())
|
||||
.create();
|
||||
public static class MapTypeAdapter extends TypeAdapter<Object> {
|
||||
@Override
|
||||
public Object read(JsonReader in) throws IOException {
|
||||
JsonToken token = in.peek();
|
||||
switch (token) {
|
||||
case BEGIN_ARRAY:
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
in.beginArray();
|
||||
while (in.hasNext()) {
|
||||
list.add(read(in));
|
||||
}
|
||||
in.endArray();
|
||||
return list;
|
||||
|
||||
case BEGIN_OBJECT:
|
||||
Map<String, Object> map = new LinkedTreeMap<String, Object>();
|
||||
in.beginObject();
|
||||
while (in.hasNext()) {
|
||||
map.put(in.nextName(), read(in));
|
||||
}
|
||||
in.endObject();
|
||||
return map;
|
||||
|
||||
case STRING:
|
||||
return in.nextString();
|
||||
|
||||
case NUMBER:
|
||||
String temp = in.nextString();
|
||||
BigDecimal dbNum = new BigDecimal(temp);
|
||||
BigDecimal maxLong = new BigDecimal(Long.MAX_VALUE);
|
||||
BigDecimal maxInteger = new BigDecimal(Integer.MAX_VALUE);
|
||||
// 数字超过long的最大值,返回BigDecimal类型
|
||||
if (dbNum.compareTo(maxLong)==1) {
|
||||
return dbNum;
|
||||
}else if(dbNum.compareTo(maxInteger)==1){
|
||||
long lngNum = Long.parseLong(temp);
|
||||
return lngNum;
|
||||
}else{
|
||||
int lngNum = Integer.parseInt(temp);
|
||||
return lngNum;
|
||||
}
|
||||
|
||||
case BOOLEAN:
|
||||
return in.nextBoolean();
|
||||
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, Object value) throws IOException {
|
||||
// 序列化无需实现
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 从后台服务获取compileid,groupid,regionid
|
||||
* @param type 1是compileid,2是groupid,3是regionid
|
||||
@@ -45,15 +117,11 @@ public class ConfigServiceUtil {
|
||||
Response response= header.get();
|
||||
if( response.getStatus() == 200){
|
||||
result= response.readEntity(String.class);
|
||||
Gson gson=new GsonBuilder().disableHtmlEscaping()
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.create();
|
||||
Map<String, Object> resMap = gson.fromJson(result,Map.class);
|
||||
if(!StringUtil.isEmpty(resMap)){
|
||||
Map<String, Object> dataMap = (Map<String, Object>)resMap.get("data");
|
||||
if(!StringUtil.isEmpty(dataMap)){
|
||||
list = (List<Integer>)dataMap.get("pzIdList");
|
||||
|
||||
}else{
|
||||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||||
}
|
||||
@@ -211,16 +279,7 @@ public class ConfigServiceUtil {
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
//创建连接
|
||||
WebTarget wt = ClientUtil.getWebTarger("http://10.0.6.30:8080/maatRest/service/cfg/v1/getCompileId?sourceName=CONFIG_COMPILE&num=1");
|
||||
//获取响应结果
|
||||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||||
Response response= header.get();
|
||||
if( response.getStatus() == 200){
|
||||
String result= response.readEntity(String.class);
|
||||
}else{
|
||||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
|
||||
}
|
||||
getId(1,1);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user