Ver código fonte

添加了全局json解析

wuweihao 5 anos atrás
pai
commit
cd1cafe6f3

+ 2 - 1
pom.xml

@@ -75,7 +75,8 @@
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
-            <version>1.2.47</version>
+            <!--<version>1.2.49</version>-->
+            <version>1.2.51</version>
         </dependency>
 
         <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->

+ 9 - 3
src/main/java/com/fd/config/MyInterceptor.java

@@ -14,7 +14,14 @@ import javax.servlet.http.HttpServletResponse;
 @Log4j2
 public class MyInterceptor implements HandlerInterceptor {
 
-    //重写preHandle方法
+    /**
+     * 重写preHandle方法
+     * @param request
+     * @param response
+     * @param handler
+     * @return
+     * @throws Exception
+     */
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         log.info("run preHandle");
@@ -22,8 +29,7 @@ public class MyInterceptor implements HandlerInterceptor {
         log.info("request url: {}", request.getRequestURL());
 
 
-        String originHeader = request.getHeader("origin");
-//        response.setHeader("Access-Control-Allow-Origin", originHeader);
+//        String originHeader = request.getHeader("origin");
         String requestHeaders = request.getHeader("Access-Control-Request-Headers");
         if (requestHeaders==null) {
             requestHeaders = "";

+ 40 - 4
src/main/java/com/fd/config/WebMvcConfg.java

@@ -1,8 +1,12 @@
 package com.fd.config;
 
 import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.alibaba.fastjson.serializer.ToStringSerializer;
 import com.alibaba.fastjson.support.config.FastJsonConfig;
 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
 import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -36,7 +40,7 @@ public class WebMvcConfg implements WebMvcConfigurer {
     public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(myInterceptor())
 //                .addPathPatterns("/**")
-                .addPathPatterns("/api/cesium/**")
+                .addPathPatterns("/api/**")
                 .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html");
     }
 
@@ -55,6 +59,7 @@ public class WebMvcConfg implements WebMvcConfigurer {
 
 
 
+
     /**
      * fastJson相关设置
      * Dto包含json,需要配置不然会异常
@@ -62,15 +67,46 @@ public class WebMvcConfg implements WebMvcConfigurer {
      */
     @Bean
     public HttpMessageConverters customConverters() {
-        FastJsonConfig fastJsonConfig = new FastJsonConfig();
-        fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteNonStringValueAsString);
+
 
         FastJsonHttpMessageConverter fastJson = new FastJsonHttpMessageConverter();
+
+        // 创建FastJson信息转换对象
+        FastJsonConfig fastJsonConfig = new FastJsonConfig();
+
+        /**
+         * 创建FastJsonConfig对象并设定序列化规则  序列化规则详见SerializerFeature类中
+         *
+         * 加入 fastJsonConfig.setSerializerFeatures 这方法,swagger会受影响。
+         * 目前把他注释掉,也能正常访问发送强求,先用着
+         * fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteNonStringValueAsString);
+         */
+
+        // 设置全程返回时间
+        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
+        // 设置返回值为null是时输出,不写的话,null 字段 不返回。也可以设置返回空串
+        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
         fastJson.setFastJsonConfig(fastJsonConfig);
+
+        //3、中文乱码解决方案
         List<MediaType> mediaTypeList = new ArrayList<>();
-        mediaTypeList.add(MediaType.valueOf("application/json;charset=UTF-8"));
+        mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
         mediaTypeList.add(MediaType.valueOf("text/html;charset=UTF-8"));
+
+
+        //4、将转换规则应用于转换对象
         fastJson.setSupportedMediaTypes(mediaTypeList);
+
+
         return new HttpMessageConverters(fastJson);
     }
+
+
+
+
+
+
+
+
+
 }

+ 1 - 1
src/main/java/com/fd/repository/StyleRepository.java

@@ -15,6 +15,6 @@ public interface StyleRepository extends IBaseRepository<StyleEntity, Long> {
      * 找最新的一条记录
      * 纯sql
      */
-    @Query(value = "SELECT * from t_style where output_file_id = ?1 order by a.create_time desc LIMIT 0,1", nativeQuery = true)
+    @Query(value = "SELECT * from t_style where output_file_id = ?1 order by create_time desc LIMIT 0,1", nativeQuery = true)
     StyleEntity findByOutputFileIdTop(Long id);
 }