@Feign通过URI指定请求地址

@FeignClient使用起来非常方便,只需要启用配置即可使用,但是有时候需要请求不通地址的服务,这时可以指定一个URI参数。

配置:

@EnableFeignClients(basePackages = "com.test.openapi.rpc")
@Configuration
public class FeignConfig extends Compact implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}

@FeignClient接口

@FeignClient(
        name = "testCloudService",
        url = "https://api.test.com"
)
@RequestMapping(
        value = "/api",
        headers = {
                "clientId=MY CLIENTID",
                "version=1.0"
        }
)
public interface ITestCloudService {

    @RequestMapping(value = "/call", method = RequestMethod.GET)
    ResultResp<Map<String, Object>> region(URI uri, @RequestHeader("token") String token);

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    ResultResp<Map<String, Object>> login(URI uri, @RequestBody Map body, @RequestHeader("token") String token);
}

如果是GET方法,需要指定一连串参数,可以使用注解@SpringQueryMap如:

@RequestMapping(value = "/user", method = RequestMethod.GET)
ResultResp<ApiUserDevice> userDevice(URI uri, @SpringQueryMap Map queryMap, @RequestHeader("token") String token);

 

 

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |