@RestController
public class JdkCatController {//OpenFeign动态代理思路
@Autowired
RestTemplate restTemplate;
@GetMapping("getFishA")
public Fish getJdkFish(){
CatOpenFeign o = (CatOpenFeign) Proxy.newProxyInstance(JdkCatController.class.getClassLoader(), new Class[]{CatOpenFeign.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//获取接口名称
GetMapping annotation = method.getAnnotation(GetMapping.class);
String[] paths = annotation.value();
String path = paths[0];
//拿到@FeignClient注解的值,
Class<?> declaringClass = method.getDeclaringClass();
FeignClient annotation1 = declaringClass.getAnnotation(FeignClient.class);
String applicationName = annotation1.value();
//发起请求
String url = "http://" + applicationName + "/" + path;
String result = restTemplate.getForObject(url, String.class);
System.out.println("调用结果:" + result);
return result;
}
});
return o.getFish();
}
}
报错信息
你这个接口实际返回的是Fish对象,然后你在发起http请求的时候返回string怎么行呢,你起码要转成Fish对象返回吧