Reflect grouping in directory structure

This commit is contained in:
Andy Wilkinson
2022-10-28 14:36:50 +01:00
parent 393e1cb6bf
commit 7112a4b6a4
788 changed files with 9 additions and 100 deletions

View File

@@ -0,0 +1 @@
Tests if `WebClient` works

View File

@@ -0,0 +1,17 @@
plugins {
id 'java'
id 'org.springframework.boot'
id 'org.springframework.aot.smoke-test'
id 'org.graalvm.buildtools.native'
}
dependencies {
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation("org.springframework.boot:spring-boot-starter-webflux")
testImplementation("org.springframework.boot:spring-boot-starter-test")
appTestImplementation(project(":aot-smoke-test-support"))
appTestImplementation("org.awaitility:awaitility:4.2.0")
}

View File

@@ -0,0 +1,42 @@
package com.example.webclient;
import java.time.Duration;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
import org.springframework.aot.smoketest.support.junit.ApplicationTest;
import static org.assertj.core.api.Assertions.assertThat;
@ApplicationTest
class WebClientApplicationAotTests {
@Test
@DisabledIfEnvironmentVariable(named = "CI", matches = "true", disabledReason = "HTTP is blocked on CI")
void httpWorks(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining("http: DataDto{url='http://httpbin.org/anything', method='GET'}");
});
}
@Test
void httpsWorks(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining("https: DataDto{url='https://httpbin.org/anything', method='GET'}");
});
}
@Test
void serviceWorks(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining(
"service: ExchangeDataDto{url='https://httpbin.org/anything', method='GET'}");
});
}
}

View File

@@ -0,0 +1,68 @@
package com.example.webclient;
import java.time.Duration;
import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.Builder;
@Component
@RegisterReflectionForBinding(DataDto.class)
class CLR implements CommandLineRunner {
private final Builder webClientBuilder;
private final DataService dataService;
CLR(WebClient.Builder webClientBuilder, DataService dataService) {
this.webClientBuilder = webClientBuilder;
this.dataService = dataService;
}
@Override
public void run(String... args) {
http();
https();
service();
}
private void http() {
try {
WebClient webClient = this.webClientBuilder.baseUrl("http://httpbin.org/").build();
DataDto dto = webClient.get().uri("/anything").retrieve().bodyToMono(DataDto.class)
.timeout(Duration.ofSeconds(5)).block();
System.out.printf("http: %s%n", dto);
}
catch (Exception ex) {
System.out.println("http failed:");
ex.printStackTrace(System.out);
}
}
private void https() {
try {
WebClient webClient = this.webClientBuilder.baseUrl("https://httpbin.org/").build();
DataDto dto = webClient.get().uri("/anything").retrieve().bodyToMono(DataDto.class)
.timeout(Duration.ofSeconds(5)).block();
System.out.printf("https: %s%n", dto);
}
catch (Exception ex) {
System.out.println("https failed:");
ex.printStackTrace(System.out);
}
}
private void service() {
try {
ExchangeDataDto dto = this.dataService.getData();
System.out.printf("service: %s%n", dto);
}
catch (Exception ex) {
System.out.println("service failed:");
ex.printStackTrace(System.out);
}
}
}

View File

@@ -0,0 +1,33 @@
package com.example.webclient;
public class DataDto {
private String url;
private String method;
public DataDto() {
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
@Override
public String toString() {
return "DataDto{" + "url='" + url + '\'' + ", method='" + method + '\'' + '}';
}
}

View File

@@ -0,0 +1,10 @@
package com.example.webclient;
import org.springframework.web.service.annotation.GetExchange;
interface DataService {
@GetExchange("/anything")
ExchangeDataDto getData();
}

View File

@@ -0,0 +1,33 @@
package com.example.webclient;
public class ExchangeDataDto {
private String url;
private String method;
public ExchangeDataDto() {
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
@Override
public String toString() {
return "ExchangeDataDto{" + "url='" + url + '\'' + ", method='" + method + '\'' + '}';
}
}

View File

@@ -0,0 +1,29 @@
package com.example.webclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@SpringBootApplication
public class WebClientApplication {
public static void main(String[] args) throws InterruptedException {
SpringApplication application = new SpringApplication(WebClientApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
Thread.currentThread().join(); // To be able to measure memory consumption
}
@Bean
DataService dataService(WebClient.Builder webClientBuilder) {
WebClient webClient = webClientBuilder.baseUrl("https://httpbin.org/").build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient))
.build();
return factory.createClient(DataService.class);
}
}