java开发中遇到过的坑_Springboot 开发过程中遇到坑点 (一)

前言

之前是不会 Springboot 的,使用集中式开发,日常在学习 Java 的 Springboot 开发过程中,比如在前期开发,会容易遇到一些问题,这里做一些记录。

问题

1.Springboot 版本已经包含了 snakeyaml,如果依然 pom 添加 Yaml 库 snakeyaml,启动 Application 会遇到奇怪的错误。

添加了 snakeyaml 后

org.yaml

snakeyaml

1.13

会出现以下错误。

00:05:11.649 [main] DEBUG org.springframework.boot.context.logging.ClasspathLoggingApplicationListener - Application failed to start with classpath: [file:/C:/Program%20Files/Java/jdk1.8.0_111/jre/lib/charsets.jar 省略

00:05:11.653 [main] ERROR org.springframework.boot.SpringApplication - Application run failed

java.lang.NoSuchMethodError: org.yaml.snakeyaml.LoaderOptions.setAllowDuplicateKeys(Z)V

at org.springframework.boot.env.OriginTrackedYamlLoader.createYaml(OriginTrackedYamlLoader.java:66)

at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:162)

at org.springframework.boot.env.OriginTrackedYamlLoader.load(OriginTrackedYamlLoader.java:82)

at org.springframework.boot.env.YamlPropertySourceLoader.load(YamlPropertySourceLoader.java:50)

at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadDocuments(ConfigFileApplicationListener.java:607)

at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:523)

at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadForFileExtension(ConfigFileApplicationListener.java:498)

at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:468)

at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$null$7(ConfigFileApplicationListener.java:447)

at java.lang.Iterable.forEach(Iterable.java:75)

省略

但是编译时不会抛错。

这类错误是 线程上下文类加载器在这个启动 Application 环境事件时失败,这里是 ApplicationFailedEvent 类型,去掉这个 Pom 包就可以恢复。

2.controller 层同个文件里面,添加到 RequestMapping 路由一样,编译时不会错误,但运行 Application 会抛错。

抛错信息这个省略。@RequestMapping注解会将 HTTP,请求映射到 MVC 和 REST 控制器的处理方法上,因为是映射,所以不能有重复的。

一般情况下,@RequestMapping("/routepath") routepath 路由 API 是小写的。

3.一开始写@Value(${xxx1.xxx2})

xxx1 是 Yaml 文件里面的第一层,xxx2 是 Yaml 文件里面的第二层。一开始写的时候容易少写一个 $,启动时会产生抛错。

4.ModelMapper 和 RestTemplate 需要进行配置才能使用。

ModelMapper 不配置就会抛错。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2020-10-28 17:22:04.607 ERROR 8280 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************

APPLICATION FAILED TO START

***************************

Description:

Field modelMapper incom.lilith.ordercenter.service.OrderCenterService required a bean of type 'org.modelmapper.ModelMapper' that could not be found.

The injection point has the following annotations:

- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'org.modelmapper.ModelMapper' inyour configuration.

需要配置

@Bean

public ModelMapper modelMapper() {

return new ModelMapper();

}

Springboot 版本 2.x 以上需要配置 RestTemplate。

@Bean

public RestTemplate restTemplate(RestTemplateBuilder builder) {

return builder.build();

}

PS:配置@Bean是指 spring 阶段是 Xml 里面的 bean,这里是指@Configuration启动容器 +@Bean注册 Bean。

@Configuration标注在类上,等于是 spring 的 xml 配置文件中的,配置在 spring 容器 (Application Context)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值