table of Contents
Feign series (01) using the simplest gesture
Spring Cloud Series catalog ( https://www.cnblogs.com/binarylei/p/11563952.html#feign )
For more use cases see Feign Github official website
1. introduced maven dependent
<dependencies>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>10.4.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>10.4.0</version>
</dependency>
</dependencies>
2. Basic Usage
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);
}
public static class Contributor {
String login;
int contributions;
}
public static class Issue {
String title;
String body;
List<String> assignees;
int milestone;
List<String> labels;
}
public class MyApp {
public static void main(String... args) {
GitHub github = Feign.builder()
.decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
// Fetch and print a list of the contributors to this library.
List<Contributor> contributors = github.contributors("OpenFeign", "feign");
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
}
Summary: Feign.target()
actually creating a dynamic agent of GitHub.
3. Feign declarative annotations
Feign via Contract
interface marked on the annotated method resolves MethodMetadata, eventually resolve to the parameter request line Http request, the request line, the request body, and then send the request using HttpClient.
Annotation | Interface Target | Usage |
---|---|---|
@RequestLine |
Method | HttpMethod definitions and UriTemplate. UriTemplate used {} wrapped expression, may be used by automatic injector @Param method parameters on |
@Param |
Parameter | Defined template variables, the value of the variable template way can be used to resolve names using a template injection |
@Headers |
Method, Type | Definition header template variables, use @Param annotation provides injection parameter values. If the annotation is added to the interface classes, all requests will Header carries information corresponding to; if on the method, it will only add to the request on a corresponding method |
@QueryMap |
Parameter | Map or define a POJO, the parameter value will be converted to the query string in the URL |
@HeaderMap |
Parameter | Map ->Http Headers |
@Body |
Method | Defines a Template , similar to a UriTemplate and HeaderTemplate , that uses @Param annotated values to resolve the corresponding Expressions . |
The basic method of using annotations as follows:
public interface FeignService {
// @Headers
@RequestLine("GET /api/documents/{contentType}")
@Headers("Accept: {contentType}")
String getDocumentByType(@Param("contentType") String type);
// @QueryMap: Map or POJO
@RequestLine("GET /find")
V find(@QueryMap Map<String, Object> queryMap);
@RequestLine("GET /find")
V find(@QueryMap CustomPojo customPojo);
// @HeaderMap: Map
@RequestLine("POST /")
void post(@HeaderMap Map<String, Object> headerMap);
// @Body
@RequestLine("POST /")
@Headers("Content-Type: application/xml")
@Body("<login \"user_name\"=\"{user_name}\" \"password\"=\"{password}\"/>")
void xml(@Param("user_name") String user, @Param("password") String password);
@RequestLine("POST /")
@Headers("Content-Type: application/json")
@Body("%7B\"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D")
void json(@Param("user_name") String user, @Param("password") String password);
}
The intentions of recording a little bit every day. Perhaps the content is not important, but the habit is very important!