Browse Source

调整配置文件目录

chenwen 2 years ago
parent
commit
2413ad1e01

+ 1 - 0
.gitignore

@@ -1,2 +1,3 @@
 /target/
 /.settings/
+/src/main/resources/static/doc/

+ 17 - 0
pom.xml

@@ -51,6 +51,11 @@
 		    <artifactId>xframework6-spring-boot3-starter</artifactId>
 		    <version>0.0.1-SNAPSHOT</version>
 		</dependency>
+		
+		<dependency>
+		    <groupId>com.github.ben-manes.caffeine</groupId>
+		    <artifactId>caffeine</artifactId>
+		</dependency>
   </dependencies>
 	
   <dependencyManagement>
@@ -96,6 +101,18 @@
 					<failOnMissingWebXml>false</failOnMissingWebXml>
 				</configuration>
 			</plugin>
+			
+			<!--api doc 生成插件-->
+			<plugin>
+                <groupId>com.github.shalousun</groupId>
+                <artifactId>smart-doc-maven-plugin</artifactId>
+                <version>2.4.9</version>
+                <configuration>
+                    <configFile>./src/main/resources/smart-doc.json</configFile>
+                    <projectName>智能油田生产决策平台api文档</projectName>
+                    <includes>com.hb.proj.*</includes>
+                </configuration>
+            </plugin>
 
 		</plugins>
 	</build>

+ 27 - 0
src/main/java/com/hb/proj/allconfig/ApplicationContextProvider.java

@@ -0,0 +1,27 @@
+package com.hb.proj.allconfig;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ApplicationContextProvider implements ApplicationContextAware  {
+
+	private  static ApplicationContext applicationContextObj=null;
+	@Override
+	public void setApplicationContext(ApplicationContext applicationContext)
+			throws BeansException {
+		
+		applicationContextObj=applicationContext;
+	}
+
+	public static <T> T  getBean(String beanName,Class<T> beanClass){
+		return applicationContextObj.getBean(beanName, beanClass);
+	}
+	
+	public static Object getBean(String beanName){
+		return applicationContextObj.getBean(beanName);
+	}
+
+}

+ 16 - 0
src/main/java/com/hb/proj/allconfig/AuthInterceptor.java

@@ -0,0 +1,16 @@
+package com.hb.proj.allconfig;
+
+import org.springframework.web.servlet.HandlerInterceptor;
+
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+
+public class AuthInterceptor implements HandlerInterceptor {
+
+	@Override
+	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
+			throws Exception {
+		return HandlerInterceptor.super.preHandle(request, response, handler);
+	}
+
+}

+ 1 - 1
src/main/java/com/hb/proj/CustomMethodArgumentResolver.java → src/main/java/com/hb/proj/allconfig/CustomMethodArgumentResolver.java

@@ -1,4 +1,4 @@
-package com.hb.proj;
+package com.hb.proj.allconfig;
 import java.util.Iterator;
 
 import org.springframework.core.MethodParameter;

+ 60 - 0
src/main/java/com/hb/proj/allconfig/LocalConfig.java

@@ -0,0 +1,60 @@
+package com.hb.proj.allconfig;
+
+import java.util.Locale;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.MessageSource;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.i18n.LocaleContextHolder;
+import org.springframework.web.servlet.LocaleResolver;
+import org.springframework.web.servlet.i18n.SessionLocaleResolver;
+
+/**
+ * 多语言配置
+ * @author cwen
+ *
+ */
+
+@Configuration
+@ConditionalOnProperty(prefix = "spring.messages",name = "active")
+public class LocalConfig {
+	
+	//spring.messages.basename 配置国际化文件的名称,比如默认值是messages,多个值逗号分隔
+	private static MessageSource messageSource;
+	
+	public LocalConfig(MessageSource messageSource) {
+		LocalConfig.messageSource=messageSource;
+	}
+	
+	public static String get(String msgKey,String defaultMsg) {
+		return get(msgKey,defaultMsg,LocaleContextHolder.getLocale());
+	}
+	
+	public static String get(String msgKey) {
+		return get(msgKey,msgKey,LocaleContextHolder.getLocale());
+	}
+	
+	public static String get(String msgKey,String defaultMessage,Locale locale)
+    {
+        try
+        {
+            return messageSource==null?defaultMessage:messageSource.getMessage(msgKey, null, defaultMessage,locale);
+        }
+        catch (Exception e)
+        {
+            return msgKey;
+        }
+    }
+
+	/**
+     * 默认解析器 其中locale表示默认语言  bean name必须是localeResolver,
+     * 这样DispatcherServlet才能自动侦测到它,每DispatcherServlet只能注册一个区域解析器
+     */
+    @Bean
+    public LocaleResolver localeResolver() {
+        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
+        localeResolver.setDefaultLocale(Locale.CHINA);
+        return localeResolver;
+    }
+}

+ 1 - 1
src/main/java/com/hb/proj/RequestValidateExceptionHandler.java → src/main/java/com/hb/proj/allconfig/RequestValidateExceptionHandler.java

@@ -1,4 +1,4 @@
-package com.hb.proj;
+package com.hb.proj.allconfig;
 
 import java.util.Set;
 

+ 28 - 1
src/main/java/com/hb/proj/SpringMvcConfigurer.java → src/main/java/com/hb/proj/allconfig/SpringMvcConfigurer.java

@@ -1,4 +1,4 @@
-package com.hb.proj;
+package com.hb.proj.allconfig;
 
 import java.util.Arrays;
 import java.util.List;
@@ -18,24 +18,38 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 @Configuration
 public class SpringMvcConfigurer implements WebMvcConfigurer {
 
+	/**
+	 * 静态资源的处理
+	 */
 	@Override
 	public void addResourceHandlers(ResourceHandlerRegistry registry) {
 		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
 		WebMvcConfigurer.super.addResourceHandlers(registry);
 	}
 	
+	
+	/**
+	 * 接口自定义参数对象的解析器
+	 */
 	@Override
 	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
 		resolvers.add(new CustomMethodArgumentResolver());
 		WebMvcConfigurer.super.addArgumentResolvers(resolvers);
 	}
 
+	/**
+	 * 接口传参增加数据转换器(时间字符转为时间对象)
+	 */
 	@Override
 	public void addFormatters(FormatterRegistry registry) {
 		registry.addConverter(new StringToDateConverter());
 		WebMvcConfigurer.super.addFormatters(registry);
 	}
 	
+	/**
+	 * 跨域配置
+	 * @return
+	 */
     @Bean
     public FilterRegistrationBean<CorsFilter> corsFilter() {
         // 跨域配置
@@ -52,5 +66,18 @@ public class SpringMvcConfigurer implements WebMvcConfigurer {
         return bean;
     }
 
+    
+    
+	//@Override
+	/*public void addInterceptors(InterceptorRegistry registry) {
+		LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
+        localeInterceptor.setParamName("lang");  //拦截lang参数
+        registry.addInterceptor(localeInterceptor);
+		WebMvcConfigurer.super.addInterceptors(registry);
+	}*/
+	
+	
+	
+
 
 }

+ 1 - 1
src/main/java/com/hb/proj/StringToDateConverter.java → src/main/java/com/hb/proj/allconfig/StringToDateConverter.java

@@ -1,4 +1,4 @@
-package com.hb.proj;
+package com.hb.proj.allconfig;
 
 import java.text.SimpleDateFormat;
 import java.util.Date;

+ 23 - 5
src/main/java/com/hb/proj/utils/RespVO.java

@@ -1,15 +1,33 @@
 package com.hb.proj.utils;
 
