diff --git a/settings.gradle b/settings.gradle index f86ca3e..557a441 100644 --- a/settings.gradle +++ b/settings.gradle @@ -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' diff --git a/spring-http-interface/build.gradle b/spring-http-interface/build.gradle new file mode 100644 index 0000000..80f968f --- /dev/null +++ b/spring-http-interface/build.gradle @@ -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' +} diff --git a/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/Application.java b/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/Application.java new file mode 100644 index 0000000..0161ca5 --- /dev/null +++ b/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/Application.java @@ -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)); + } +} diff --git a/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyClientConfig.java b/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyClientConfig.java new file mode 100644 index 0000000..c482289 --- /dev/null +++ b/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyClientConfig.java @@ -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); + } +} diff --git a/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyData.java b/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyData.java new file mode 100644 index 0000000..234b6b1 --- /dev/null +++ b/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyData.java @@ -0,0 +1,4 @@ +package cn.springcamp.spring.httpinterface; + +public record MyData(Long id, String name) { +} diff --git a/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyService.java b/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyService.java new file mode 100644 index 0000000..edecb86 --- /dev/null +++ b/spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyService.java @@ -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 deleteData(@PathVariable long id); +}