在SpringCloud中使用Feign的兩種方式


在SpringCloud中通常使用OpenFeign來封裝微服務接口,有如下兩種方式:

一、RequestLine注解

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);
}

二、RequestMapping/PostMapping/GetMapping注解

此種方式比較簡單,直接定義接口

@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);
}

三、使用

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient  

@EnableFeignClients
@RestController
public class WebApp {
    public static void main(String[] args) {
        SpringApplication.run(WebApp.class, args);
    }

    @Autowired(required = false)
    private Contract cc;

    @Autowired(required = false)
    private EmployeeService employeeService;

    @Autowired(required = false)
    private EmployeeService1 employeeService1;

    @PostMapping("add")
    public CommonResult add(@RequestBody TblEmployee employee) {
        return employeeService.add(employee);
    }

    @PostMapping("addBatch")
    public CommonResult addBatch(@RequestBody List<TblEmployee> employees) {
        return employeeService.addBatch(employees);
    }

    @GetMapping("/test")
    public String test() {
        Class<? extends Contract> aClass = cc.getClass();
        return aClass == null ? "" : aClass.getName();
    }
}

常見問題

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

有兩種解決辦法:

1.不注入Contract

//@Configuration
public class FeginConfig {
    //@Bean
    public Contract feignConfiguration() {
       return new feign.Contract.Default();
    }
}

2.注入SpringMvcContract。

@Configuration
public class FeginConfig {
    @Bean
    public Contract feignConfiguration() {
        return new SpringMvcContract();
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM