1. 错误写法
@GetMapping("/queryList")
R queryList( MusicVo musicVo );
2. 异常信息
Request method 'POST' not supported
3. 原因
OpenFeign@QueryMap注解支持将 POJO 用作 GET 参数映射。不幸的是,默认的 OpenFeign QueryMap 注解与 Spring 不兼容,因为它缺少value属性。
Spring Cloud OpenFeign 提供了等效的@SpringQueryMap注解,用于将 POJO 或 Map 参数注解为查询参数映射。
feign get方式无法解析对象参数,如果传参是一个类对象,框架就需要把这个类对象解析成query参数。但是直接在方法中写传参框架不会自动把类对象解析成query参数。@SpringQueryMap,此注解的作用就是把pojo解析成k=v&k=v的query参数格式。
3. 用 @SpringQueryMap 修饰 传参对象
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
@FeignClient( value = "system")
public interface SystemFeignApi {
@GetMapping("/queryList")
R queryList(@SpringQueryMap MusicVo musicVo );
}
4. 调用 异常解除
@RestController
@RequestMapping("/music")
public class MusicController {
@Resource
private SystemFeignApi systemFeignApi ;
@GetMapping("/queryList")
public Object queryList(MusicVo musicVo ) {
if (musicVo ==null) {
return ResponseUtil.fail(-1, "参数不能为空!");
}
return systemFeignApi.queryList(musicVo);
}
}