For Testing
@Spy
@Captor
Used to capture the arguments which are passed to a Mock
For REST
@PatchMapping
The PatchMapping is (just) an alias for @RequestMapping(method=Patch)
And PATCH is meant for a (partial) update, just like PUT but PUT is meant for updating the entire object, better said: to store an object on a url (with an id) And PATCH is just to update some of the properties of the stored object
@ControllerAdvice
The @ControllerAdvice annotation was first introduced in Spring 3.2. It allows you to handle exceptions across the whole application, not just to an individual controller. You can think of it as an interceptor of exceptions thrown by methods annotated with @RequestMapping or one of the shortcuts.
This post discuss the advantages and points to consider when a controller advice is introduced into a REST API written in Spring through a simple application.
Topic: Feign
Introduction
In the DataServiceClient class the Feign library is used
In this section, we will introduce and explain Feign, a declarative HTTP client developed by Netflix.
What you will learn
-
Why and When you want to used the Feign library
-
What Feign is
-
How to use it
Why and When: Feign
You want to use Feign when you are building a REST client
What: Feign
Feign aims at simplifying HTTP API clients. Simply put, the developer needs only to declare and annotate an interface while the actual implementation will be provisioned at runtime. (kind of comparable as the CrudRepository interface of Spring Boot)
How: Feign
Setup: Feign
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>9.3.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>9.3.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
<version>9.3.1</version>
</dependency>
We created an empty Maven project which has no Spring Boot dependencies in it!!! |
Besides the feign-core dependency (which is also pulled in), we’ll use a few plugins, especially: feign-okhttp for internally using Square’s OkHttp client to make requests, feign-gson for using Google’s GSON as JSON processor and feign-slf4j for using the Simple Logging Facade to log requests.
To actually get some log output, you’ll need your favorite, SLF4J-supported logger implementation on your classpath.
Before we continue to create our client interface, we’ll set up a Book model for holding our data ⇒
public class Book {
private String isbn;
private String author;
private String title;
private String synopsis;
private String language;
// getters and setters omitted
}
Now we’ll define our Feign client.
We will use the @RequestLine annotation to specify the HTTP verb and a path part as argument, and the parameters will be modeled using the @Param annotation:
public interface BookClient {
@RequestLine("GET /{isbn}")
Book findByIsbn(@Param("isbn") String isbn);
@RequestLine("GET")
List<Book> findAll();
@RequestLine("POST")
@Headers("Content-Type: application/json")
void create(Book book);
}
That’s all! Now we’ll use the Feign.builder() to configure our interface-based client. The actual implementation will be provisioned at runtime ⇒
BookClient bookClient = Feign.builder()
.client(new OkHttpClient())
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
.logger(new Slf4jLogger(BookClient.class))
.logLevel(Logger.Level.FULL)
.target(BookClient.class, "http://localhost:8081/api/books");
Assignment: Feign
To learn working with the Feigh library
During this assignment you will create a Feighn client for an (existing) REST service
-
The code above
-
And this link: https://www.baeldung.com/intro-to-feign
-
I create a Feign client
-
And invoke the main method
Bonus Assignment ???
Also add GET for id Also add POST
Follow-up: Feign
Below you find for this topic some extra resources to watch after the week the topic Feign is introduced during the training
Lombok
@Singular
Used with the @Builder annotation to create a singular way to add one entry for the annotated List e.g.
@Singular private final List<String> interests;
Person person = Person.builder()
.givenName("Aaron")
.additionalName("A")
.familyName("Aardvark")
.interest("history") //<=
.interest("sport") // <=
.build();