JsonMapper、XmlMapper工具,增加 JsonView 过滤参数

This commit is contained in:
thinkgem
2020-05-20 23:16:26 +08:00
parent 4609d8aee7
commit 6b059e29dd
9 changed files with 131 additions and 38 deletions

View File

@@ -108,6 +108,20 @@ public class JsonMapper extends ObjectMapper {
return null;
}
}
/**
* Object可以是POJO也可以是Collection或数组。
* 如果对象为Null, 返回"null".
* 如果集合为空集合, 返回"[]".(根据 JsonView 渲染)
*/
public String toJsonString(Object object, Class<?> jsonView) {
try {
return this.writerWithView(jsonView).writeValueAsString(object);
} catch (IOException e) {
logger.warn("write to json string error:" + object, e);
return null;
}
}
/**
* 输出JSONP格式数据.
@@ -218,6 +232,13 @@ public class JsonMapper extends ObjectMapper {
return JsonMapper.getInstance().toJsonString(object);
}
/**
* 对象转换为JSON字符串根据 JsonView 渲染)
*/
public static String toJson(Object object, Class<?> jsonView){
return JsonMapper.getInstance().toJsonString(object, jsonView);
}
/**
* 对象转换为JSONP字符串
*/

View File

@@ -67,6 +67,18 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
}
}
/**
* Object可以是POJO也可以是Collection或数组。根据 JsonView 渲染)
*/
public String toXmlString(Object object, Class<?> jsonView) {
try {
return this.writerWithView(jsonView).writeValueAsString(object);
} catch (IOException e) {
logger.warn("write to xml string error:" + object, e);
return null;
}
}
/**
* 反序列化POJO或简单Collection如List<String>.
* @see #fromJson(String, JavaType)
@@ -97,6 +109,13 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
return XmlMapper.getInstance().toXmlString(object);
}
/**
* 对象转换为XML字符串根据 JsonView 渲染)
*/
public static String toXml(Object object, Class<?> jsonView){
return XmlMapper.getInstance().toXmlString(object, jsonView);
}
/**
* XML字符串转换为对象
*/

View File

@@ -21,6 +21,7 @@ import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.io.PropertiesUtils;
import com.jeesite.common.lang.ExceptionUtils;
@@ -179,8 +180,20 @@ public class ServletUtils {
* @param data 消息数据
* @return JSON字符串{result:'true',message:'', if map then key:value,key2:value2... else data:{} }
*/
@SuppressWarnings("unchecked")
public static String renderResult(String result, String message, Object data) {
return renderResult(result, message, data, null);
}
/**
* 返回结果JSON字符串支持JsonP请求参数加__callback=回调函数名)
* @param result Global.TRUE or Globle.False
* @param message 执行消息
* @param data 消息数据
* @param jsonView 根据 JsonView 过滤
* @return JSON字符串{result:'true',message:'', if map then key:value,key2:value2... else data:{} }
*/
@SuppressWarnings("unchecked")
public static String renderResult(String result, String message, Object data, Class<?> jsonView) {
Map<String, Object> resultMap = MapUtils.newHashMap();
resultMap.put("result", result);
resultMap.put("message", message);
@@ -200,6 +213,7 @@ public class ServletUtils {
resultMap.put("data", data);
}
}
Object object = null;
HttpServletResponse response = getResponse();
HttpServletRequest request = getRequest();
if (request != null){
@@ -209,24 +223,29 @@ public class ServletUtils {
if (response != null){
response.setContentType(MediaType.APPLICATION_XML_VALUE);
}
return XmlMapper.toXml(resultMap);
}else{
if (response != null){
response.setContentType(MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8");
if (jsonView != null) {
return XmlMapper.toXml(resultMap, jsonView);
}else {
return XmlMapper.toXml(resultMap);
}
if (ObjectUtils.toBoolean(PropertiesUtils.getInstance().getProperty("web.jsonp.enabled"))) {
String functionName = request.getParameter("__callback");
if (StringUtils.isNotBlank(functionName)){
return JsonMapper.toJsonp(functionName, resultMap);
}
}
if (ObjectUtils.toBoolean(PropertiesUtils.getInstance().getProperty("web.jsonp.enabled"))) {
String functionName = request.getParameter("__callback");
if (StringUtils.isNotBlank(functionName)){
object = new JSONPObject(functionName, resultMap);
}
return JsonMapper.toJson(resultMap);
}
}else{
if (response != null){
response.setContentType(MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8");
}
return JsonMapper.toJson(resultMap);
}
if (response != null){
response.setContentType(MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8");
}
if (object == null) {
object = resultMap;
}
if (jsonView != null) {
return JsonMapper.toJson(object, jsonView);
}else {
return JsonMapper.toJson(object);
}
}
@@ -253,6 +272,19 @@ public class ServletUtils {
return renderString(response, renderResult(result, message, data), null);
}
/**
* 直接将结果JSON字符串渲染到客户端支持JsonP请求参数加__callback=回调函数名)
* @param response 渲染对象:{result:'true',message:'',data:{}}
* @param result 结果标识Global.TRUE or Globle.False
* @param message 执行消息
* @param data 消息数据
* @param jsonView 根据 JsonView 过滤
* @return null
*/
public static String renderResult(HttpServletResponse response, String result, String message, Object data, Class<?> jsonView) {
return renderString(response, renderResult(result, message, data, jsonView), null);
}
/**
* 将对象转换为JSON、XML、JSONP字符串渲染到客户端JsonP请求参数加__callback=回调函数名)
* @param request 请求对象用来得到输出格式的指令JSON、XML、JSONP
@@ -261,18 +293,33 @@ public class ServletUtils {
* @return null
*/
public static String renderObject(HttpServletResponse response, Object object) {
return renderObject(response, object, null);
}
/**
* 将对象转换为JSON、XML、JSONP字符串渲染到客户端JsonP请求参数加__callback=回调函数名)
* @param request 请求对象用来得到输出格式的指令JSON、XML、JSONP
* @param response 渲染对象
* @param object 待转换JSON并渲染的对象
* @param jsonView 根据 JsonView 过滤
* @return null
*/
public static String renderObject(HttpServletResponse response, Object object, Class<?> jsonView) {
HttpServletRequest request = getRequest();
String uri = request.getRequestURI();
if (StringUtils.endsWithIgnoreCase(uri, ".xml") || StringUtils
.equalsIgnoreCase(request.getParameter("__ajax"), "xml")){
return renderString(response, XmlMapper.toXml(object));
}else{
if (ObjectUtils.toBoolean(PropertiesUtils.getInstance().getProperty("web.jsonp.enabled"))) {
String functionName = request.getParameter("__callback");
if (StringUtils.isNotBlank(functionName)){
return renderString(response, JsonMapper.toJsonp(functionName, object));
}
}
if (ObjectUtils.toBoolean(PropertiesUtils.getInstance().getProperty("web.jsonp.enabled"))) {
String functionName = request.getParameter("__callback");
if (StringUtils.isNotBlank(functionName)){
object = new JSONPObject(functionName, object);
}
}
if (jsonView != null) {
return renderString(response, JsonMapper.toJson(object, jsonView));
}else {
return renderString(response, JsonMapper.toJson(object));
}
}