1: When multiple clients are used, the feign interface is extracted into the public jar. At this time, the client's startup class needs to scan the package where the feign is located in the jar. It must be registered in spring and feign at the same time. Otherwise, it will be reported when starting: "Consider defining a bean of type '******Feign' in your configuration."
@SpringBootApplication@EnableTransactionManagement@EnableDiscoveryClient@ComponentScan(basePackages={"com.lcamtech.aidis.feign","com.lcamtech.aids.dts"})@EnableFeignClients(basePackages = {"com.lcamtech.aidis.feign"})@EnableCaching@MapperScan(basePackages = "com.lcamtech.aids.dts.mapper")public class Application extends SpringBootServletInitializer{ public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Key points:
@ComponentScan(basePackages={"com.lcamtech.aidis.feign","com.lcamtech.aids.dts"})@EnableFeignClients(basePackages = {"com.lcamtech.aidis.feign"})The aidis package is a jar containing feign. At this time, @ComponentScan also needs to scan the package of this project at the same time.
2: When using Fegin to pass the value, GET becomes POST
@FeignClient(value = "SERVICE-NAME")public interface UserAccountFeign { @RequestMapping(value = "/ac/exist", method = RequestMethod.GET) public BaseResult isExist(@RequestParam("mobile") String mobile);}When feign is passed, the data will be placed in the RequestBody by default, so it will cause the default POST request (it is useless to write GET in time. At this time, @RequestParam needs to be declared in the parameter list to make normal GET requests.
3: When feign request returns a complex object
like:
public class Result{ private string code; private string message; private Object data; //get/set}Problem description: When the request returns an object of Result, the data value inside the object will become a linkedHashMap and will not be converted into the corresponding class object. If the transfer is directly forced, the type error will be reported.
Solution 1: Simple conversion
/** * @Description: Convert data to the corresponding container* @param bean * @param clazz * @return * @throws * @author SunF * @date 2018/6/20 10:28 */ public static <T> T convertValue(Object bean, Class<T> clazz){ try{ ObjectMapper mapper = new ObjectMapper(); return mapper.convertValue(bean, clazz); }catch(Exception e){ log.error("Error conversion: BeanUtil.convertValue() --->" + e.getMessage()); return null; } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.