zhouhao 7 роки тому
батько
коміт
3c4e57b428
12 змінених файлів з 162 додано та 54 видалено
  1. 10 0
      hsweb-examples/hsweb-examples-simple/pom.xml
  2. 4 6
      hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestController.java
  3. 27 0
      hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestEntity.java
  4. 18 25
      hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/convert/FastJsonGenericHttpMessageConverter.java
  5. 22 8
      hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/convert/FastJsonHttpMessageConverter.java
  6. 4 8
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-controller/src/main/java/org/hswebframework/web/controller/dictionary/DictionaryController.java
  7. 60 0
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/BoostDictDefineRepository.java
  8. 6 1
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/SimpleDictionaryService.java
  9. 1 2
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/parser/DictParserFormatter.java
  10. 1 2
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/parser/SimpleDictParserFormatter.java
  11. 1 2
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/parser/SimpleSingleDictParser.java
  12. 8 0
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-starter/src/main/java/org/hswebframework/web/dictionary/starter/DictionaryFactoryAutoConfiguration.java

+ 10 - 0
hsweb-examples/hsweb-examples-simple/pom.xml

@@ -106,6 +106,11 @@
             </exclusions>
         </dependency>
 
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-concurrent-cache</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-undertow</artifactId>
@@ -129,6 +134,11 @@
             <version>${project.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-system-dictionary-starter</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <!--组织架构-->
         <dependency>
             <groupId>org.hswebframework.web</groupId>

+ 4 - 6
hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestController.java

@@ -84,7 +84,10 @@ public class TestController implements QueryController<UserEntity, String, Query
     public ResponseMessage<PersonnelAuthorization> testPersonnel() {
         return ResponseMessage.ok(PersonnelAuthorization.current().orElseThrow(UnAuthorizedException::new));
     }
-
+    @PostMapping("/test/testDict")
+    public ResponseMessage<TestEntity> testPersonnel(@RequestBody TestEntity entity) {
+        return ResponseMessage.ok(entity);
+    }
     @Override
     public TestService getService() {
         return new TestService();
@@ -135,9 +138,4 @@ public class TestController implements QueryController<UserEntity, String, Query
             return null;
         }
     }
-
-    public static void main(String[] args) {
-        String id =org.apache.commons.codec.binary.Base64.encodeBase64String("test".getBytes());
-        System.out.println(id);
-    }
 }

+ 27 - 0
hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestEntity.java

@@ -0,0 +1,27 @@
+package org.hswebframework.web.example.simple;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.hswebframework.web.dict.Dict;
+import org.hswebframework.web.dict.Item;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class TestEntity {
+
+    @Dict(alias = "sexText", items = {
+            @Item(text = "男", value = "1"),
+            @Item(text = "女", value = "2")
+    })
+    private Byte sex;
+
+    private String sexText;
+}

+ 18 - 25
hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/convert/FastJsonGenericHttpMessageConverter.java

@@ -12,6 +12,8 @@ import org.hswebframework.web.ThreadLocalUtils;
 import org.hswebframework.web.controller.message.ResponseMessage;
 import org.hswebframework.utils.StringUtils;
 import org.hswebframework.web.convert.CustomMessageConverter;
+import org.hswebframework.web.dict.DictSupportApi;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.Ordered;
 import org.springframework.http.HttpInputMessage;
 import org.springframework.http.HttpOutputMessage;
@@ -37,6 +39,8 @@ import java.util.Set;
 public class FastJsonGenericHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> implements Ordered {
 
     public final static Charset UTF8 = Charset.forName("UTF-8");
+    @Autowired(required = false)
+    private DictSupportApi dictSupportApi;
 
     private Charset charset = UTF8;
 
@@ -111,11 +115,15 @@ public class FastJsonGenericHttpMessageConverter extends AbstractGenericHttpMess
                 }
             }
         }
-        return JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), type);
+        Object object = JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), type);
+        if (dictSupportApi != null) {
+            object = dictSupportApi.unwrap(object);
+        }
+        return object;
     }
 
     @Override
-    public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
+    public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         InputStream in = inputMessage.getBody();
         byte[] buf = new byte[1024];
@@ -136,12 +144,19 @@ public class FastJsonGenericHttpMessageConverter extends AbstractGenericHttpMess
         if (obj instanceof String) {
             return (String) obj;
         }
+
         String text;
         String callback = ThreadLocalUtils.getAndRemove("jsonp-callback");
         if (obj instanceof ResponseMessage) {
             ResponseMessage message = (ResponseMessage) obj;
-            text = JSON.toJSONString(obj, parseFilter(message), features);
+            if (dictSupportApi != null) {
+                message.setResult(dictSupportApi.wrap(message.getResult()));
+            }
+            text = JSON.toJSONString(obj, FastJsonHttpMessageConverter.parseFilter(message), features);
         } else {
+            if (dictSupportApi != null) {
+                obj = dictSupportApi.wrap(obj);
+            }
             text = JSON.toJSONString(obj, features);
         }
         if (!StringUtils.isNullOrEmpty(callback)) {
@@ -161,27 +176,5 @@ public class FastJsonGenericHttpMessageConverter extends AbstractGenericHttpMess
         out.flush();
     }
 
-    protected static SerializeFilter[] parseFilter(ResponseMessage<?> responseMessage) {
-        List<SerializeFilter> filters = new ArrayList<>();
-        if (responseMessage.getIncludes() != null) {
-            for (Map.Entry<Class<?>, Set<String>> classSetEntry : responseMessage.getIncludes().entrySet()) {
-                SimplePropertyPreFilter filter = new SimplePropertyPreFilter(classSetEntry.getKey());
-                filter.getIncludes().addAll(classSetEntry.getValue());
-                filters.add(filter);
-            }
-        }
-        if (responseMessage.getExcludes() != null) {
-            for (Map.Entry<Class<?>, Set<String>> classSetEntry : responseMessage.getExcludes().entrySet()) {
-                SimplePropertyPreFilter filter = new SimplePropertyPreFilter(classSetEntry.getKey());
-                filter.getExcludes().addAll(classSetEntry.getValue());
-                filters.add(filter);
-            }
-        }
-        PropertyFilter responseMessageFilter = (object, name, value) ->
-                !(object instanceof ResponseMessage) || value != null;
-        filters.add(responseMessageFilter);
-
-        return filters.toArray(new SerializeFilter[filters.size()]);
-    }
 
 }

+ 22 - 8
hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/convert/FastJsonHttpMessageConverter.java

@@ -8,10 +8,13 @@ import com.alibaba.fastjson.serializer.PropertyFilter;
 import com.alibaba.fastjson.serializer.SerializeFilter;
 import com.alibaba.fastjson.serializer.SerializerFeature;
 import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
+import org.hswebframework.utils.ClassUtils;
 import org.hswebframework.web.ThreadLocalUtils;
 import org.hswebframework.web.controller.message.ResponseMessage;
 import org.hswebframework.utils.StringUtils;
 import org.hswebframework.web.convert.CustomMessageConverter;
+import org.hswebframework.web.dict.DictSupportApi;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.Ordered;
 import org.springframework.http.HttpInputMessage;
 import org.springframework.http.HttpOutputMessage;
@@ -32,6 +35,9 @@ import java.util.Set;
 
 public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> implements Ordered {
 
+    @Autowired(required = false)
+    private DictSupportApi dictSupportApi;
+
     public final static Charset UTF8 = Charset.forName("UTF-8");
 
     private Charset charset = UTF8;
@@ -80,8 +86,8 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O
     }
 
     public Object readByBytes(Class<?> clazz, byte[] bytes) {
-        if(clazz==String.class){
-            return new String(bytes,charset);
+        if (clazz == String.class) {
+            return new String(bytes, charset);
         }
         if (null != converters) {
             CustomMessageConverter converter = converters.stream()
@@ -92,12 +98,15 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O
                 return converter.convert(clazz, bytes);
             }
         }
-        return JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
+        Object object = JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
+        if (dictSupportApi != null) {
+            object = dictSupportApi.unwrap(object);
+        }
+        return object;
     }
 
     @Override
-    protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException,
-            HttpMessageNotReadableException {
+    protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         InputStream in = inputMessage.getBody();
         byte[] buf = new byte[1024];
@@ -122,8 +131,14 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O
         String callback = ThreadLocalUtils.getAndRemove("jsonp-callback");
         if (obj instanceof ResponseMessage) {
             ResponseMessage message = (ResponseMessage) obj;
+            if (dictSupportApi != null) {
+                message.setResult(dictSupportApi.wrap(message.getResult()));
+            }
             text = JSON.toJSONString(obj, parseFilter(message), features);
         } else {
+            if (dictSupportApi != null) {
+                obj = dictSupportApi.wrap(obj);
+            }
             text = JSON.toJSONString(obj, features);
         }
         if (!StringUtils.isNullOrEmpty(callback)) {
@@ -136,15 +151,14 @@ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<O
     }
 
     @Override
-    protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException,
-            HttpMessageNotWritableException {
+    protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException {
         OutputStream out = outputMessage.getBody();
         byte[] bytes = converter(obj).getBytes(charset);
         out.write(bytes);
         out.flush();
     }
 
-    protected static SerializeFilter[] parseFilter(ResponseMessage<?> responseMessage) {
+    public static SerializeFilter[] parseFilter(ResponseMessage<?> responseMessage) {
         List<SerializeFilter> filters = new ArrayList<>();
         if (responseMessage.getIncludes() != null) {
             for (Map.Entry<Class<?>, Set<String>> classSetEntry : responseMessage.getIncludes().entrySet()) {

+ 4 - 8
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-controller/src/main/java/org/hswebframework/web/controller/dictionary/DictionaryController.java

@@ -21,6 +21,7 @@ import io.swagger.annotations.Api;
 import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.commons.entity.param.QueryParamEntity;
 import org.hswebframework.web.controller.GenericEntityController;
+import org.hswebframework.web.controller.SimpleGenericEntityController;
 import org.hswebframework.web.entity.dictionary.DictionaryEntity;
 import org.hswebframework.web.entity.dictionary.DictionaryItemEntity;
 import org.hswebframework.web.logging.AccessLogger;
@@ -36,17 +37,12 @@ import org.springframework.web.bind.annotation.RestController;
  */
 @RestController
 @RequestMapping("${hsweb.web.mappings.dictionary:dictionary}")
-@Authorize(permission = "dictionary",description = "数据字典管理")
-@Api(value = "数据字典",tags = "数据字典-字典配置")
-public class DictionaryController implements GenericEntityController<DictionaryEntity, String, QueryParamEntity, DictionaryEntity> {
+@Authorize(permission = "dictionary", description = "数据字典管理")
+@Api(value = "数据字典", tags = "数据字典-字典配置")
+public class DictionaryController implements SimpleGenericEntityController<DictionaryEntity, String, QueryParamEntity> {
 
     private DictionaryService dictionaryService;
 
-    @Override
-    public DictionaryEntity modelToEntity(DictionaryEntity model, DictionaryEntity entity) {
-        return model;
-    }
-
     @Autowired
     public void setDictionaryService(DictionaryService dictionaryService) {
         this.dictionaryService = dictionaryService;

+ 60 - 0
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/BoostDictDefineRepository.java

@@ -0,0 +1,60 @@
+package org.hswebframework.web.service.dictionary.simple;
+
+import org.hswebframework.web.dict.ClassDictDefine;
+import org.hswebframework.web.dict.DictDefine;
+import org.hswebframework.web.dict.ItemDefine;
+import org.hswebframework.web.dict.defaults.DefaultClassDictDefine;
+import org.hswebframework.web.dict.defaults.DefaultDictDefine;
+import org.hswebframework.web.dict.defaults.DefaultDictDefineRepository;
+import org.hswebframework.web.dict.defaults.DefaultItemDefine;
+import org.hswebframework.web.entity.dictionary.DictionaryEntity;
+import org.hswebframework.web.service.dictionary.DictionaryService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+@Service
+@CacheConfig(cacheNames = "dictionary")
+public class BoostDictDefineRepository extends DefaultDictDefineRepository {
+
+    @Autowired
+    private DictionaryService dictionaryService;
+
+    @Override
+    @Cacheable(key = "'DictDefineById:'+#id")
+    public DictDefine getDefine(String id) {
+        DictionaryEntity entity = dictionaryService.selectByPk(id);
+        if (entity == null) {
+            return super.getDefine(id);
+        }
+        List<ItemDefine> items = entity.getItems()
+                .stream()
+                .map(item -> DefaultItemDefine
+                        .builder()
+                        .value(item.getValue())
+                        .text(item.getText())
+                        .comments(item.getDescribe())
+                        .build())
+                .collect(Collectors.toList());
+
+        return DefaultDictDefine.builder()
+                .id(id)
+                .comments(entity.getDescribe())
+                .items(items)
+                .build();
+    }
+
+    @Override
+    @Cacheable(key = "'DictDefineByClass:'+#type.name")
+    public List<ClassDictDefine> getDefine(Class type) {
+        return super.getDefine(type);
+    }
+}

+ 6 - 1
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/SimpleDictionaryService.java

@@ -20,9 +20,12 @@ import org.hswebframework.web.dao.dictionary.DictionaryDao;
 import org.hswebframework.web.entity.dictionary.DictionaryEntity;
 import org.hswebframework.web.entity.dictionary.DictionaryItemEntity;
 import org.hswebframework.web.id.IDGenerator;
+import org.hswebframework.web.service.EnableCacheAllEvictGenericEntityService;
+import org.hswebframework.web.service.EnableCacheGenericEntityService;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.service.dictionary.DictionaryService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheConfig;
 import org.springframework.stereotype.Service;
 
 /**
@@ -31,7 +34,8 @@ import org.springframework.stereotype.Service;
  * @author hsweb-generator-online
  */
 @Service("dictionaryService")
-public class SimpleDictionaryService extends GenericEntityService<DictionaryEntity, String>
+@CacheConfig(cacheNames = "dictionary")
+public class SimpleDictionaryService extends EnableCacheAllEvictGenericEntityService<DictionaryEntity, String>
         implements DictionaryService {
     @Autowired
     private DictionaryDao dictionaryDao;
@@ -46,4 +50,5 @@ public class SimpleDictionaryService extends GenericEntityService<DictionaryEnti
         return dictionaryDao;
     }
 
+
 }

+ 1 - 2
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/parser/DictParserFormatter.java

@@ -4,9 +4,8 @@ import java.util.List;
 import java.util.function.BiFunction;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
+ * @see 3.0
  */
 public interface DictParserFormatter {
     <T> List<FormatterResult<T>> format(DictParserFormat format, Object value, BiFunction<String, String, T> mapping);

+ 1 - 2
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/parser/SimpleDictParserFormatter.java

@@ -7,9 +7,8 @@ import java.util.function.BiFunction;
 import java.util.stream.Collectors;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
+ * @since 3.0
  */
 public class SimpleDictParserFormatter implements DictParserFormatter {
 

+ 1 - 2
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-service/hsweb-system-dictionary-service-simple/src/main/java/org/hswebframework/web/service/dictionary/simple/parser/SimpleSingleDictParser.java

@@ -12,9 +12,8 @@ import java.util.function.Predicate;
 import java.util.stream.Collectors;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
+ * @since 3.0
  */
 public class SimpleSingleDictParser implements SingleDictParser {
 

+ 8 - 0
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-starter/src/main/java/org/hswebframework/web/dictionary/starter/DictionaryFactoryAutoConfiguration.java

@@ -1,5 +1,8 @@
 package org.hswebframework.web.dictionary.starter;
 
+import org.hswebframework.web.dict.DictDefineRepository;
+import org.hswebframework.web.dict.DictSupportApi;
+import org.hswebframework.web.dict.defaults.DefaultDictSupportApi;
 import org.hswebframework.web.service.dictionary.builder.DictionaryParserBuilder;
 import org.hswebframework.web.service.dictionary.simple.builder.SimpleDictionaryParserBuilder;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -19,4 +22,9 @@ public class DictionaryFactoryAutoConfiguration {
     public DictionaryParserBuilder dictionaryParserBuilder() {
         return new SimpleDictionaryParserBuilder();
     }
+
+    @Bean
+    public DictSupportApi dictSupportApi(DictDefineRepository repository) {
+        return new DefaultDictSupportApi(repository);
+    }
 }