Skip to content

Commit

Permalink
spring-http-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
qihaiyan committed Dec 24, 2023
1 parent bf71165 commit 591d0ee
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ include 'json-utils'
include 'spring-data-envers-conditional'
include 'spring-data-jdbc-client'
include 'spring-rest-client'
include 'spring-http-interface'
5 changes: 5 additions & 0 deletions spring-http-interface/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.vintage:junit-vintage-engine'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.springcamp.spring.httpinterface;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private MyService myService;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) {
log.info("get data: {}", myService.getData("myHeader"));
log.info("get data by id: {}", myService.getData(1L));
log.info("save data: {}", myService.saveData(new MyData(1L, "test")));
log.info("delete data by id: {}", myService.deleteData(1L));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.springcamp.spring.httpinterface;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

@Configuration
public class MyClientConfig {
@Bean
MyService myService() {
RestClient restClient = RestClient.builder().baseUrl("https://httpbin.org").build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();

return factory.createClient(MyService.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cn.springcamp.spring.httpinterface;

public record MyData(Long id, String name) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cn.springcamp.spring.httpinterface;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.service.annotation.DeleteExchange;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.PostExchange;

public interface MyService {
@GetExchange("/anything")
String getData(@RequestHeader("MY-HEADER") String headerName);

@GetExchange("/anything/{id}")
String getData(@PathVariable long id);

@PostExchange("/anything")
String saveData(@RequestBody MyData data);

@DeleteExchange("/anything/{id}")
ResponseEntity<Void> deleteData(@PathVariable long id);
}

0 comments on commit 591d0ee

Please sign in to comment.