springboot使用feign访问api

本文主要介绍如何在springboot项目中使用feign来访问cnode中文公共api接口


一、fein简介

Feign使得 Java HTTP 客户端编写更方便。Feign 灵感来源于Retrofit、JAXRS-2.0和WebSocket。Feign最初是为了降低统一绑

定Denominator到HTTP API的复杂度,不区分是否支持Restful。Feign旨在通过最少的资源和代码来实现和HTTP API的连接。通过可

定制的解码器和错误处理,可以编写任意的HTTP API。

github:https://github.com/OpenFeign/feign

二、feign使用步骤

1、增加pom依赖

<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-core</artifactId>
   <version>9.5.0</version>
</dependency>
<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-slf4j</artifactId>
   <version>9.5.0</version>
</dependency>
<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-hystrix</artifactId>
   <version>9.5.0</version>
</dependency>
<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-jackson</artifactId>
   <version>9.5.0</version>
</dependency>

2、application.properties增加基础url

application.cnode.url=https://cnodejs.org


3、写访问api的接口

@Headers("Content-Type:application/json")
public interface NodeClient {
    @RequestLine("GET /api/v1/topics?page={page}&tab={tab}&limit={limit}&mdrender={mdrender}")
    CnodeTopicsResponse getTopics(@Param("page") int page,@Param("tab") String tab,
                                  @Param("limit")int limit,@Param("mdrender") String mdrender);
}
4、使用注解,配置NodeClient,并设置等候响应时间

@Configuration
public class NodeConfig {
    @Value("${application.cnode.url}")
    private String baseUrl;

    @Bean
    NodeClient nodeClient() throws InterruptedException {
        return HystrixFeign.builder()
                .decoder(new JacksonDecoder())
                .encoder(new JacksonEncoder())
                .setterFactory((target, method) ->
                        new SetterFactory.Default().create(target, method).
                                andCommandPropertiesDefaults(HystrixCommandProperties.defaultSetter().
                                        withExecutionTimeoutInMilliseconds(10000)))
                .target(NodeClient.class, this.baseUrl);
    }
}
5、配置controller

@RestController
@CrossOrigin
public class NodeController {
    private NodeClient nodeClient;
    @Autowired
    public NodeController(final NodeClient nodeClient) {
        this.nodeClient = nodeClient;
    }

    @RequestMapping(value = "/node",method = RequestMethod.GET)
    public CnodeTopicsResponse get(CnodeTopicsRequest cnodeTopicsRequest){
        return nodeClient.getTopics(cnodeTopicsRequest.getPage(),
                cnodeTopicsRequest.getTab(),
                cnodeTopicsRequest.getLimit(),
                cnodeTopicsRequest.getMdrender());
    }

}

6、其中CnodeTopicsRequest和CnodeTopicsResponse如下

public class CnodeTopicsRequest {
    private Integer page;
    private String tab;
    private Integer limit;
    private String mdrender;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String getTab() {
        return tab;
    }

    public void setTab(String tab) {
        this.tab = tab;
    }


    public String getMdrender() {
        return mdrender;
    }

    public void setMdrender(String mdrender) {
        this.mdrender = mdrender;
    }
}
public class CnodeTopicsResponse {
    private boolean success;
    private List<CnodeArticle> data;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public List<CnodeArticle> getData() {
        return data;
    }

    public void setData(List<CnodeArticle> data) {
        this.data = data;
    }
}
6、然后启动application,访问链接: http://localhost:8080/node?page=1&limit=100&tab=&mdrender=false

猜你喜欢

转载自blog.csdn.net/artaganan8/article/details/79970551