SpringBoot问题系列(一)

mac2025-10-17  5

1 认证401

取消security <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

2 Unsatisfied dependency expressed through field

Unsatisfied dependency expressed through field ‘peopleInfosService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘peopleInfosServiceImpl’: Unsatisfied dependency expressed through field ‘peopleInfosDao’;

启动文件添加扫描的包 @MapperScan("com.sb.dao") 完整 @SpringBootApplication @MapperScan("com.sb.dao") @EnableCaching public class SbUsageApplication{ public static void main(String[] args){ SpringApplication.run(SbUsageApplication.class, args); } }

3 Invalid bound statement (not found)

问题 sql.xml和mappr.java映射文件不在同一个文件夹下.解决 sql.xml和mappr.java映射文件放在同一个文件夹下.

4 数据库字段映射不到类字段

问题 mybatis-config.xml配置文件未加载实体类.解决 mybatis-config.xml添加实体类路径. <configuration> <typeAliases> <package name="com.sb.po"></package> </typeAliases> </configuration>

5 外部Tomcat启动spring-boot找不到controller

问题 未设置外部tomcat启动屏蔽sb自带的tomcat; 未设置servlet拦截路径解决 修改pom.xml文件,添加屏蔽tomcat <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>

启动文件配置servlet

public class SbUsageApplication extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application){ return application.sources(SbUsageApplication.class); } public static void main(String[] args){ SpringApplication.run(SbUsageApplication.class, args); } }

6 读取templates动态文件

添加thymeleaf <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 访问 returun "index";

7 访问静态资源

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.context.annotation.Configuration; @Configuration public class ConfigurerAdapter implements WebMvcConfigurer{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry){ registry.addResourceHandler("/**") .addResourceLocations("classpath:/resources/") .addResourceLocations("classpath:/static/"); } }

其中,addResourceHandler为路由URI,addResourceLocations为静态资源路径path,可配置多个路径.

http://localhost:8090/index.html

8 加载yml配置文件报错

Failed to load property source from location ‘classpath:/application.yml’: duplicate key

原因 yml文件格式错误,使用了tab进行缩进.解决 tab缩进改为空格缩进.

9 缺少bean

原因 启动文件没有扫描到bean组件.解决 在启动类中添加组件扫描.Usage import org.springframework.context.annotation.ComponentScan; @ComponentScan(basePackages = { "包路径" }) Demo import org.springframework.context.annotation.ComponentScan; @ComponentScan(basePackages = {"com.sb.config"})

但是使用ComponentScan会导致接口请求错误404.

情况1 将bean与启动类放在同级文件夹下,war包可正常提供web服务,jar仍出现404.情况2 将bean和component与启动类放在同级文件夹下,jar包正常提供web服务,war包404报错.总结 jar包和war包对组件位置要求不同.

10 mybatis懒加载序列化失败

Type definition error: [simple type, class org.apache.ibatis.executor.loader.javassist.Javassist

原因 序列化及反序列化失败.解决 实例类上添加@JsonIgnoreProperties(value={“handler”}) import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(value={"handler"})

11 SSL警告信息

Establishing SSL connection without server’s identity verification is not recommended.

原因方案 添加userSSL=false配置 jdbc.url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=false

【参考文献】 [1]https://blog.csdn.net/hz_940611/article/details/80771832 [2]https://blog.csdn.net/yy290879584/article/details/83419244 [3]https://www.cnblogs.com/a-xu/p/10027643.html [4]https://blog.csdn.net/Eggppp/article/details/84877921 [5]https://blog.csdn.net/zj7321/article/details/83240475 [6]https://blog.csdn.net/en_joker/article/details/87358573 [7]https://www.cnblogs.com/javajiuyangzhenjing/p/10210646.html [8]https://blog.csdn.net/qq_39597203/article/details/79939027 [9]https://blog.csdn.net/wgp15732622312/article/details/79951977

最新回复(0)