使用Spring Boot WebClient调用Dummy API后Postman无返回数据求助
服务层接口逻辑错误
你在EmpService接口上加了@Service注解是无效的,接口不能被Spring实例化。同时接口内的getAllEmployees被定义为静态方法,实现类的同签名方法不属于重写,调用时会直接执行接口的静态方法返回null。
修复:删除接口上的@Service注解,把方法改为普通抽象方法:public interface EmpService { List<Employee> getAllEmployees(); }WebClient依赖注入失败
你定义的SimpleWebClient是普通成员方法,不是构造方法,Spring不会自动调用该方法注入你配置的WebClientBean,会导致webClient为空引发空指针。
修复:把该方法改为构造方法,加上@Autowired注解:@Autowired public SimpleWebClient(WebClient webClient) { this.webClient = webClient; }响应式流未触发执行
你直接把Flux<Employee>放到Model里,Flux是冷流,没有订阅操作不会真正发起HTTP请求拉取数据。如果是用传统同步视图,需要阻塞获取结果:
修复:控制器里修改赋值逻辑:model.addAttribute("listemps", webClientApp.webClientBuilder().collectList().block());实体类反序列化配置错误
你加了@JsonRootName注解,但默认Jackson没有开启根节点解包配置,反序列化会失败。另外employeeImage字段定义为Blob类型,而Dummy API返回的是图片URL字符串,类型不匹配会报错。
修复:- 把
employeeImage字段类型改为String - 在application.yml/properties里添加配置开启根节点解包:
spring: jackson: deserialization: UNWRAP_ROOT_VALUE: true- 把
注解使用冗余错误
ServiceImpl上同时加了@Repository和@Service属于冗余注解,@ComponentScan放在业务实现类上不会生效,该注解应该标注在启动类上。接口返回值不符合Postman测试预期
你写的/consume接口返回的是视图名index,会渲染前端页面,Postman调用不会返回JSON格式的员工数据。如果要测试接口返回值,可以单独写一个接口直接返回员工数据:@GetMapping("/emps") public Flux<Employee> getEmps() { return webClientApp.webClientBuilder(); }
内容的提问来源于stack exchange,提问作者Julie Gladden

