首先:案例是model里的属性名大写,http response时json默认变成小写开头,和model属性不一致。
例子:
model为:
class User{
private String NAME;
private int AGE;
getter 和 setter...
}
http 请求后响应的json为:
{
"name":"张三",
"age":20
}
业务需求json为:
{
"NAME":"张三",
"AGE":20
}
可行的解决方案:
网上搜了好多,大部分都是在属性上添加@JsonProperty("XXX"),但是这里就又出现了个问题,转json后会有两个同名但大小写不一样的属性;? 就又去搜索,说是在getter方法上添加@JsonProperty("XXX"),解决了,但是字段少没问题,咱每个实体model上添加下,如果字段多了呢?实体多了呢?
完美的解决方案:
本人自己推敲的,分享给大家,好用就用,不好用请忽略,当然肯定有其他更好的,有知道的请文末留言告知博主,非常感谢?
下面上方法代码:
响应json前将对象传入此方法即可
/**
* 对象转json时属性变大写
*
* @param entity
* @return
*/
public static Map object2UpperCase(Object entity){
Map res = Maps.newHashMap();
if (null==entity){
return res;
}
//将对象的属性转化为大写
String json = JacksonUtil.bean2Json(entity);
Map map = JacksonUtil.json2Map(json,String.class,Object.class);
if (null!=map && !map.isEmpty()){
for(Map.Entry entry:map.entrySet()){
/**
* TODO-关键在这里根据业务需要转大小写
*/
res.put(entry.getKey().toUpperCase(),entry.getValue());
}
}
return res;
}
JacksonUtil.java json转化工具类
package com.ww.common.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* json转化工具
* @author Ace Lee
*
*/
public class JacksonUtil {
private static final ObjectMapper MAPPER = new ObjectMapper();
static {
//MAPPER.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, false);
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true);
}
private static final JsonFactory JSONFACTORY = new JsonFactory();
/**
* 把json参数转换成对应Vo
* 第二个参数请使用 Class ,比如JacksonUtil.json2Bean("{\"name\":\"0000\",\"age\":456}", Car.class);
* @param json
* @param valueTypeRef
* @return
*/
public static T json2Bean(String json, TypeReference valueTypeRef) {
T rtn =null;
try {
rtn = MAPPER.readValue(json, valueTypeRef);
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return rtn;
}
public static T json2Bean(String json, Class cls) {
T rtn =null;
try {
rtn = MAPPER.readValue(json, cls);
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return rtn;
}
/**
* 转换vo 为 json
*/
public static String bean2Json(Object o) {
StringWriter sw = new StringWriter();
JsonGenerator jsonGenerator = null;
try {
jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
MAPPER.writeValue(jsonGenerator, o);
return sw.toString();
} catch (Exception e) {
throw new RuntimeException(e);
//return null;
} finally {
if (jsonGenerator != null) {
try {
jsonGenerator.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
/**
* 转换vo 为 json
*/
public static String bean2Json(Object o,ObjectMapper mapper) {
StringWriter sw = new StringWriter();
JsonGenerator jsonGenerator = null;
try {
jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
mapper.writeValue(jsonGenerator, o);
return sw.toString();
} catch (Exception e) {
throw new RuntimeException(e);
//return null;
} finally {
if (jsonGenerator != null) {
try {
jsonGenerator.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
/**
* 转换vo 为 json,属性为null不被序列化
*/
public static String bean2JsonWithoutNull(Object o) {
StringWriter sw = new StringWriter();
JsonGenerator jsonGenerator = null;
ObjectMapper mapper=new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
try {
jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
mapper.writeValue(jsonGenerator, o);
return sw.toString();
} catch (Exception e) {
throw new RuntimeException(e);
//return null;
} finally {
if (jsonGenerator != null) {
try {
jsonGenerator.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
/**
* 转换Json String 为 HashMap
*/
@SuppressWarnings("unchecked")
public static Map json2Map(String json,
boolean collToString) {
try {
Map map = MAPPER.readValue(json, HashMap.class);
if (collToString) {
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue() instanceof Collection
|| entry.getValue() instanceof Map) {
entry.setValue(bean2Json(entry.getValue()));
}
}
}
return map;
} catch (Exception e) {
//return null;
throw new RuntimeException(e);
}
}
/**
* json String转换为Map
* @author [email protected]
* @date 2016年1月5日 下午9:12:06
* @param json
* @param clk - key的类型
* @param clv - value的类型
* @return
* @description
*/
public static Map json2Map(String json,Class clk,Class clv) {
try {
JavaType javaType = MAPPER.getTypeFactory().constructMapType(Map.class, clk, clv);
Map map = MAPPER.readValue(json, javaType);
return map;
} catch (Exception e) {
//return null;
throw new RuntimeException(e);
}
}
/**
* json String转换为ConcurrentMap
* @author [email protected]
* @date 2016年1月5日 下午9:12:06
* @param json
* @param clk - key的类型
* @param clv - value的类型
* @return
* @description
*/
public static ConcurrentMap json2ConMap(String json,Class clk,Class clv) {
try {
JavaType javaType = MAPPER.getTypeFactory().constructMapType(ConcurrentHashMap.class, clk, clv);
ConcurrentMap map = MAPPER.readValue(json, javaType);
return map;
} catch (Exception e) {
//return null;
throw new RuntimeException(e);
}
}
@SuppressWarnings("serial")
public static class jsonParseException extends Exception {
public jsonParseException(String message) {
super(message);
}
}
/**
* List 转换成json
*
* @param list
* @return
*/
public static String list2Json(List
另附上jackson的pom依赖
注意三个版本号必须保持一致
com.fasterxml.jackson.core
jackson-databind
2.9.3
com.fasterxml.jackson.core
jackson-core
2.9.3
com.fasterxml.jackson.core
jackson-annotations
2.9.3
有更好的方案,欢迎留言,再次感谢!