-public class RespVO {
+public class RespVO<T>{
 
+	/**
+	 * true:调用成功;false:调用失败
+	 * 
+	 */
 	private Boolean success;
 	
-	private Object  data;
 	
+	/**
+	 * 接口返回数据
+	 */
+	private T  data;
+	
+	/**
+	 * 错误信息
+	 */
 	private String  error;
 	
+	/**
+	 * 接口调用返回码,0:成功;非0:失败
+	 */
 	private int code;  //兼容laygrid
 	
+	
+	/**
+	 * 一般消息
+	 */
 	private String msg;  //兼容laygrid
 	
 	public RespVO() {
@@ -17,7 +35,7 @@ public class RespVO {
 		this.code=0;
 	}
 	
-	public RespVO(Boolean success,Object  data,String  error) {
+	public RespVO(Boolean success,T  data,String  error) {
 		this.success=success;
 		this.data=data;
 		this.error=error;
@@ -33,11 +51,11 @@ public class RespVO {
 		this.success = success;
 	}
 
-	public Object getData() {
+	public T getData() {
 		return data;
 	}
 
-	public void setData(Object data) {
+	public void setData(T data) {
 		this.data = data;
 	}
 

+ 15 - 8
src/main/java/com/hb/proj/utils/RespVOBuilder.java

@@ -1,21 +1,24 @@
 package com.hb.proj.utils;
 
+import com.hb.proj.allconfig.LocalConfig;
+import com.hb.xframework.util.MD5Encrypt;
+
 public class RespVOBuilder {
 
-	public static RespVO  ok() {  //默认只设置success=true
-		return new RespVO();
+	public static RespVO<Object>  ok() {  //默认只设置success=true
+		return new RespVO<Object>();
 	}
 	
-	public static RespVO  ok(Object data) {
-		return new RespVO(true,data,null);
+	public static <T>  RespVO<T>  ok(T data) {
+		return new RespVO<T>(true,data,null);
 	}
 	
-	public static RespVO  error(String error) {
-		return new RespVO(false,null,error);
+	public static RespVO<Object>  error(String error) {
+		return new RespVO<Object>(false,null,getI18n(error));
 	}
 	
-	public static RespVO  error(String error,Object data) {
-		return new RespVO(false,data,error);
+	public static <T> RespVO<T>  error(String error,T data) {
+		return new RespVO<T>(false,data,getI18n(error));
 	}
 	
 	public static LayGridResp getLayGridResp(int total,Object data){
@@ -27,4 +30,8 @@ public class RespVOBuilder {
 		}
 		
 	}
+	
+	private static String getI18n(String str) {
+		return LocalConfig.get(MD5Encrypt.md5(str),str);
+	}
 }

+ 6 - 0
src/main/resources/application-dev.properties

@@ -9,7 +9,11 @@ server.tomcat.uri-encoding=UTF-8
 server.servlet.encoding.charset=UTF-8
 server.servlet.encoding.enabled=true
 server.servlet.encoding.force=true
+
+#语言国际化配置
+spring.messages.active=false
 spring.messages.encoding=UTF-8
+spring.messages.basename: static/i18n/messages
 
 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
 spring.jackson.time-zone=GMT+8
@@ -24,3 +28,5 @@ spring.datasource.password=hb
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 spring.datasource.type=com.hb.xframework.dao.util.HikariDataSourceWrap
 spring.datasource.dialect=MySQL
+
+

+ 15 - 0
src/main/resources/smart-doc.json

@@ -0,0 +1,15 @@
+{
+	"serverUrl":"http://127.0.0.1:8080/myproj",
+	
+	"outPath": "src/main/resources/static/doc",
+	"allInOne":true,
+	"createDebugPage":true,
+	"revisionLogs": [{ 
+      "version": "1.0", 
+      "revisionTime": "2023-02-01 10:30",
+      "status": "update", 
+      "author": "文", 
+      "remarks": "desc" 
+      }
+    ]
+}

+ 0 - 0
src/main/resources/static/i18n/messages.properties


+ 0 - 0
src/main/resources/static/i18n/messages_en_US.properties


+ 1 - 0
src/main/resources/static/i18n/messages_zh_CN.properties

@@ -0,0 +1 @@
+5c76474c13d9f8c5cd912ec5ca3f214a=lost param222