Bael 5296: Added Http Request Header using the Feign Client (#12201)
* Implemented cassandra batch query * Added netty version param * Reformatted correctly * Reformatted correctly * Reformatted correctly * Formatting fix resolved * Formatting fix resolved * Removed unused method * Refactored method for better readability * tab spaces corrected * Added http headers in feign * Updated code * Updated code * Removed unused code * Removed unused logger code * Implemented Interceptor and logging related code review * Added AuthService Code * Removed toString method * Removed unnecessary declaration * Removed new line * Added feign headers log as well * Moved to Authorisation package for better naming * spaces removed * @Override included Co-authored-by: saikat chakraborty <saikat.chakraborty@tesco.com>
This commit is contained in:
parent
4abe624ea6
commit
4b36bbf0b7
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.feign.clients.builder;
|
||||
|
||||
import com.baeldung.feign.header.authorisation.ApiAuthorisationService;
|
||||
import com.baeldung.feign.header.interceptor.AuthRequestInterceptor;
|
||||
|
||||
|
||||
import feign.Feign;
|
||||
import feign.Logger;
|
||||
import feign.gson.GsonDecoder;
|
||||
import feign.gson.GsonEncoder;
|
||||
import feign.slf4j.Slf4jLogger;
|
||||
|
||||
|
||||
public class BookFeignClientBuilder {
|
||||
|
||||
public static <T> T createClient(Class<T> type, String uri) {
|
||||
return Feign.builder()
|
||||
.encoder(new GsonEncoder())
|
||||
.decoder(new GsonDecoder())
|
||||
.logger(new Slf4jLogger(type))
|
||||
.logLevel(Logger.Level.HEADERS)
|
||||
.target(type, uri);
|
||||
}
|
||||
|
||||
public static <T> T createClientWithInterceptor(Class<T> type, String uri) {
|
||||
return Feign.builder()
|
||||
.requestInterceptor(new AuthRequestInterceptor(new ApiAuthorisationService()))
|
||||
.encoder(new GsonEncoder())
|
||||
.decoder(new GsonDecoder())
|
||||
.logger(new Slf4jLogger(type))
|
||||
.logLevel(Logger.Level.HEADERS)
|
||||
.target(type, uri);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.feign.clients.header.dynamicheader;
|
||||
|
||||
import com.baeldung.feign.models.Book;
|
||||
|
||||
import feign.HeaderMap;
|
||||
import feign.Headers;
|
||||
import feign.RequestLine;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
public interface BookClient {
|
||||
|
||||
@RequestLine("POST")
|
||||
void create(@HeaderMap Map<String, Object> headers, Book book);
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.feign.clients.header.staticheader;
|
||||
|
||||
import com.baeldung.feign.models.Book;
|
||||
import com.baeldung.feign.models.BookResource;
|
||||
import feign.Headers;
|
||||
import feign.Param;
|
||||
import feign.RequestLine;
|
||||
|
||||
@Headers("Accept-Language: en-US")
|
||||
public interface BookClient {
|
||||
|
||||
@RequestLine("GET /{isbn}")
|
||||
BookResource findByIsbn(@Param("isbn") String isbn);
|
||||
|
||||
@RequestLine("POST")
|
||||
@Headers("Content-Type: application/json")
|
||||
void create(Book book);
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.feign.clients.header.staticheader.parameterized;
|
||||
|
||||
import com.baeldung.feign.models.BookResource;
|
||||
|
||||
import feign.Headers;
|
||||
import feign.Param;
|
||||
import feign.RequestLine;
|
||||
|
||||
|
||||
@Headers("x-requester-id: {requester}")
|
||||
public interface BookClient {
|
||||
|
||||
@RequestLine("GET /{isbn}")
|
||||
BookResource findByIsbn(@Param("requester") String requestorId, @Param("isbn") String isbn);
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.feign.header.authorisation;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class ApiAuthorisationService implements AuthorisationService {
|
||||
|
||||
@Override
|
||||
public String getAuthToken() {
|
||||
return "Bearer " + UUID.randomUUID();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.feign.header.authorisation;
|
||||
|
||||
public interface AuthorisationService {
|
||||
|
||||
String getAuthToken();
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.feign.header.interceptor;
|
||||
|
||||
import com.baeldung.feign.header.authorisation.AuthorisationService;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
|
||||
|
||||
public class AuthRequestInterceptor implements RequestInterceptor {
|
||||
|
||||
private AuthorisationService authTokenService;
|
||||
|
||||
public AuthRequestInterceptor(AuthorisationService authTokenService) {
|
||||
this.authTokenService = authTokenService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
template.header("Authorisation", authTokenService.getAuthToken());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -5,9 +5,10 @@
|
|||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
<logger name="feign.Logger" level="DEBUG" />
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.feign.clients.header.dynamicheader;
|
||||
|
||||
import com.baeldung.feign.clients.builder.BookFeignClientBuilder;
|
||||
import com.baeldung.feign.models.Book;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Consumes https://github.com/Baeldung/spring-hypermedia-api
|
||||
*/
|
||||
public class BookClientLiveTest {
|
||||
|
||||
private BookClient bookClient;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
bookClient = BookFeignClientBuilder.createClient(BookClient.class, "http://localhost:8081/api/books");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookClient_shouldPostBook() throws Exception {
|
||||
String isbn = UUID.randomUUID()
|
||||
.toString();
|
||||
|
||||
Book book = new Book(isbn, "Me", "It's me!", null, null);
|
||||
|
||||
Map<String,Object> headerMap = new HashMap<>();
|
||||
|
||||
headerMap.put("metadata-key1", "metadata-value1");
|
||||
headerMap.put("metadata-key2", "metadata-value2");
|
||||
|
||||
bookClient.create(headerMap, book);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.feign.clients.header.interceptor;
|
||||
|
||||
import com.baeldung.feign.clients.builder.BookFeignClientBuilder;
|
||||
import com.baeldung.feign.clients.header.staticheader.BookClient;
|
||||
import com.baeldung.feign.models.Book;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Consumes https://github.com/Baeldung/spring-hypermedia-api
|
||||
*/
|
||||
public class BookClientLiveTest {
|
||||
|
||||
private BookClient bookClient;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
bookClient = BookFeignClientBuilder.createClientWithInterceptor(BookClient.class, "http://localhost:8081/api/books");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookClient_shouldFindOneBook() throws Exception {
|
||||
Book book = bookClient.findByIsbn("0151072558")
|
||||
.getBook();
|
||||
assertThat(book.getAuthor(), containsString("Orwell"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookClient2_shouldFindOneBook() throws Exception {
|
||||
Book book = bookClient.findByIsbn("0151072558")
|
||||
.getBook();
|
||||
assertThat(book.getAuthor(), containsString("Orwell"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookClient_shouldPostBook() throws Exception {
|
||||
String isbn = UUID.randomUUID()
|
||||
.toString();
|
||||
Book book = new Book(isbn, "Me", "It's me!", null, null);
|
||||
bookClient.create(book);
|
||||
|
||||
book = bookClient.findByIsbn(isbn)
|
||||
.getBook();
|
||||
assertThat(book.getAuthor(), is("Me"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.feign.clients.header.staticheader;
|
||||
|
||||
import com.baeldung.feign.clients.builder.BookFeignClientBuilder;
|
||||
import com.baeldung.feign.models.Book;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Consumes https://github.com/Baeldung/spring-hypermedia-api
|
||||
*/
|
||||
public class BookClientLiveTest {
|
||||
|
||||
private BookClient bookClient;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
bookClient = BookFeignClientBuilder.createClient(BookClient.class, "http://localhost:8081/api/books");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookClient_shouldFindOneBook() throws Exception {
|
||||
Book book = bookClient.findByIsbn("0151072558")
|
||||
.getBook();
|
||||
assertThat(book.getAuthor(), containsString("Orwell"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookClient_shouldPostBook() throws Exception {
|
||||
String isbn = UUID.randomUUID()
|
||||
.toString();
|
||||
Book book = new Book(isbn, "Me", "It's me!", null, null);
|
||||
|
||||
bookClient.create(book);
|
||||
|
||||
book = bookClient.findByIsbn(isbn)
|
||||
.getBook();
|
||||
assertThat(book.getAuthor(), is("Me"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.feign.clients.header.staticheader.parameterized;
|
||||
|
||||
import com.baeldung.feign.clients.builder.BookFeignClientBuilder;
|
||||
import com.baeldung.feign.models.Book;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Consumes https://github.com/Baeldung/spring-hypermedia-api
|
||||
*/
|
||||
public class BookClientLiveTest {
|
||||
|
||||
private BookClient bookClient;
|
||||
|
||||
private String requester = "test";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
bookClient = BookFeignClientBuilder.createClient(BookClient.class, "http://localhost:8081/api/books");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookClient_shouldFindOneBook() throws Exception {
|
||||
Book book = bookClient.findByIsbn(requester, "0151072558")
|
||||
.getBook();
|
||||
assertThat(book.getAuthor(), containsString("Orwell"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue