解决RestTemplate加@Autowired注入不了的问题
作者:梦想注定是孤独的旅行 时间:2022-07-14 03:00:48
RestTemplate加@Autowired注入不了
1、在启动类加入
如图箭头所示代码:
然后在进行@Autowired发现不报错了。
完美解决
SpringBoot 如何注入RestTemplate
创建一个文件夹 ,我这边习惯于创建config文件夹
将下面的一段代码放到里面
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RedisConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
RestTemplate restTemplate = builder.build();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
return restTemplate;
}
}
之后使用
@Autowired
private RestTemplate restTemplate;
直接正常使用就可以
String url = "http://localhost:8080/findById?id=1";//请求的地址
String request = restTemplate.getForObject(url, String.class);
来源:https://blog.csdn.net/qq_40667143/article/details/83444822
标签:RestTemplate,@Autowired,注入
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用Android中BitmapShader制作自带边框的圆形头像
2021-12-25 20:09:11
![](https://img.aspxhome.com/file/2023/0/92580_0s.png)
Java多线程编程综合案例详解
2023-12-09 18:13:25
![](https://img.aspxhome.com/file/2023/7/68517_0s.png)
nacos中的配置使用@Value注解获取不到值的原因及解决方案
2023-11-29 13:43:00
![](https://img.aspxhome.com/file/2023/9/60769_0s.jpg)
C# .NET 中的缓存实现详情
2023-05-30 18:35:12
C#使用Selenium+PhantomJS抓取数据
2022-09-13 23:18:02
![](https://img.aspxhome.com/file/2023/4/96354_0s.png)
Nacos docker单机模式部署实现过程详解
2021-05-27 01:07:28
![](https://img.aspxhome.com/file/2023/9/95939_0s.png)
Java数据结构之单链表详解
2023-11-04 17:02:20
![](https://img.aspxhome.com/file/2023/9/58719_0s.png)
Android EditText限制输入字符的方法总结
2023-04-22 03:16:24
![](https://img.aspxhome.com/file/2023/6/92606_0s.png)
C#中的那些警告该如何去除(完全去除C#警告)
2023-09-07 20:55:53
![](https://img.aspxhome.com/file/2023/1/76381_0s.png)
Java中static关键字的作用和用法详细介绍
2022-07-05 08:33:53
Java之InputStreamReader类的实现
2023-09-03 01:28:17
springboot拦截器过滤token,并返回结果及异常处理操作
2023-02-19 11:45:50
C#禁止textbox复制、粘贴、剪切及鼠标右键的方法
2022-08-21 09:13:40
Java API文档的使用方法详解
2022-06-15 20:12:03
![](https://img.aspxhome.com/file/2023/5/85915_0s.png)
教你如何用好 Java 中的枚举
2022-11-03 10:31:21
java 创建线程的几种方式
2023-10-29 19:40:16
![](https://img.aspxhome.com/file/2023/5/58695_0s.png)
Spring超详细讲解IOC与解耦合
2023-06-16 12:32:50
![](https://img.aspxhome.com/file/2023/3/66713_0s.png)
Spring Boot实现分布式系统中的服务发现和注册(最新推荐)
2022-07-10 03:50:23
Java之SSM中bean相关知识汇总案例讲解
2021-11-10 06:16:26
![](https://img.aspxhome.com/file/2023/9/68689_0s.jpg)
SpringBoot实战之高效使用枚举参数(原理篇)案例详解
2022-02-10 23:54:23
![](https://img.aspxhome.com/file/2023/5/77055_0s.jpg)