在SpringCloud中通常使用OpenFeign来封装微服务接口,有如下两种方式:
1.准备config
@Configuration
public class FeginConfig {
@Bean
public Contract feignConfiguration() {
return new feign.Contract.Default();
}
}
2.申明接口
@FeignClient(name = "EMPLOYEE", configuration = FeginConfig.class)
public interface EmployeeService1 {
@RequestLine("POST /employee/add")
@Headers({"Content-Type: application/json", "Accept: application/json"})
CommonResult add(TblEmployee employee);
@RequestLine("POST /employee/addBatch")
@Headers({"Content-Type: application/json", "Accept: application/json"})
CommonResult addBatch(List<TblEmployee> employee);
}
此种方式比较简单,直接定义接口
@FeignClient("EMPLOYEE")
public interface EmployeeService {
//@PostMapping(value = "/employee/add")
@RequestMapping(value="/employee/add",consumes = "application/json",method = RequestMethod.POST)
CommonResult add(TblEmployee employee);
@PostMapping(value = "/employee/addBatch")
//@RequestMapping(value="/employee/addBatch",consumes = "application/json",method = RequestMethod.POST)
CommonResult addBatch(List<TblEmployee> employee);
}
1.在使用RequestMapping/PostMapping/GetMapping时,如果注入了Contract,且实例为new feign.Contract.Default()。就会出现如下异常:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘webApp‘: Unsatisfied dependency expressed through field ‘employeeService‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘com.laowu.service.EmployeeService‘: FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method EmployeeService#add(TblEmployee) not annotated with HTTP method type (ex. GET, POST)
Warnings:
- Class EmployeeService has annotations [FeignClient] that are not used by contract Default
- Method add has an annotation RequestMapping that is not used by contract Default
有两种解决办法:
//@Configuration
public class FeginConfig {
//@Bean
public Contract feignConfiguration() {
return new feign.Contract.Default();
}
}
@Configuration
public class FeginConfig {
@Bean
public Contract feignConfiguration() {
return new SpringMvcContract();
}
}
原文:https://www.cnblogs.com/wugang/p/14477803.html