-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Declarative RestClient is implemented
- Loading branch information
musab.bozkurt
committed
Feb 12, 2024
1 parent
35ce73b
commit e380638
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...a/com/mb/livedataservice/client/jsonplaceholder/DeclarativeJSONPlaceholderRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.mb.livedataservice.client.jsonplaceholder; | ||
|
||
import com.mb.livedataservice.client.jsonplaceholder.request.PostRequest; | ||
import com.mb.livedataservice.client.jsonplaceholder.response.PostResponse; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.service.annotation.DeleteExchange; | ||
import org.springframework.web.service.annotation.GetExchange; | ||
import org.springframework.web.service.annotation.PostExchange; | ||
|
||
import java.util.List; | ||
|
||
public interface DeclarativeJSONPlaceholderRestClient { | ||
|
||
@GetExchange("/posts") | ||
List<PostResponse> getAllPosts(); | ||
|
||
@GetExchange("/posts/{id}") | ||
PostResponse getPost(@PathVariable Integer id); | ||
|
||
@DeleteExchange("/posts/{id}") | ||
void deletePost(@PathVariable Integer id); | ||
|
||
@PostExchange("/posts") | ||
PostResponse createPost(@RequestBody PostRequest post); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/mb/livedataservice/config/DeclarativeHttpInterfaces.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.mb.livedataservice.config; | ||
|
||
import com.mb.livedataservice.client.jsonplaceholder.DeclarativeJSONPlaceholderRestClient; | ||
import com.mb.livedataservice.exception.BaseException; | ||
import com.mb.livedataservice.exception.ErrorCode; | ||
import lombok.SneakyThrows; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.client.JdkClientHttpRequestFactory; | ||
import org.springframework.http.client.support.BasicAuthenticationInterceptor; | ||
import org.springframework.web.client.RestClient; | ||
import org.springframework.web.client.support.RestClientAdapter; | ||
import org.springframework.web.service.invoker.HttpServiceProxyFactory; | ||
|
||
import java.time.Duration; | ||
|
||
@Configuration | ||
public class DeclarativeHttpInterfaces { | ||
|
||
@Bean | ||
public DeclarativeJSONPlaceholderRestClient declarativeJSONPlaceholderRestClient(RestClient.Builder builder, JSONPlaceholderClientProperties JSONPlaceholderClientProperties) { | ||
JdkClientHttpRequestFactory jdkClientHttpRequestFactory = new JdkClientHttpRequestFactory(); | ||
jdkClientHttpRequestFactory.setReadTimeout(Duration.ofSeconds(15)); | ||
|
||
RestClient restClient = builder | ||
.baseUrl(JSONPlaceholderClientProperties.getUrl()) | ||
.requestFactory(jdkClientHttpRequestFactory) | ||
.requestInterceptor(new BasicAuthenticationInterceptor(JSONPlaceholderClientProperties.getClientId(), JSONPlaceholderClientProperties.getClientSecret())) | ||
.defaultStatusHandler(HttpStatusCode::isError, (request, response) -> { | ||
throw new BaseException(new ErrorCode() { | ||
|
||
@Override | ||
@SneakyThrows | ||
public HttpStatus getHttpStatus() { | ||
return HttpStatus.resolve(response.getStatusCode().value()); | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public String getCode() { | ||
return response.getStatusText(); | ||
} | ||
}); | ||
}) | ||
.build(); | ||
|
||
return HttpServiceProxyFactory | ||
.builderFor(RestClientAdapter.create(restClient)) | ||
.build() | ||
.createClient(DeclarativeJSONPlaceholderRestClient.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters