problems_springboot
- problems_springboot
- 1 springboot无法查询到后台的数据
- 2 Spring Data JPA的多数据源配置时,执行sql失败
- 3 springboot run with flyway failed
- 4 idea中springboot项目打包多module的maven项目时报错
- 5 springboot启动报错
- 6 利用jackson将jsonStr转为java对象的多种报错
- 7 swagger界面上只显示一个实体类
- 8 springboot后端报错
- 9 idea中的springboot+gradle项目报错
- 10 请求
/bbb
报错Full authentication is required to access this resource
- 11 调用/refresh接口报错
- 12 mybatis开启驼峰命名映射
- 13 mybatis-plus执行insert操作时总是报错
- 14 @ConfigurationProperties不生效
- 15 springboot启动报错,无法注入bean
- 16 springboot启动报错
- 17
problems_springboot
1 springboot无法查询到后台的数据
springbooot中,整合mybatis时,在IUserMapper接口中,
@Select("select * from user where name like '%李%'")
List<User> findUserByName(String name);
@Select的sql语句查不到数据。
RCA: mysql的url中,
application.properties
的配置中,配置了spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring5
未指定编码,导致中文乱码问题,所以后台就查不到数据了。
solution: url中指定字符编码,配置如下:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring5?useSSL=true&useUnicode=true&characterEncoding=UTF-8
2 Spring Data JPA的多数据源配置时,执行sql失败
desc: error log below:
Caused by: org.hibernate.exception.SQLGrammarException: error performing isolated work
Caused by: java.sql.SQLSyntaxErrorException: Table 'test.hibernate_sequence' doesn't exist
RCA:
need to explicitly specify the primary key's auto-increment strategy to GenerationType.IDENTITY.
solution:
@Entity
@Data
@NoArgsConstructor
public class User {
@Id
// 需要明确指定主键的自增策略,否则报错:SQLSyntaxErrorException: Table 'test.hibernate_sequence' doesn't exist
// strategy 默认值GenerationType.AUTO,需要修改为GenerationType.IDENTITY
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
3 springboot run with flyway failed
desc: error log below:
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `test` but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
RCA:
如果我们并不是在项目之初就启用flyway的话,那么在有历史数据的情况下,启用flyway后,由于数据库中,并不存在flyway所依赖的表,所以将会出现:set baselineOnMigrate to true to initialize the schema history table
的错误。
solution:
add config item spring.flyway.baselineOnMigrate=true
to application.properties or application.yml
note:
Maybe it should be flyway.baselineOnMigrate=true
in springboot 1.x. If the above config is invalid, we can try flyway.baselineOnMigrate=true
.
4 idea中springboot项目打包多module的maven项目时报错
desc: error log below:
Could not find artifact com.mediocre:parentProject:pom:1.0-SNAPSHOT
RCA:
无法解析父级Maven的pom.
子级Maven的pom文件中,relativePath标签使用的默认值:<relativePath/>
,文档中介绍默认值表示<relativePath>../pom.xml</relativePath>
,但编译环境(IDEA和命令行)不认这种写法。
solution:
只有显式地写为<relativePath>../pom.xml</relativePath>
,再次执行mvn clean package名称,编译通过,没有报错。
后来,测试发现只要注释掉 <relativePath>../pom.xml</relativePath>
也能正常打包了。
还有人说,多模块项目构建时,先将parent项目要先install一回,之后子项目才可以运行mvn compile命令,否则就会报如上异常。
extension: about packaging
Maven中pom.xml的packaging类型: 项目的打包类型:pom、jar、war.
父模块必须以pom打包类型,同时以
而对于各个子项目,需要在其对应的pom文件开头申明对父级项目的引用,通过GAV(groupId, artifactId, version)实现。对于子项目自己的GAV配置,GV如果不配置,则会从父级项目的配置继承过来。子模块可通过dependencies标签来添加自己的依赖,此外子类项目的packaging值只能是war或者jar,前面已经说过,packaging默认是jar类型。如果是需要部署的项目,则需要打包成war类型,如果只是内部调用或者是作服务使用,则推荐打包成jar类型。
5 springboot启动报错
errorlog:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.NoRouteToHostException: 没有到主机的路由 (Host unreachable)
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
RCA:
数据库服务器主机关机了,或者网络不通。
6 利用jackson将jsonStr转为java对象的多种报错
note: 以下有些有解决方案,有些没来得及写。只要了解了jackson的转换规则和特点,就很容易发现报错原因和解决方案。
errorlog1:
Cannot construct instance of `com.mediocre.dto.PostQueryDTO` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
RCA: PostQueryDTO类缺少默认的无参构造器,因为无参构造器被其他的显式声明的有参构造器覆盖了。
solution: 给DTO添加 @NoArgsConstructor注解,或者显式编写一个无参构造器。
// error2:
// jsonStr -> HashMap<String, Object>, 其中 Object 包括2种类型:Post[], String
JavaType javaType = BizUtils.getCollectionType(HashMap.class, ArrayList.class, Post.class);
Map<String, ArrayList<Post>> map = Constant.OBJECTMAPPER.readValue(postListJsonStr, javaType);
errorlog2:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot find a (Map) Key deserializer for type [collection type; class java.util.ArrayList, contains [simple type, class java.lang.Object]]
// error3:
JavaType javaType = BizUtils.getCollectionType(HashMap.class, ArrayList.class);
Map<String, ArrayList<Post>> map = Constant.OBJECTMAPPER.readValue(postListJsonStr, javaType);
errorlog3:
java.lang.IllegalArgumentException: Cannot create TypeBindings for class java.util.HashMap with 1 type parameter: class expects 2
// error4:
JavaType javaType = BizUtils.getCollectionType(HashMap.class, String.class, ArrayList.class);
Map<String, ArrayList<Post>> map = Constant.OBJECTMAPPER.readValue(postListJsonStr, javaType);
errorlog4:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<java.lang.Object>` out of VALUE_NUMBER_INT token
at [Source: (String)"{"postList":[{"id":15165266,"title":"bbb_update3","url":"//www.cnblogs.com/mediocreWorld/p/15165266.html","isPublished":true,"isDraft":false,"feedBackCount":0,"webCount":1,"aggCount":0,"viewCount":1,"datePublished":"2021-08-20T10:24:00","entryName":null,"postType":1,"postConfig":16477,"dateUpdated":"2021-08-20T10:43:00","accessPermission":0},{"id":15156160,"title":"skills_java","url":"//www.cnblogs.com/mediocreWorld/p/15156160.html","isPublished":true,"isDraft":false,"feedBackCount":0,"webCount""[truncated 2911 chars]; line: 1, column: 3363] (through reference chain: java.util.HashMap["postsCount"])
RCA: json字符串是Map<String, Object>形式,因为map中的value可能是List也可能是Stirng,以及Int,但是我这写死了为List,所以报错了。
solution: 将BizUtils.getCollectionType(HashMap.class, String.class, ArrayList.class) 改为 BizUtils.getCollectionType(HashMap.class, String.class, Object.class)
// error5:
JavaType javaType = BizUtils.getCollectionType(ArrayList.class, Post.class);
JavaType javaType2 = BizUtils.getCollectionType(HashMap.class, String.class, javaType.getClass());
Map<String, ArrayList<Post>> map = Constant.OBJECTMAPPER.readValue(postListJsonStr, javaType2);
errorlog5:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.fasterxml.jackson.databind.type.CollectionType` out of START_ARRAY token
// error6:
JavaType javaType = BizUtils.getCollectionType(HashMap.class, String.class, String.class);
Map<String, String> map = Constant.OBJECTMAPPER.readValue(postListJsonStr, javaType);
errorlog6:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token
at [Source: (String)"{"postList":[{"id":15165266,"title":"bbb_update3","url":"//www.cnblogs.com/mediocreWorld/p/15165266.html","isPublished":true,"isDraft":false,"feedBackCount":0,"webCount":1,"aggCount":0,"viewCount":1,"datePublished":"2021-08-20T10:24:00","entryName":null,"postType":1,"postConfig":16477,"dateUpdated":"2021-08-20T10:43:00","accessPermission":0},{"id":15156160,"title":"skills_java","url":"//www.cnblogs.com/mediocreWorld/p/15156160.html","isPublished":true,"isDraft":false,"feedBackCount":0,"webCount""[truncated 2911 chars]; line: 1, column: 13] (through reference chain: java.util.HashMap["postList"])
RCA: jsonStr -> HashMap<String, Object>, 其中 Object 包括2种类型:Post[], String,这里将Post[]数组类型也转换为String了,所以报错
solution: 将 BizUtils.getCollectionType(HashMap.class, String.class, String.class) 改为 BizUtils.getCollectionType(HashMap.class, String.class, Object.class)
7 swagger界面上只显示一个实体类
Models模块中,无论如何只有User这一个实体类。 后来发现不是加上@ApiModel就可以加入Models中的,必须要在controller层中关联这个实体类。由于我把所有实体类创建好后只测试了User,其它的实体类都没关联,所以swagger-ui.html中的Models没有实体类的显示。具体参考下面的文章:
https://deepmind.t-salon.cc/article/5113
https://blog.csdn.net/weixin_38819361/article/details/107208228
https://blog.csdn.net/chen__fei/article/details/86110889
还在网上看到一种说法, @ApiModel的value属性,必须全局唯一,否则只显示一个,所以一般使用description属性来注释实体类。
8 springboot后端报错
com.apusic.ams.connector.ClientAbortException: java.io.IOException: 断开的管道(Broken pipe)
RCA:
后端执行时间过长,前端已经关闭了连接,后端执行完后,尝试往前端连接写入数据,此时连接已断开,所以报该错误。这次后端是查找了大量数据,然后进行处理,最后写入到excel文档,然后尝试通过连接返回前端,以便下载该文档,但是处理和写入的过程太长了,有2分钟。
solution:
- 前端发起请求时,设置一个较长的超时时间
- 后端优化代码,缩短处理时间
9 idea中的springboot+gradle项目报错
desc:
idea中的springboot项目,打开某个类run.halo.app.config.properties.HaloProperties.java,报错(使用gradle编译)。
errorlog:
springboot configuration annotation processor not found in classpath
网上的解决方案是pom.xml文件里面添加如下配置.
<!-- 自定义的元数据依赖->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
但是,这个时maven项目的解决方案,我的是gradle项目。
在idea推荐的参考链接中找到了解决方案:
(点击上面截图中的“open documentation”,可以打开idea推荐的参考链接: https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor)
在build.gradle文件中,dependencies {} 中,添加如下语句:
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
解决。
另外,我发现build.gradle中原来有另一个配置: annotationProcessor 'org.projectlombok:lombok' ,但是并不能消除那个提示信息。
那暂时先不管那个提示信息了,以后有影响再修改测试吧。
note: gradle的build.gradle,相当于maven的pom.xml ,都是配置依赖的。
另外一个问题:我的很多java类中报错:找不到方法,我观察了下,都是getter和setter方法。
RCA & solution: 我没有安装lombok插件,在idea中安装lombok插件,即可解决。
10 请求/bbb
报错Full authentication is required to access this resource
发起请求后的响应内容:
{"code":3,"message":"Full authentication is required to access this resource","data":""}
RCA: Spring Security 禁止访问该 uri
在spring security的配置文件 public class SecurityConfig extends JwtWebSecurityConfigurerAdapter {}
中,有如下配置:
// 过滤器的安全拦截器的每一次的要求
http.authorizeRequests().filterSecurityInterceptorOncePerRequest(true)
.antMatchers("/auth/**", "/auth", "/captcha", "/error").permitAll()
//通过Oauth2登录时绑定用户接口,暂时开启,否则与JwtAuthorizationTokenFilter逻辑冲突(本系统还没有用户所以查不出来)
.antMatchers("/bindOAuth2User").permitAll()
.antMatchers("/file/**").permitAll()
.antMatchers("/api/bbb/**").permitAll()
// .antMatchers("/bbb/**").permitAll()
// 使其支持跨域
.requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
.anyRequest().fullyAuthenticated()
.and().exceptionHandling().accessDeniedHandler(baseJwtAccessDeniedHandler())
.authenticationEntryPoint(baseJwtAuthenticationEntryPoint());
配置了只允许访问特定模式的路径。
solution:
将请求路径 /bbb
改为 /api/bbb
11 调用/refresh接口报错
desc:
执行如下命令: curl -X POST http://localhost:8082/refresh
errorlog:
前端报错:
{"timestamp":1637053306778,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource.","path":"/refresh"}
后端报错:
Full authentication is required to access actuator endpoints. Consider adding Spring Security or set 'management.security.enabled' to false.
solution:
在配置文件bootstrap.yml中增加配置项:
# 禁用springSecurity
management:
security:
enabled: false
12 mybatis开启驼峰命名映射
最简单的方法是在application.yml中配置,如下:
# mybatis
mybatis:
configuration:
mapUnderscoreToCamelCase: true
13 mybatis-plus执行insert操作时总是报错
执行方法如下:
类1:public interface BizOrderService extends IService<BizOrder> {...}
类2:public class BizOrderServiceImpl extends ServiceImpl<BizOrderMapper, BizOrder> implements BizOrderService {...}
BizOrderServiceImpl.save(someEntityObject);
errorlog:
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: INSERT command denied to user 'ciic'@'192.168.1.3' for table 'biz_order'
### The error may exist in com/beyondsoft/orders/mapper/BizOrderMapper.java (best guess)
### The error may involve com.beyondsoft.orders.mapper.BizOrderMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO orders.biz_order ( order_id, member_id, order_sn, member_username, total_amount, pay_amount, pay_type, receiver_name, receiver_phone, receiver_detailed_address, order_status, delete_status, create_time ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
### Cause: java.sql.SQLSyntaxErrorException: INSERT command denied to user 'ciic'@'192.168.1.3' for table 'biz_order'
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: INSERT command denied to user 'ciic'@'192.168.1.3' for table 'biz_order'
这个错误原因我查了好久,太隐蔽了。
RCA:
注意打印到控制台的insert语句:INSERT INTO orders.biz_order...
这里面的数据库名是orders,而这个是我本地的数据库名,现在切换到了远程数据库,数据库名称应该是 bys_orders
看到这,就很容易定位到错误了,是在实体类 BizOrder 上配置的注解错误了: @TableName(schema = "orders", value = "biz_order")
应该配置为 @TableName(schema = "bys_orders", value = "biz_order")
后续:后来又发生一次该错误,直接把 schema参数去掉了,也可以正常执行的。
其实schema参数,反而使系统和数据库耦合度提高了,所以应该去掉,mybatisplus不需要该参数,默认使用application.yml文件中配置的数据源。
14 @ConfigurationProperties不生效
DESC:以下类FeignConfig中的所有字段,无法注入yml配置文件中的内容。
@Configuration
@ConfigurationProperties("orders.feign.client.inventorys")
public class FeignConfig {
private String name;
private String url;
private Integer connectTimeoutMillis;
private Integer readTimeoutMillis;
private Long retryPeriod;
private Long retryMaxPeriod;
private Integer retryMaxAttempts;
...
}
RCA:该类没有setter方法
SOLUTION:在类上面添加注解 @Setter
15 springboot启动报错,无法注入bean
10:11:21.314 [restartedMain] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - [refresh,596] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'swaggerConfig': Unsatisfied dependency expressed through field 'projectConfig'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ian.common.config.ProjectConfig' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
RCA1:
注解
检查@Controller、@Service、@Repository、 @Component 是否加上其中的一个.
RCA2:
检查包扫描的路径
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!
SOLUTION:
我是第二种情况,无意间改动了包结构,导致系统无法扫描到不在覆盖范围内的类了。修改包结构,解决。
16 springboot启动报错
22:32:41.647 [restartedMain] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - [refresh,559] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authorizationServerConfig': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
RCA:
使用了默认的DataSource,但却在Application启动类中添加了 (exclude= {DataSourceAutoConfiguration. class }),错误代码如下:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
SOLUTION:
去掉exclude,改为如下代码:
@SpringBootApplication