-
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
4 changed files
with
112 additions
and
22 deletions.
There are no files selected for viewing
18 changes: 1 addition & 17 deletions
18
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 |
---|---|---|
@@ -1,27 +1,11 @@ | ||
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 class Application { | ||
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)); | ||
} | ||
} |
21 changes: 16 additions & 5 deletions
21
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 |
---|---|---|
@@ -1,19 +1,30 @@ | ||
package cn.springcamp.spring.httpinterface; | ||
|
||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
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.client.RestTemplate; | ||
import org.springframework.web.client.support.RestTemplateAdapter; | ||
import org.springframework.web.service.invoker.HttpServiceProxyFactory; | ||
import org.springframework.web.util.DefaultUriBuilderFactory; | ||
|
||
@Configuration | ||
public class MyClientConfig { | ||
@Bean | ||
MyService myService() { | ||
RestClient restClient = RestClient.builder().baseUrl("https://httpbin.org").build(); | ||
RestClientAdapter adapter = RestClientAdapter.create(restClient); | ||
public RestTemplate restTemplate(RestTemplateBuilder builder) { | ||
return builder.build(); | ||
} | ||
|
||
@Bean | ||
public MyService myService(RestTemplate restTemplate) { | ||
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("https://httpbin.org")); | ||
RestTemplateAdapter adapter = RestTemplateAdapter.create(restTemplate); | ||
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build(); | ||
|
||
// RestClient restClient = RestClient.builder(restTemplate).baseUrl("https://httpbin.org").build(); | ||
// RestClientAdapter adapter = RestClientAdapter.create(restClient); | ||
// HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build(); | ||
|
||
return factory.createClient(MyService.class); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
spring-http-interface/src/main/java/cn/springcamp/spring/httpinterface/MyController.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,35 @@ | ||
package cn.springcamp.spring.httpinterface; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Slf4j | ||
@RestController | ||
public class MyController { | ||
@Autowired | ||
private MyService myService; | ||
|
||
@GetMapping("/foo") | ||
public String getData() { | ||
return myService.getData("myHeader"); | ||
} | ||
|
||
@GetMapping("/foo/{id}") | ||
public String getDataById(@PathVariable Long id) { | ||
return myService.getData(id); | ||
} | ||
|
||
@PostMapping("/foo") | ||
public String saveData() { | ||
return myService.saveData(new MyData(1L, "demo")); | ||
} | ||
|
||
@DeleteMapping("/foo") | ||
public ResponseEntity<Void> deleteData() { | ||
ResponseEntity<Void> resp = myService.deleteData(1L); | ||
log.info("delete {}", resp); | ||
return resp; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...-http-interface/src/test/java/cn/springcamp/spring/httpinterface/DemoApplicationTest.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,60 @@ | ||
package cn.springcamp.spring.httpinterface; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.MockServerRestClientCustomizer; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.client.ExpectedCount; | ||
import org.springframework.test.web.client.MockRestServiceServer; | ||
import org.springframework.test.web.client.match.MockRestRequestMatchers; | ||
import org.springframework.test.web.client.response.MockRestResponseCreators; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; | ||
|
||
@Slf4j | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class DemoApplicationTest { | ||
|
||
@Autowired | ||
private TestRestTemplate testRestTemplate; | ||
@Autowired | ||
private RestTemplate restTemplate; | ||
@Autowired | ||
private MyService myService; | ||
|
||
private MockRestServiceServer mockRestServiceServer; | ||
private MockServerRestClientCustomizer mockServerRestClientCustomizer; | ||
|
||
@Before | ||
public void before() { | ||
mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build(); | ||
this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org"))) | ||
.andExpect(method(HttpMethod.GET)) | ||
.andRespond(MockRestResponseCreators.withSuccess("{\"get\": 200}", MediaType.APPLICATION_JSON)); | ||
this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org"))) | ||
.andExpect(method(HttpMethod.POST)) | ||
.andRespond(MockRestResponseCreators.withSuccess("{\"post\": 200}", MediaType.APPLICATION_JSON)); | ||
this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org"))) | ||
.andExpect(method(HttpMethod.DELETE)) | ||
.andRespond(MockRestResponseCreators.withSuccess("{\"delete\": 200}", MediaType.APPLICATION_JSON)); | ||
} | ||
|
||
@Test | ||
public void testRemoteCallRest() { | ||
log.info("testRemoteCallRest get {}", testRestTemplate.getForObject("/foo", String.class)); | ||
log.info("testRemoteCallRest getById {}", testRestTemplate.getForObject("/foo/1", String.class)); | ||
log.info("testRemoteCallRest post {}", testRestTemplate.postForObject("/foo", new MyData(1L, "demo"), String.class)); | ||
testRestTemplate.exchange("/foo", HttpMethod.DELETE, HttpEntity.EMPTY, String.class); | ||
} | ||
} |