第一个Feign程序

一 Feign介绍
被集成到Spring Cloud Netflix模块
它是Github上面的一个开源项目
二 新建一个服务端项目first-boot
1 控制器MyController
package org.crazyit.cloud;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello World";
    }
}
2 控制器MyRestController
package org.crazyit.cloud;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Person getPerson(@PathVariable Integer id) {
        Person p = new Person();
        p.setId(id);
        p.setName("angus");
        p.setAge(30);
        return p;
    }
}
3 启动项目first-boot
二 使用CSF调用REST服务
1 新建项目cxf-client
2 加入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.crazyit.cloud</groupId>
      <artifactId>cxf-client</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <dependencies>
            <dependency>
                  <groupId>org.apache.cxf</groupId>
                  <artifactId>cxf-core</artifactId>
                  <version>3.1.10</version>
            </dependency>
            <dependency>
                  <groupId>org.apache.cxf</groupId>
                  <artifactId>cxf-rt-rs-client</artifactId>
                  <version>3.1.10</version>
            </dependency>
      </dependencies>
      
</project>
3 新建测试类
package org.crazyit.cloud;

import java.io.InputStream;

import javax.ws.rs.core.Response;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.jaxrs.client.WebClient;

public class CxfClient {

    public static void main(String[] args) throws Exception {
        // 创建WebClient
        WebClient client = WebClient.create("http://localhost:8080/person/1");
        // 获取响应
        Response response = client.get();
        // 获取响应内容
        InputStream ent = (InputStream) response.getEntity();
        String content = IOUtils.readStringFromStream(ent);
        // 输出字符串
        System.out.println(content);
    }

}
4 启动测试类,输出
{"id":1,"name":"angus","age":30}
三 新建feign-client项目
1 新增相关依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.crazyit.cloud</groupId>
      <artifactId>feign-client</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <dependencies>
            <dependency>
                  <groupId>io.github.openfeign</groupId>
                  <artifactId>feign-core</artifactId>
                  <version>9.5.0</version>
            </dependency>
            <dependency>
                  <groupId>io.github.openfeign</groupId>
                  <artifactId>feign-gson</artifactId>
                  <version>9.5.0</version>
            </dependency>           
      </dependencies>
</project>
2 新建接口HelloClient
package org.crazyit.cloud;

import feign.Param;
import feign.RequestLine;

public interface HelloClient {

    @RequestLine("GET /hello")
    public String hello();
    
    @RequestLine("GET /person/{id}")
    public Person getPerson(@Param("id") Integer id);
}
3 新建测试类
package org.crazyit.cloud;
import feign.Feign;
public class HelloMain {
      public static void main(String[] args) {
            HelloClient client = Feign.builder().target(HelloClient.class,
                        "http://localhost:8080");
            String result = client.hello();
            System.out.println(result);
      }
}
4 启动测试类,测试输出
Hello World
5 新建实体类Person
package org.crazyit.cloud;
public class Person {
      private Integer id;
      private String name;
      private String age;
      public Integer getId() {
            return id;
      }
      public void setId(Integer id) {
            this.id = id;
      }
      public String getName() {
            return name;
      }
      public void setName(String name) {
            this.name = name;
      }
      public String getAge() {
            return age;
      }
      public void setAge(String age) {
            this.age = age;
      }
      
}
6 新建测试类
package org.crazyit.cloud;

import feign.Feign;
import feign.gson.GsonDecoder;

public class PersonMain {

    public static void main(String[] args) {
        HelloClient client = Feign.builder()
                .decoder(new GsonDecoder())
                .target(HelloClient.class,
                "http://localhost:8080");
        Person p = client.getPerson(1);
        System.out.println(p.getId());
        System.out.println(p.getName());
        System.out.println(p.getAge());
    }

}
7 启动测试类,测试输出
1
angus
30


猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/81056564
今日推荐