多模块Maven项目
.gitignore文件
.idea *.iml target out log tmp test
父模块pom文件
4.0.0 com.xx.x1 x1
1.0-SNAPSHOT api common manager web-base pom org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE 1.8 3.6.1 3.1.0 其中一个子模块pom文件 org.springframework.boot spring-boot-starter-web junit junit test org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java org.projectlombok lombok 1.18.4 org.apache.commons commons-lang3 com.google.guava guava 22.0 org.apache.poi poi 3.17 org.apache.poi poi-ooxml 3.17
xx com.xx.x4 1.0-SNAPSHOT 4.0.0 api com.xx.x3 web-base 1.0-SNAPSHOT org.apache.maven.plugins maven-compiler-plugin ${maven.compiler.version} compile compile compile testCompile test-compile testCompile maven-assembly-plugin ${maven.assembly.version} assembly/assembly.xml aobp-xingxi-api package single aobp-xingxi-api false
启动类Boot
@SpringBootApplication(scanBasePackages = "com.xx.xxx.xxxx") @MapperScan(basePackages = "com.xx.xxx.xxxx.api.dao") @Slf4j public class Boot { public static void main(String[] args) { log.info("service starting"); SpringApplication.run(Boot.class, args).registerShutdownHook(); log.info("service start success"); } } yaml文件:
server: port: 8081 tomcat: basedir: tmp/tomcat spring: datasource: hikari: minimum-idle: 16 maximum-pool-size: 32 driver-class-name: com.mysql.cj.jdbc.Driver username: password: url: jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai mybatis: mapper-locations: classpath:mappers/*.xml type-aliases-package: com.xx.xxx.xxxx.common.entity save:(创建包) dir: user: tmp/user package: tmp/package img: tmp/img logging:(debug时用于打印sql语句) level: com.xx.xxx.xxxx.api.dao: debug 创建包时:
@Value("${save.dir.img}") private String imgDir; @PostConstruct void init(){ File file = new File(imgDir); if(!file.exists()){ file.mkdirs(); } } 创建文件位置:
各模块包名
跨域配置:
/** * @ClassName: GlobalCorsConfig * @Description: 跨域配置 * @author: yaozhenhua * @date: 2018/12/27 15:07 */ @Configuration public class GlobalCorsConfig { @Bean public CorsFilter corsFilter() { //1.添加CORS配置信息 CorsConfiguration config = new CorsConfiguration(); //放行哪些原始域 config.setAllowedHeaders(CollectionUtils.arrayToList(new String[]{"*"})); //是否发送Cookie信息 config.setAllowCredentials(true); //放行哪些原始域(请求方式) config.setAllowedMethods(CollectionUtils.arrayToList(new String[]{"POST", "GET", "PUT", "OPTIONS", "DELETE"})); //放行哪些原始域(头部信息) config.setAllowedOrigins(CollectionUtils.arrayToList(new String[]{"*"})); //2.添加映射路径 UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource(); configSource.registerCorsConfiguration("/**", config); //3.返回新的CorsFilter. return new CorsFilter(configSource); } } Controller层全局异常处理器:
@ControllerAdvice @Slf4j public class AobpExceptionHandler { //声明要捕获的异常 @ExceptionHandler(Exception.class) @ResponseBody public Result defaultExceptionHandler(Exception e) { if (e instanceof AobpException) { log.error(((AobpException) e).getMsg()); AobpException aobpException = (AobpException) e; return Result.error(aobpException.getCode(), aobpException.getMsg()); } log.warn(e.getMessage()); return Result.error(StatusConstant.STATUS_INTERNAL_ERR); } }