SpringBoot 相关问题汇总【持续更新】

1. 当前工程多个子模块,当前工程 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>
    <groupId>com.feng.mall</groupId>
    <artifactId>mall-feng</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mall-feng</name>
    <description>All in one</description>
    <packaging>pom</packaging>
    <modules>
        <module>mall-product</module>
    </modules>
</project>

<packaging>pom</packaging>标签中内容是pom,表示当前工程管理多个子模块,而当前工程不包含任何java代码。紧接着是 <modules></modules> 包括多个子模块。

2. spring-boot-maven-plugin not found

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

解决办法:指定版本

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.5.2</version>
</plugin>

3. import javax.validation 包下的注解飘红,说找不到

  1. 该包在 springboot 2.3.0 以前的版本,只需项目中引入

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  2. 在springboot 2.3.0 以后版本中,需要额外引入

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

4. lombok 1.18.4 在 idea 2021.1.3 Ultimate Edition中失效

问题:“You aren‘t using a compiler supported by lombok, so lombok will not work and has been disabled.”
解决办法:在以下位置加该配置"-Djps.track.ap.dependencies=false"
在这里插入图片描述

5. ubuntu20.04上安装chrome

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb

6. springboot 采用高2.5.2后,出现 When allowCredentials is true, allowedOrigins cannot contain the special value “*” since that cannot be set on the “Access-Control-Allow-Origin” response header. To allow credentials to a set of origins, list them explicitly or consider using “allowedOriginPatterns” instead.

将如下代码:

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOrigins("*")
            .allowCredentials(true)
            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
            .maxAge(3600);
    }
}

改为:

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOriginPatterns("*")
            .allowCredentials(true)
            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
            .maxAge(3600);
    }
}

7. mybatis-plus整合到springboot项目

<!-- 加入 mybatis-plus 启动器相关依赖 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
</dependency>

8. org.apache.http.HttpStatus 类所在的包 httpcomponents

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.14</version>
</dependency>

9. org.apache.commons.lang.StringUtils 类所在的包 commons-lang

<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>${commons.lang.version}</version>
</dependency>

10. idea 中 java 版本为1.5, 修改1.8后自动变回1.5的情况

在这里插入图片描述
pom.xml文件中,加入如下配置

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

11. parent.relativePath’ of POM io.renren:renren-generator:1.0.0 (/home/mall/work/gitrepository/mall-feng/renren-generator/pom.xml) points at com.feng.mall:mall-feng instead of org.springframework.boot:spring-boot-starter-parent, please verify your project structure

加入<relativePath></relativePath>

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.5.2</version>
	<relativePath></relativePath>
</parent>

12. Spring Cloud 整合 OpenFeign 时候报错 No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer

原因:由于SpringCloud Feign 在Hoxton.M2 RELEASED 版本之后不再使用 Ribbon 而是使用 spring-cloud-loadbalancer,所以不引入 spring-cloud-loadbalancer 会报错

pom.xml中添加:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

13. Mybatis-Plus 实体类中的字段,如果在数据库的表中不存在,要想查询不报错,可以使用 @TableField(exist = false) 注解

	@TableField(exist = false)
	private List<CategoryEntity> children;

14. maven 导入 aliyun-oss-spring-boot-starter 报 not found

导入 aliyun-oss-spring-boot-starter 报not found

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>aliyun-oss-spring-boot-starter</artifactId>
</dependency>

解决办法:加入 aliyun-spring-boot-dependencies

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>aliyun-spring-boot-dependencies</artifactId>
            <version>1.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

15. 导入阿里云OSS相关依赖,启动报错

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    com.alibaba.cloud.context.AliCloudSdk.<init>(AliCloudSdk.java:76)

The following method did not exist:

    com.aliyuncs.profile.DefaultProfile.getHttpClientConfig()Lcom/aliyuncs/http/HttpClientConfig;

The method's class, com.aliyuncs.profile.DefaultProfile, is available from the following locations:

    jar:file:/home/mall/.m2/repository/com/aliyun/aliyun-java-sdk-core/3.4.0/aliyun-java-sdk-core-3.4.0.jar!/com/aliyuncs/profile/DefaultProfile.class

The class hierarchy was loaded from the following locations:

    com.aliyuncs.profile.DefaultProfile: file:/home/mall/.m2/repository/com/aliyun/aliyun-java-sdk-core/3.4.0/aliyun-java-sdk-core-3.4.0.jar

解决办法,导入aliyun-java-sdk-core

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.22</version>
</dependency>

16. jackson相关注解

参考:https://blog.csdn.net/itguangit/article/details/78701110
jackson的注解分为三类:1. 序列化时生效的注解;2.反序列化时生效的注解;3.两种情况都生效的注解。

  1. @JsonInclude 可以标注在字段和类上
    @JsonInclude(JsonInclude.Include.NON_NULL) 标识如果字段的值为NULL,则序列化成JSON中,不会包含该字段。
  2. @JsonIgnore 标注在属性或方法上,表示忽略标注的该属性。
  3. @JsonIgnoreProperties 标注在类上
    @JsonIgnoreProperties(value = {“createTime”,“updateTime”}) 表示忽略类的属性 createTime 和 updateTime。
  4. @JsonIgnoreType 标注在类上
    当其它类中的属性类型时该类时,则该属性被忽略。
    @JsonIgnoreType
    public class SomeOtherEntity {
        private Long id;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
    }
    public class SomeEntity {
        private String name;
        private String desc;
        private SomeOtherEntity entity;
    }
    
  5. @JsonProperty 指定某个属性和json映射的名称
    public class Entity {
        @JsonProperty("user_name")
        private String userName;
    }
    
    {"user_name":"zhangsan"}
    
  6. @JsonPropertyOrder 指定属性在 json 字符串中的顺序
  7. @JsonSetter 反序列化情况生效。解决 json 键名与 pojo 字段名称不匹配问题。
    public class Entity {
        private String desc;
        @JsonSetter("description") // json 中的 description 字段值,赋值给实体类的 des字段。
        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
    

17. MyBatis-Plus 报错 Cause: java.sql.SQLException: Field ‘spu_id’ doesn’t have a default value

原因:实体类指定了主键生成策略,是自动增加。但是在保存数据的时候,对象的属性对应着数据库中的主键字段,又赋值了。所以会报错。第一种解决方案

  1. 对应主键的字段不要赋值
  2. 修改主键策略

如:

public class SpuInfoDescEntity implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 商品id
	 */
	@TableId
	private Long spuId;
	/**
	 * 商品介绍
	 */
	private String decript;

}

改为

public class SpuInfoDescEntity implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 商品id
	 */
	@TableId(type = IdType.INPUT)
	private Long spuId;
	/**
	 * 商品介绍
	 */
	private String decript;

}

18.日期的格式化

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值