Springboot项目开发遇到的各种问题汇总及解决方案《持续更新中···》 |
您所在的位置:网站首页 › 轮播图html代码最容易遇到的问题 › Springboot项目开发遇到的各种问题汇总及解决方案《持续更新中···》 |
异常一:IDEA连接mysql8.0.16,报[08001] Could not create connection to database server. Connection to icloud_db@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up. 原因是Mysql版本比较高出现的系列适配问题。 解决方式: 1. mysql5.5 使用:com.mysql.jdbc.Driver而8.0.*版本用com.mysql.cj.jdbc.Driver 2. 追加useSSL+serverTimezone+characterEncoding+autoReconnect jdbc:mysql://localhost:3306/icloud_db?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true mysql的连接驱动配置在这个位置,idea--->Database--->"+"---->>data source--->>mysql
------------------------------------------------------------------ 分割线 ---------------------------------------------------------------- 异常二: IDEA中,使用generator插件生成mybatis代码,遇到Could not autowire. No beans of 'xxx' type found.异常 解决方式1:mapper文件上加@Repository注解,这是从sping2.0新增的一个注解,用于简化 Spring 的开发,实现数据访问; 2:在mapper文件上加@Component注解,把普通pojo实例化到spring容器中; ------------------------------------------------------------------ 分割线 ---------------------------------------------------------------- 异常三: TypeException: The alias 'Criterion' is already mapped to the value **** 通过mybaits插件自动生成代码(多张数据库表的mapper文件)时,报如上异常; 原因:是因为mybatis2.0.1自身缺陷导致的bug,而且mybatis默认使用2.0.1。 解决方式:使用mybatis-spring 2.0.0 或 2.0.2-SNAPSHOT ------------------------------------------------------------------ 分割线 ---------------------------------------------------------------- 异常四: FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes. 原因:通过MultipartFile文件上传时,报该异常。原因:上传文件大小超过tomcate的最大限制;解决方式: 方式一:在application.properties中加入配置,改变上传文件最大值; 1、1.4版本之前配置方式: multipart.maxFileSize=2000Mb multipart.maxRequestSize=2500Mb 2、1.4版本后修改为: spring.http.multipart.maxFileSize=2000Mb spring.http.multipart.maxRequestSize=2500Mb 3、2.0版本之后修改该为: spring.http.multipart.max-file-size=2000Mb spring.http.multipart.max-request-size=2500Mb 方式二:在启动类 Application中添加配置,我是采取这种方式解决的。 1.给该类添加@Configuration 注解, 2.在该类中添加该方法; @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //文件最大 factory.setMaxFileSize("10240KB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); }------------------------------------------------------------------ 分割线 ---------------------------------------------------------------- 异常五:Could not resolve view with name 'xxxxxxxxxxx' in servlet with name 'dispatcherServlet'" 出现该异常的原因:由于我的请求参数比较多,于是把它们封装成一个类,然后又在.mapper文件中引用了该类;而且Controller类用@Controller注解的; 解决方式: 给Controller类添加@RestController注解; @Controller与@RestController区别如下@RestController is a stereotype annotation that combines @ResponseBody and @Controller. ------------------------------------------------------------------ 分割线 ---------------------------------------------------------------- 异常六: FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of ... 出现原因:上传的文件大小超过最大限制 解决方式: 在你的Application入口类中加入,multipartConfigElement方法 /** * 设置文件上传最大值 * * @return */ @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //文件最大 factory.setMaxFileSize(DataSize.of(50, DataUnit.MEGABYTES)); //MB /// 设置总上传数据总大小 factory.setMaxRequestSize(DataSize.of(70, DataUnit.MEGABYTES)); return factory.createMultipartConfig(); }------------------------------------------------------------------ 分割线 --------------------------------------------------------------- 异常七:在使用mybatis-generator:generate自动生成Dao和Mapper文件时报异常: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project XXX: resource datasource.properties does not exist 原因:无法找到对应 resource文件. 解决方法:在pom.xml文件中加入 src/main/java **/*.xml true src/main/resources **/*.properties------------------------------------------------------------------ 分割线 --------------------------------------------------------------- 异常八:redis Connection refused: no further information 原因: redis配置文件 配置有误或者格式错误; 解决方案: 1、 redis 配置,yml文件 # redis 配置 redis: database: 3 # redis数据库索引(默认为0),我们使用索引为3的数据库,避免和其他数据库冲突 host: localhost # redis服务器地址(默认为localhost) port: 6379 # redis端口(默认为6379) password: # redis访问密码(默认为空) timeout: 0 # redis连接超时时间(单位为毫秒) jedis: pool: # redis连接池配置 max-active: 8 # 最大可用连接数(默认为8,负数表示无限) max-idle: 8 # 最大空闲连接数(默认为8,负数表示无限) min-idle: 0 # 最小空闲连接数(默认为0,该值只有为正数才有作用) max-wait: -1 # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)2、 redis 配置,properties文件 spring.redis.hostName: 192.168.174.128 spring.redis.port: 6379 spring.redis.password: xuan123456 spring.redis.database: 2 spring.redis.timeout: 0 spring.redis.pool.max-active: 8 spring.redis.pool.max-wait: -1 spring.redis.pool.max-idle: 8 spring.redis.pool.min-idle: 0 ------------------------------------------------------------------ 分割线 --------------------------------------------------------------- 异常九:System. out.println("中文") 在Console上打印出来为乱码 解决方案:File--Settings--Editor--File Encodings 中全局选择GBK;然后OK;------------------------------------------------------------------ 分割线 --------------------------------------------------------------- 异常十:使用mybatis时,遇到 nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class XXX 时, 原因是mybatis插件自动生成mapper.xml中你新增了构造,但是mapper.java中你没同步对应的构造; 解决方案:请根据异常提示的类(mybatis自动生成的类),检查构造方法;缺少对应的构造方法; ------------------------------------------------------------------ 分割线 --------------------------------------------------------------- 异常十一:打包时遇到 target\surefire-reports for the individual test results. Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream. 原因:单元测试不通过,maven打包就停止编译; 解决方案:在pom.xml中下加入 org.apache.maven.plugins maven-surefire-plugin true ------------------------------------------------------------------ 分割线 --------------------------------------------------------------- 异常十二:SpringBoot引用Eureka导致返回结果由json变为xml解决方案 原因是:Eureka内部依赖了com.fasterxml.jackson.dataformat 的 jackson-dataformat-xml,xml的优先级比json高,所以返回的是xml; 此处列举了一种比较合理的解决方案: 1、 pom.xml中添加如下依赖: com.fasterxml.jackson.jaxrs jackson-jaxrs-xml-provider 2.14.2然后run 项目, 在访问的url接口后面加上.json则返回json;.xml则返回xml; 如: http://localhost:9092/user/login.json?account=1 原理是 jackson-jaxrs-xml-provider 会根据后缀拦截替换返回的是json还是xml; 如果加了 .json 报 404 则 需要配置一下 WebMvcConfigurer; @Configuration public class WebConfig extends WebMvcConfigurationSupport { /** * setUseSuffixPatternMatch : 设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配; * setUseTrailingSlashMatch : 设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配 * * @param configurer */ @Override protected void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseSuffixPatternMatch(true) .setUseTrailingSlashMatch(true); } }------------------------------------------------------------------ 分割线 --------------------------------------------------- 异常十三:SpringCloud 项目时 IllegalStateException: PathVariable annotation was empty on param 0. 出错代码: @GetMapping("/sayhi.json/{nickname}") R sayhi(@PathVariable String nickname);解决方案:PathVariable 添加 value @PathVariable(value="nickname") @GetMapping("/sayhi.json/{nickname}") R sayhi(@PathVariable(value="nickname") String nickname);------------------------------------------------------------------ 分割线 --------------------------------------------------- 异常十三:springcloud gateway 运行报错:Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency. 提示: spring-boot-starter-web 依赖注释掉;如果不存在该依赖则需在.yml文件中加入: spring: main: web-application-type: reactive------------------------------------------------------------------ 分割线 --------------------------------------------------- 异常十四:使用 Nacos + 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会报错。 解决方案: com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.cloud spring-cloud-starter-loadbalancer org.springframework.cloud spring-cloud-starter-openfeign------------------------------------------------------------------ 分割线 --------------------------------------------------- 异常十五:使用nacos+sentinel,feign: sentinel: enabled: true 运行服务即报错: Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.cloud.openfeign.FeignClientFactoryBean.getFallback()" because "feignClientFactoryBean" is null 原因: alibaba已经修复该问题,不要听那些降springboot版本等骚操作; 解决方案:可以通过开启懒加载来解决该问题: 将 spring.cloud.openfeign.lazy-attributes-resolution 设置为 true openfeign4.0 with sentinel run failed! · Issue #3024 · alibaba/spring-cloud-alibaba · GitHubCaused by: java.lang.NullPointerException: Cannot invoke "org.springframework.cloud.openfeign.FeignClientFactoryBean.getFallback()" because "feignClientFactoryBean" is null at com.alibaba.cloud.sentinel.feign.SentinelFeign$Builder$1.crea...https://github.com/alibaba/spring-cloud-alibaba/issues/3024 ------------------------------------------------------------------ 分割线 --------------------------------------------------- 异常十六:springcloud 项目 nacos + gateway 访问接口地址时报错: gateway报There was an unexpected error (type=Service Unavailable, status=503) 异常原因: 由于springcloud2020弃用了Ribbon,因此Alibaba在2021版本nacos中删除了Ribbon的jar包,因此无法通过lb路由到指定微服务,出现了503情况。所以只需要引入springcloud loadbalancer包即可 解决方案: 在gateway 微服务模块的 pom.xml中添加依赖:然后重新运行服务即可; org.springframework.cloud spring-cloud-starter-loadbalancer------------------------------------------------------------------ 分割线 --------------------------------------------------- 异常十七:微服务 Ambiguous mapping. Cannot map 'xxxxxxController' method 原因:多个服务中@PostMapping路径相同导致映射冲突;最合理的解决方案: @FeignClient()中加contextId = "" 区分; 如: ·在UserClient111类中 @FeignClient(name = "provider-user", contextId = "ManagerUserClient", fallback = UserClientFallback.class, configuration = FeignConfig.class) public interface UserClient111 { @Operation(summary = "用户注册") @PostMapping("/user/register.json") ResponseData register(@SpringQueryMap UserRegisterVO vo); } ·在UserClient222类中 @FeignClient(name = "provider-user", contextId = "OperatorUserClient", fallback = UserClientFallback.class, configuration = FeignConfig.class) public interface UserClient222 { @Operation(summary = "用户注册") @PostMapping("/user/register.json") ResponseData register(@SpringQueryMap UserRegisterVO vo); }------------------------------------------------------------------ 分割线 --------------------------------------------------- 异常十八:new Date()插入数据库后时间晚8小时 原因:中国是东八区,mysql使用了默认时区0时区; 个人觉得没必要修改mysql的时区,比较合理的一种方案是:设置 serverTimezone=GMT%2B8 如下: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8【腾讯云低价服务器】 云产品精选秒杀,云服务器1核2G 99元/年 新用户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |