https://github.com/ashutoshsahoo/gs-spring-boot-http-client
Spring Declarative HTTP Client using @HttpExchange
https://github.com/ashutoshsahoo/gs-spring-boot-http-client
getexchange httpexchange postexchange spring-boot3 spring-boot3x spring6
Last synced: 2 months ago
JSON representation
Spring Declarative HTTP Client using @HttpExchange
- Host: GitHub
- URL: https://github.com/ashutoshsahoo/gs-spring-boot-http-client
- Owner: ashutoshsahoo
- Created: 2023-11-28T10:27:42.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-15T07:22:23.000Z (almost 2 years ago)
- Last Synced: 2025-03-20T18:26:11.536Z (about 1 year ago)
- Topics: getexchange, httpexchange, postexchange, spring-boot3, spring-boot3x, spring6
- Language: Java
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.MD
Awesome Lists containing this project
README
# gs-spring-boot-http-client
Spring Declarative HTTP Client using @HttpExchange
* Declaration
```java
@HttpExchange(url = "/users",
accept = MediaType.APPLICATION_JSON_VALUE,
contentType = MediaType.APPLICATION_JSON_VALUE)
public interface UserClient {
@GetExchange
List getAll();
@GetExchange("/{id}")
UserDto getById(@PathVariable("id") Long id);
@PostExchange
ResponseEntity save(@RequestBody UserDto userDto);
@PutExchange("/{id}")
ResponseEntity update(@PathVariable Long id, @RequestBody UserDto userDto);
@DeleteExchange("/{id}")
ResponseEntity delete(@PathVariable Long id);
}
```
* Configuration
```java
@Configuration
public class UserClientConfig {
@Bean
RestClient restClient() {
return RestClient.builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.build();
}
@Bean
UserClient userClient(RestClient restClient) {
HttpServiceProxyFactory httpServiceProxyFactory =
HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient))
.build();
return httpServiceProxyFactory.createClient(UserClient.class);
}
}
```