-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
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
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,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' | ||
} |
27 changes: 27 additions & 0 deletions
27
spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/Application.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,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)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyClientConfig.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,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); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyData.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,4 @@ | ||
package cn.springcamp.spring.httpinterface; | ||
|
||
public record MyData(Long id, String name) { | ||
} |
23 changes: 23 additions & 0 deletions
23
spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyService.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,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); | ||
} |