总结 JAVA 开发过程中遇到的 SpringBoot 相关的配置问题
1. CORS(跨域)配置#
1.1. 方法一#
重写 WebMvcConfigurer 的 addCorsMappings 方法
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET")
.maxAge(3600)
.allowCredentials(true);
}1.2. 方法二#
适用于 Spring Boot + Shiro
import org.springframework.boot.web.servlet.FilterRegistrationBean;
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;
@Configuration
public class CorsConfig {
@Bean
public FilterRegistrationBean<CorsFilter> corsFilterFilterRegistrationBean() {
final FilterRegistrationBean<CorsFilter> registration = new FilterRegistrationBean<>(corsFilter());
registration.setOrder(0);
return registration;
}
private CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}2. Jackson 配置#
2.1. 方式一#
application.properties 配置文件
//设置输出时间戳
spring.jackson.serialization.write-dates-as-timestamps=true
//设置日期格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+82.2. 方式二#
重写 WebMvcConfigurer 的 extendMessageConverters 方法
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
// 忽略未识别属性
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 时间格式化
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// 字符串null转""
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString("");
}
});
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
// 设置格式化内容
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
converters.add(0, stringHttpMessageConverter);
converters.add(1, jackson2HttpMessageConverter);
}3. Mybatis 输出 SQL 语句#
application.properties配置文件
logging.level.[Mapper文件的包]=debug4. lombok 配置#
4.1. @EqualsAndHashCode(callSuper = true) 注解全局配置#
在 src/main/java 下创建 lombok.config 文件,内容如下:
# 声明该配置文件是一个根配置文件,从该配置文件所在的目录开始扫描
config.stopBubbling=true
# 全局配置 equalsAndHashCode 的 callSuper 属性为true
lombok.equalsAndHashCode.callSuper=call参考:https://projectlombok.org/features/EqualsAndHashCode
@EqualsAndHashCode 注解的 callSuper 默认为 false,如果需要进行全局设置,则使用配置文件。推荐使用注解。
5. SpringBoot 报错: 413 Request Entity Too Large#
5.1. 使用了 nginx 做反向代理,需要修改 nginx.conf#
添加配置:client_max_body_size 10m;
# 启动 nginx
# docker run --name=nginx --rm nginx:1.19
# 复制 docker 中的 nginx.conf 进行修改
# docker cp nginx:/etc/nginx/nginx.conf ./
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 10m;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server {
listen 80;
location ^~/api/ {
proxy_pass http://manager:30002/;
proxy_redirect off;
proxy_connect_timeout 4s; #配置点1
proxy_read_timeout 60s; #配置点2,如果没效,可以考虑这个时间配置长一点
proxy_send_timeout 12s; #配置点3
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr; #一般的web服务器用这个 X-Real-IP 来获取源IP
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; #如果nginx 服务器是作为反向代理服务器的,则这个配置项是必须的;否则看不到源IP
}
location / {
root /usr/share/nginx/html/;
index index.php index.html index.htm;
}
}
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}5.2. Spring Boot 应用#
在 application.properties 添加如下配置
server.tomcat.max-http-form-post-size=10MB
spring.servlet.multipart.max-file-size=100MB