Spring系列学习之Spring Cloud OpenFeign声明性HTTP REST客户端

英文原文:https://spring.io/projects/spring-cloud-openfeign

目录

概述

特性

入门

快速开始

学习

文档

示例


概述

该项目通过自动配置和Spring环境以及其他Spring编程模型习惯用法为Spring Boot应用程序提供OpenFeign集成。

特性

  •      声明性REST客户端:Feign创建使用JAX-RS或Spring MVC注释修饰的接口的动态实现


入门



@SpringBootApplication
@EnableFeignClients
public class WebApplication {

	public static void main(String[] args) {
		SpringApplication.run(WebApplication.class, args);
	}

	@FeignClient("name")
	static interface NameService {
		@RequestMapping("/")
		public String getName();
	}
}

https://github.com/spring-cloud-samples/feign-eureka/blob/master/client/src/main/java/demo/HelloClientApplication.java 

扫描二维码关注公众号,回复: 4650928 查看本文章
package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * @author Spencer Gibb
 */
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class HelloClientApplication {
	@Autowired
	HelloClient client;

	@RequestMapping("/")
	public String hello() {
		return client.hello();
	}

	public static void main(String[] args) {
		SpringApplication.run(HelloClientApplication.class, args);
	}

	@FeignClient("HelloServer")
	interface HelloClient {
		@RequestMapping(value = "/", method = GET)
		String hello();
	}
}

快速开始

使用Spring Initializr引导您的应用程序。

学习

文档

每个Spring项目都有自己的; 它详细解释了如何使用项目功能以及使用它们可以实现的功能。

2.1.0 RC3 PRE CURRENT Reference Doc. API Doc.
2.0.3 SNAPSHOT CURRENT Reference Doc. API Doc.
2.0.2 CURRENT GA Reference Doc. API Doc.

示例

尝试一些例子:

猜你喜欢

转载自blog.csdn.net/boonya/article/details/85254044