spring cloud feign

<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>
@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;
	}
}

  


public interface HelloClient {

    @RequestLine("GET /hello")
    public String hello();
    
    @RequestLine("GET /person/{id}")
    public Person getPerson(@Param("id") Integer id);
}
public static void main(String[] args) {
		HelloClient client = Feign.builder().target(HelloClient.class,
				"http://localhost:8080");
		String result = client.hello();
		System.out.println(result);
	}

  

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());
    }

 

posted @ 2018-03-10 15:44  zfzf1  阅读(116)  评论(0编辑  收藏  举报