Commit 8a4983e2 authored by 雍欢's avatar 雍欢

1、UaspRequestMappingHandlerMapping兼容spring mvc;

2、bindDictionaryTextView
parent 27b72ec5
package com.huigou.uasp.client; package com.huigou.uasp.client;
import java.io.*; import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collection; import java.util.*;
import java.util.Enumeration; import java.util.stream.Collectors;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.Entity;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -243,7 +241,7 @@ public class CommonController extends ControllerBase { ...@@ -243,7 +241,7 @@ public class CommonController extends ControllerBase {
} }
protected String blank(Status status, String message, Object data) { protected String blank(Status status, String message, Object data) {
Map<String, Object> object = new HashMap<String, Object>(); Map<String, Object> object = new HashMap<>();
object.put("status", status.ordinal());// 状态 object.put("status", status.ordinal());// 状态
if (!StringUtil.isBlank(message)) { if (!StringUtil.isBlank(message)) {
object.put("message", message);// 信息 object.put("message", message);// 信息
...@@ -251,21 +249,79 @@ public class CommonController extends ControllerBase { ...@@ -251,21 +249,79 @@ public class CommonController extends ControllerBase {
if (data != null) {// 数据 if (data != null) {// 数据
if (data instanceof Page<?>) { if (data instanceof Page<?>) {
Page<?> page = (Page<?>) data; Page<?> page = (Page<?>) data;
Map<String, Object> result = new HashMap<String, Object>(3); Map<String, Object> result = new HashMap<>(3);
List<?> rows = page.getContent().stream()
.map(this::bindDictionaryTextView)
.collect(Collectors.toList());
result.put(Constants.RECORD, page.getTotalElements()); result.put(Constants.RECORD, page.getTotalElements());
result.put(Constants.ROWS, page.getContent()); result.put(Constants.ROWS, rows);
object.put(Constants.DATA, result); object.put(Constants.DATA, result);
} else if (data instanceof SDO) { } else if (data instanceof SDO) {
object.put(Constants.DATA, ((SDO) data).getProperties()); object.put(Constants.DATA, ((SDO) data).getProperties());
} else if (ClassHelper.isBaseType(data.getClass())) { } else if (ClassHelper.isBaseType(data.getClass())) {
object.put(Constants.DATA, data.toString()); object.put(Constants.DATA, data.toString());
} else { } else {
if (data instanceof Collection) {
data = ((Collection) data).stream()
.map(this::bindDictionaryTextView)
.collect(Collectors.toList());
} else if (data instanceof Map) {
List<?> rows = (List<?>) ((Map) data).get(Constants.ROWS);
if (rows != null) {
rows = rows.stream().map(this::bindDictionaryTextView).collect(Collectors.toList());
((Map) data).put(Constants.ROWS, rows);
}
} else {
data = bindDictionaryTextView(data);
}
object.put(Constants.DATA, data); object.put(Constants.DATA, data);
} }
} }
return blank(JSONUtil.toString(object)); return blank(JSONUtil.toString(object));
} }
/**
* 绑定字典的name。
* 只会对jpa实体类的被 @Dictionary 注解标记了的字段进行处理。
*
* @return 处理之后的结果。
*/
private Object bindDictionaryTextView(Object item) {
if (item == null) {
return null;
}
Entity entityAnnotation = item.getClass().getAnnotation(Entity.class);
if (entityAnnotation == null) {
return item;
}
Map<String, Object> map = ClassHelper.beanToMap(item);
Map<String, String> textViews = Arrays.stream(item.getClass().getDeclaredFields())
.filter(f -> f.getAnnotation(com.topsunit.query.annotations.Dictionary.class) != null)
.collect(Collectors.toMap(f -> String.join("", f.getName(), "TextView"), f -> mapToTextView(item, f)));
map.putAll(textViews);
return map;
}
private String mapToTextView(Object entity, Field field) {
com.topsunit.query.annotations.Dictionary dictionaryAnnotation = field.getAnnotation(com.topsunit.query.annotations.Dictionary.class);
Object codeValue = null;
try {
field.setAccessible(true);
codeValue = field.get(entity);
} catch (Exception e) {
}
if (codeValue == null) {
return "";
}
String textView = SystemCache.getDictionaryDetailText(dictionaryAnnotation.value(), codeValue);
// 如果textView为null,这里不能直接返回null值,不然Stream在合并的时候会报空指针异常
return textView != null
? textView
: String.valueOf(codeValue);
}
protected String success() { protected String success() {
return blank(Status.SUCCESS_TIPS, null, "ok"); return blank(Status.SUCCESS_TIPS, null, "ok");
} }
......
...@@ -14,9 +14,9 @@ import com.huigou.uasp.annotation.ControllerMethodMapping; ...@@ -14,9 +14,9 @@ import com.huigou.uasp.annotation.ControllerMethodMapping;
/** /**
* spring mvc 请求路径响应Mapping * spring mvc 请求路径响应Mapping
* *
* @author xx * @author xx
* 替换spring mvc 原有注解扫描功能,在Controller类上使用注解ControllerMapping后自动将类中方法加入到RequestMapping * 替换spring mvc 原有注解扫描功能,在Controller类上使用注解ControllerMapping后自动将类中方法加入到RequestMapping
*/ */
public class UaspRequestMappingHandlerMapping extends RequestMappingHandlerMapping { public class UaspRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
...@@ -24,7 +24,10 @@ public class UaspRequestMappingHandlerMapping extends RequestMappingHandlerMappi ...@@ -24,7 +24,10 @@ public class UaspRequestMappingHandlerMapping extends RequestMappingHandlerMappi
@Override @Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo info = null; RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
if (info != null) {
return info;
}
RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class); RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (methodAnnotation != null) { if (methodAnnotation != null) {
RequestCondition<?> methodCondition = getCustomMethodCondition(method); RequestCondition<?> methodCondition = getCustomMethodCondition(method);
...@@ -68,7 +71,7 @@ public class UaspRequestMappingHandlerMapping extends RequestMappingHandlerMappi ...@@ -68,7 +71,7 @@ public class UaspRequestMappingHandlerMapping extends RequestMappingHandlerMappi
protected RequestMappingInfo createRequestMappingInfo(ControllerMapping controllerMapping, Class<?> handlerType) { protected RequestMappingInfo createRequestMappingInfo(ControllerMapping controllerMapping, Class<?> handlerType) {
String[] patterns = resolveEmbeddedValuesInPatterns(controllerMapping.value()); String[] patterns = resolveEmbeddedValuesInPatterns(controllerMapping.value());
if (patterns != null && (patterns.length == 0)) { if (patterns != null && (patterns.length == 0)) {
patterns = new String[] { this.initLower(handlerType.getSimpleName()) }; patterns = new String[]{this.initLower(handlerType.getSimpleName())};
} }
RequestCondition<?> customCondition = getCustomTypeCondition(handlerType); RequestCondition<?> customCondition = getCustomTypeCondition(handlerType);
return RequestMappingInfo.paths(patterns).customCondition(customCondition).options(this.config).build(); return RequestMappingInfo.paths(patterns).customCondition(customCondition).options(this.config).build();
...@@ -77,14 +80,14 @@ public class UaspRequestMappingHandlerMapping extends RequestMappingHandlerMappi ...@@ -77,14 +80,14 @@ public class UaspRequestMappingHandlerMapping extends RequestMappingHandlerMappi
protected RequestMappingInfo createRequestMappingInfo(ControllerMethodMapping controllerMethodMapping, Method method) { protected RequestMappingInfo createRequestMappingInfo(ControllerMethodMapping controllerMethodMapping, Method method) {
String[] patterns = resolveEmbeddedValuesInPatterns(controllerMethodMapping.value()); String[] patterns = resolveEmbeddedValuesInPatterns(controllerMethodMapping.value());
if (patterns != null && (patterns.length == 0)) { if (patterns != null && (patterns.length == 0)) {
patterns = new String[] { method.getName() }; patterns = new String[]{method.getName()};
} }
RequestCondition<?> methodCondition = getCustomMethodCondition(method); RequestCondition<?> methodCondition = getCustomMethodCondition(method);
return RequestMappingInfo.paths(patterns).methods(controllerMethodMapping.method()).customCondition(methodCondition).options(this.config).build(); return RequestMappingInfo.paths(patterns).methods(controllerMethodMapping.method()).customCondition(methodCondition).options(this.config).build();
} }
protected RequestMappingInfo createRequestMappingInfo(Method method) { protected RequestMappingInfo createRequestMappingInfo(Method method) {
String[] patterns = new String[] { method.getName() }; String[] patterns = new String[]{method.getName()};
RequestCondition<?> methodCondition = getCustomMethodCondition(method); RequestCondition<?> methodCondition = getCustomMethodCondition(method);
return RequestMappingInfo.paths(patterns).customCondition(methodCondition).options(this.config).build(); return RequestMappingInfo.paths(patterns).customCondition(methodCondition).options(this.config).build();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment