Java教程

spring boot项目中踩到的坑

本文主要是介绍spring boot项目中踩到的坑,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

在使用springboot的过程中,遇到过各种各样的小问题。 下面我将我所遇到过得问题做一个小的总结,算是做一个记录。

1.

导包错误导致的启动程序时报错:****Could not autowire. No beans of 'xxxx' type found(或者required a bean of type ‘XXX’ that could not be found)的错误提示

曾经导错的包:import com.alibaba.dubbo.config.annotation.Service;

正确的service包: import org.springframework.stereotype.Service;

正确的mapper包:import org.apache.ibatis.annotations.Mapper;

2.

在application.yml配置文件中使用@ConfigurationProperties(prefix = "user")配置dao对象的默认值时,上方会提示:Spring Boot Configuration Annotation Processor not found in claspath,虽然不影响程序的正常执行,但是在配置文件中为dao对象的属性赋值时,没有自动补全的提示。
image

此时可以在pom文件中增加依赖关系:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

此时在配置文件中编辑dao类的属性时,会自动弹出提示,方便键入,避免出错。

3.

pom.xml文件中使用 pagehelper时,启动项目报错,显示依赖循环。
image

查找资料后了解到是pagerhelper的版本和spring boot的版本冲突导致的,可以将spring boot的版本切换到兼容的release版本,此时项目能正常启动。

4.

springboot Invalid character found in the request target 特殊字符传参报错

产生原因: tomcat8.0以上版本遵从RFC规范添加了对Url的特殊字符的限制,url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~四个特殊字符以及保留字符( ! * ’ ( ) ; : @ & = + $ , / ? # [ ] ) (26*2+10+4+18=84)这84个字符,请求中出现了{}大括号或者[],所以tomcat报错。

问题解决:在@SpringBootApplication启动类中增加下述代码

@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "|{}[]\\"));
return factory;
}

问题得到解决。

这篇关于spring boot项目中踩到的坑的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!