模版工程搭建

发布时间 2023-08-24 11:12:18作者: 鹿鹿的布丁

用于记录基础功能的搭建-【持续更新】

基础工程创建

简单的过程不去记录,没有意义

  • 最初的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
    </parent>

    <groupId>com.tzcxyh</groupId>
    <artifactId>lulu</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>lulu</name>
    <description>lulu</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • application.properties添加初始配置
#设置项目端口号 默认 8080
server.port=80
#设置项目访问前缀地址
server.servlet.context-path=/lulu

spring.application.name=lulu

logback配置

  • 在application.properties中添加
#日志文件配置
logging.file.path=lulu-log
logging.file.name=${spring.application.name}
  • 在resources文件夹中添加spring-logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <springProperty scope="context" name="APP_NAME" source="logging.file.name"/>
    <!-- 文件路径 -->
    <springProperty scope="context" name="FILE_PATH" source="logging.file.path"/>

    <!-- 日志文件输出 -->
    <appender name="FILE-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 文件路径 -->
        <file>${FILE_PATH}/${APP_NAME}-all.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- 文件名称 -->
            <fileNamePattern>${FILE_PATH}/%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
            <!-- 日志保留天数 -->
            <MaxHistory>30</MaxHistory>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
            <!-- 日志总保存量为80GB -->
            <totalSizeCap>80GB</totalSizeCap>
            <!--文件达到 最大xxx时会被压缩和切割 -->
            <maxFileSize>50MB</maxFileSize>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level | %logger{50} [%L] -| %msg %n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--控制台 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>[%yellow(%d{yyyy-MM-dd HH:mm:ss.SSS})] [%red(%thread)] %highlight(%-5level) | %cyan(%logger{50}) [%boldGreen(%L)] | %magenta(%msg) %n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE-LOG"/>
    </root>
</configuration>

解决跨域问题

  • 新建config文件夹,添加CorsConfig配置
package com.tzcxyh.lulu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * @Description 全局跨域处理
 **/
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true); // 允许cookies跨域
        //不同的springboot版本,2.4之后用这个
        corsConfiguration.addAllowedOriginPattern("*");
//        config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        corsConfiguration.addAllowedHeader("*");// #允许访问的头信息,*表示全部
        corsConfiguration.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        corsConfiguration.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许
        corsConfiguration.addAllowedMethod("HEAD");
        corsConfiguration.addAllowedMethod("GET");
        corsConfiguration.addAllowedMethod("PUT");
        corsConfiguration.addAllowedMethod("POST");
        corsConfiguration.addAllowedMethod("DELETE");
        corsConfiguration.addAllowedMethod("PATCH");
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}