说明
官方文档地址:https://cloud.spring.io/spring-cloud-openfeign/spring-cloud-openfeign.html
使用
pom
<?xml version="1.0" encoding="UTF-8"?>
<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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nighting</groupId>
<artifactId>feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
FeignApplication
package com.nighting.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
FeignDemoController
package com.nighting.feign.controller;
import com.nighting.feign.restclient.FeignEurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class FeignDemoController {
@Autowired
private FeignEurekaClient feignEurekaClient;
/**
* GET请求
* 从url获取请求参数
*
* @param name
* @return
*/
@GetMapping("/helloGet")
public Map<String, Object> helloGet(String name, Integer age) {
return feignEurekaClient.helloGet(name, age);
}
/**
* POST请求
* 从表单获取请求参数
* multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件
*
* @param name
* @return
*/
@PostMapping("/helloPostFormData")
public Map<String, Object> helloPostFormData(String name, Integer age) {
return feignEurekaClient.helloPostFormData(name, age);
}
/**
* POST请求
* 从表单获取请求参数
* application/x-www-form-urlencoded:只能上传键值对,并且键值对都是间隔分开的。
*
* @param name
* @return
*/
@PostMapping("/helloPostUrlencoded")
public Map<String, Object> helloPostUrlencoded(String name, Integer age) {
return feignEurekaClient.helloPostUrlencoded(name, age);
}
/**
* POST请求
* 获取文本信息,没有key值,只有value
* text
*
* @param name
* @return
*/
@PostMapping("/helloPostText")
public Map<String, Object> helloPostText(@RequestBody String name, Integer age) {
return feignEurekaClient.helloPostText(name, age);
}
/**
* POST请求
* 获取文本信息,没有key值,只有value
* text/plain: 没有格式的文本
*
* @param name
* @return
*/
@PostMapping("/helloPostTextPlain")
public Map<String, Object> helloPostTextPlain(@RequestBody String name, Integer age) {
return feignEurekaClient.helloPostTextPlain(name, age);
}
/**
* POST请求
* json字符串
* application/json
*
* @return
*/
@PostMapping("/helloPostJson")
public Map<String, Object> helloPostJson(@RequestBody Map<String, Object> param, Integer age) {
return feignEurekaClient.helloPostJson(param, age);
}
/**
* POST请求
* application/xml
*
* @return
*/
@PostMapping(value = "/helloPostXml", consumes = {MediaType.APPLICATION_XML_VALUE})
public Map<String, Object> helloPostXml(@RequestBody XmlParam xml, Integer age) {
return feignEurekaClient.helloPostXml(xml, age);
}
/**
* POST请求
* text/xml
*
* @return
*/
@PostMapping(value = "/helloPostTextXml", consumes = MediaType.TEXT_XML_VALUE)
public Map<String, Object> helloPostTextXml(@RequestBody XmlParam xml, Integer age) {
return feignEurekaClient.helloPostTextXml(xml, age);
}
}
FeignEurekaClient
package com.nighting.feign.restclient;
import com.nighting.feign.controller.XmlParam;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
@FeignClient("eureka-client")
public interface FeignEurekaClient {
/**
* GET请求
* 从url获取请求参数
*
* @param name
* @return
*/
@RequestMapping(value = "/helloGet", method = RequestMethod.GET)
Map<String, Object> helloGet(@RequestParam String name, @RequestParam Integer age);
/**
* POST请求
* 从表单获取请求参数
* multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件
*
* @param name
* @return
*/
@RequestMapping(value = "/helloPostFormData", method = RequestMethod.POST)
Map<String, Object> helloPostFormData(@RequestParam String name, @RequestParam Integer age);
/**
* POST请求
* 从表单获取请求参数
* application/x-www-form-urlencoded:只能上传键值对,并且键值对都是间隔分开的。
*
* @param name
* @return
*/
@RequestMapping(value = "/helloPostUrlencoded", method = RequestMethod.POST)
Map<String, Object> helloPostUrlencoded(@RequestParam String name, @RequestParam Integer age);
/**
* POST请求
* 获取文本信息,没有key值,只有value
* text
*
* @param name
* @return
*/
@RequestMapping(value = "/helloPostText", method = RequestMethod.POST)
Map<String, Object> helloPostText(@RequestBody String name, @RequestParam Integer age);
/**
* POST请求
* 获取文本信息,没有key值,只有value
* text/plain: 没有格式的文本
*
* @param name
* @return
*/
@RequestMapping(value = "/helloPostTextPlain", method = RequestMethod.POST)
Map<String, Object> helloPostTextPlain(@RequestBody String name, @RequestParam Integer age);
/**
* POST请求
* json字符串
* application/json
*
* @return
*/
@RequestMapping(value = "/helloPostJson", method = RequestMethod.POST)
Map<String, Object> helloPostJson(@RequestBody Map<String, Object> param, @RequestParam Integer age);
/**
* POST请求
* application/xml
*
* @return
*/
@RequestMapping(value = "/helloPostXml", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE})
Map<String, Object> helloPostXml(@RequestBody XmlParam xml, @RequestParam Integer age);
/**
* POST请求
* text/xml
*
* @return
*/
@RequestMapping(value = "/helloPostTextXml", consumes = MediaType.TEXT_XML_VALUE)
Map<String, Object> helloPostTextXml(@RequestBody XmlParam xml, @RequestParam Integer age);
}
XmlParam
package com.nighting.feign.controller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "xml")
public class XmlParam {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}