Feign 的简单使用及传参方式

3,515 阅读4分钟

Feign 的简单使用及传参方式

Author Avatar zdRan 5月 17, 2018

Feign 的简单使用及传参方式

0. Feign 简介

Feign 是简化Java HTTP客户端开发的工具。它使用注解的方式将HTTP的URL封装成接口,每个URL对应一个接口,大大简化了HTTP客户端的开发。

1. 添加依赖

1<dependency>
2    <groupId>io.github.openfeign</groupId>
3    <artifactId>feign-core</artifactId>
4</dependency>
5<dependency>
6    <groupId>io.github.openfeign</groupId>
7    <artifactId>feign-jackson</artifactId>
8</dependency>
9<dependency>
10    <groupId>io.github.openfeign</groupId>
11    <artifactId>feign-httpclient</artifactId>
12</dependency>
13<dependency>
14    <groupId>org.apache.httpcomponents</groupId>
15    <artifactId>httpclient</artifactId>
16</dependency>

2. 定义API

1
2import feign.Headers;
3import feign.Param;
4import feign.RequestLine;
5
6import java.util.List;
7
8/**
9 * Create by zdran@gmail.com on 2018/3/20
10 *
11 */
12//Headers 注解:配置请求头信息
13@Headers({"Accept: application/json", "Content-Type: application/json"})
14public interface UserApi {
15    /**
16     * RequestLine 注解:请求的方法与url,这里需要注意的是url写的是与Controller里的地址,不是完整的url地址。
17     * GET 请求方法,遵循RESTful风格
18     * @return
19     */
20    @RequestLine("GET /user/all")
21    List<User> getAllUser();
22
23    /**
24     * restful方式传参
25     * @param name
26     * @return
27     */
28    @RequestLine("GET /user/{name}")
29    User getByName(@Param("name") String name);
30
31    /**
32     * url方式传参数
33     * @param id
34     * @return
35     */
36    @RequestLine("GET /user/id?id={id}")
37    User getById(@Param("id") String id);
38
39    /**
40     * post 传参,传复杂类型
41     * @param user
42     */
43    @RequestLine("POST /user/add")
44    void addUser(User user);
45}

3. 定义实现API的controller

1import org.springframework.stereotype.Controller;
2import org.springframework.web.bind.annotation.*;
3
4import javax.validation.Valid;
5import java.util.ArrayList;
6import java.util.List;
7
8/**
9 * Create by zdran@gmail.com on 2018/3/20
10 *
11 */
12@RestController
13@RequestMapping(value = "/user", produces = "application/json")
14public class UserController {
15
16    @GetMapping(value = "/all")
17    List<User> getAll(){
18        List<User> users = new ArrayList<>();
19        User user = new User();
20        user.setName("获取所有用户");
21        users.add(user);
22        return users;
23    }
24    @GetMapping(value = "/{name}")
25    User getByName(@PathVariable String name){
26        User user = new User();
27        user.setName("获取用户:"+name);
28        return user;
29    }
30    @GetMapping(value = "/id")
31    User getById(String id){
32        User user = new User();
33        user.setName("获取用户:"+id);
34        return user;
35    }
36    @PostMapping(value = "/add")
37    void addUser(@RequestBody User user){
38
39    }
40}

4. 使用

1import feign.Feign;
2import feign.httpclient.ApacheHttpClient;
3import feign.jackson.JacksonDecoder;
4import feign.jackson.JacksonEncoder;
5import org.springframework.stereotype.Controller;
6import org.springframework.web.bind.annotation.GetMapping;
7import org.springframework.web.bind.annotation.PathVariable;
8
9/**
10 * Create by zdran@gmail.com on 2018/3/20
11 *
12 */
13@Controller
14public class ClientController {
15    private static final String apiBaseUrl = "http://localhost:8080/feign";
16    UserApi userApi = Feign.builder()
17            .client(new ApacheHttpClient())
18            .encoder(new JacksonEncoder())
19            .decoder(new JacksonDecoder())
20            .target(UserApi.class, apiBaseUrl);
21
22    @GetMapping(value = "/client/user/{name}")
23    public User getUserInfo(@PathVariable String name){
24        return userApi.getByName(name);
25    }
26
27}

转载请注明出处
本文链接:zdran.com/20180517.ht…

expand_less