报错找不到 TestRestTemplate

简介: 报错找不到 TestRestTemplate


脑子——认识的能力——是像肌肉一样,靠练习、锻炼而培养起来的。——高尔基


今天遇到个问题:


1
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate.


原来是SpringBootTest没加webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT

加上就好了


import jakarta.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
/**
 * XxxControllerTest
 *
 * @author achao@apache.org
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // 确保使用RANDOM_PORT
public class XxxControllerTest {
    @Resource
    private TestRestTemplate restTemplate;
    @Test
    void generateTokenTest() {
        var res = restTemplate.getForEntity("https://xxx.xx", Res.class).getBody();
        Assertions.assertNotNull(res);
    }
}


相关文章
|
消息中间件 NoSQL Java
springboot redis 实现消息队列
springboot redis 实现消息队列
1370 1
|
12月前
|
JSON 前端开发 Java
Spring Boot返回Json数据及数据封装
Spring Boot中,@RestController注解可自动将数据转为JSON格式,其底层默认使用Jackson框架。该注解等价于@Controller与@ResponseBody结合,简化开发。Jackson支持对象、List、Map等常见数据结构的JSON转换,并可通过配置自定义null值处理方式,如将null转为空字符串,提升接口返回的规范性与前端友好性。(238字)
|
canal 数据可视化 关系型数据库
2025年5大国产ETL工具横向评测
在企业数据管理中,ETL工具成为整合分散数据的关键。本文介绍了五款主流国产ETL工具:FineDataLink(低代码、功能全面)、Kettle(开源易用)、DataX(高速同步)、Canal(MySQL实时增量处理)和StreamSets(可视化强),帮助用户根据需求选择最合适的工具,提升数据效率与业务价值。
2751 56
|
12月前
|
JSON Java 数据格式
Object转JSON
该方法将Java对象转换为JSON字符串,使用JSONArray.fromObject实现序列化,并输出转换结果日志,最后返回生成的JSON字符串。适用于对象数据的JSON格式化处理。
|
前端开发
ajax设置header
ajax设置header
480 0
|
Java Spring
No qualifying bean of type 'XXXXX' available
该文档描述了Spring框架中`org.springframework.beans.factory.NoSuchBeanDefinitionException`异常的处理方法,该异常是因为无法找到类型为`com.weblog.auth.mapper.UserMapper`的bean导致。解决办法包括在对应的Mapper上添加`@Mapper`注解,并在启动类上添加`@MapperScan`注解以扫描包含Mapper接口的文件夹。
1669 8
|
前端开发 JavaScript Java
Spring Boot中的数据校验
Spring Boot中的数据校验
|
Java 容器
RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out问题处理
讲述RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out处理方案
|
SQL Java 数据库连接
Mybatis:通过on duplicate key update实现批量插入或更新
Mybatis:通过on duplicate key update实现批量插入或更新
|
Java 数据库连接 API
【Java笔记+踩坑】Spring Data JPA
从常用注解、实体类和各层编写方法入手,详细介绍JPA框架在增删改查等方面的基本用法,以及填充用户名日期、分页查询等高级用法。
【Java笔记+踩坑】Spring Data JPA