下面列出了org.springframework.http.server.reactive.ReactorHttpHandlerAdapter#org.springframework.web.server.adapter.WebHttpHandlerBuilder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void handleErrorFromFilter() throws Exception {
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerHttpResponse response = new MockServerHttpResponse();
TestExceptionHandler exceptionHandler = new TestExceptionHandler();
WebHttpHandlerBuilder.webHandler(new StubWebHandler())
.filter(new ExceptionFilter())
.exceptionHandler(exceptionHandler).build()
.handle(request, response)
.block();
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
assertNotNull(exceptionHandler.ex);
assertEquals("boo", exceptionHandler.ex.getMessage());
}
WebServer start() throws Exception {
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
.filter(new IndexRewriteFilter())
.build();
Tomcat tomcat = new Tomcat();
tomcat.setHostname("localhost");
tomcat.setPort(9090);
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
TomcatWebServer server = new TomcatWebServer(tomcat);
server.start();
return server;
}
@Test
public void applyFiltersBeforeServerCreated() {
this.serverSpec.webFilter(new TestWebFilter("App-A"));
this.serverSpec.webFilter(new TestWebFilter("App-B"));
this.serverSpec.apply(new MockServerConfigurer() {
@Override
public void beforeServerCreated(WebHttpHandlerBuilder builder) {
builder.filters(filters -> {
filters.add(0, new TestWebFilter("Fwk-A"));
filters.add(1, new TestWebFilter("Fwk-B"));
});
}
});
this.serverSpec.build().get().uri("/")
.exchange()
.expectBody(String.class)
.consumeWith(result -> assertThat(
result.getResponseBody(), containsString("test-attribute=:Fwk-A:Fwk-B:App-A:App-B")));
}
@Test
public void handleErrorFromFilter() throws Exception {
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerHttpResponse response = new MockServerHttpResponse();
TestExceptionHandler exceptionHandler = new TestExceptionHandler();
WebHttpHandlerBuilder.webHandler(new StubWebHandler())
.filter(new ExceptionFilter())
.exceptionHandler(exceptionHandler).build()
.handle(request, response)
.block();
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
assertNotNull(exceptionHandler.ex);
assertEquals("boo", exceptionHandler.ex.getMessage());
}
@Test
public void applyFiltersBeforeServerCreated() {
this.serverSpec.webFilter(new TestWebFilter("App-A"));
this.serverSpec.webFilter(new TestWebFilter("App-B"));
this.serverSpec.apply(new MockServerConfigurer() {
@Override
public void beforeServerCreated(WebHttpHandlerBuilder builder) {
builder.filters(filters -> {
filters.add(0, new TestWebFilter("Fwk-A"));
filters.add(1, new TestWebFilter("Fwk-B"));
});
}
});
this.serverSpec.build().get().uri("/")
.exchange()
.expectBody(String.class)
.consumeWith(result -> assertThat(
result.getResponseBody(), containsString("test-attribute=:Fwk-A:Fwk-B:App-A:App-B")));
}
@Bean
public Server jettyServer(ApplicationContext context) throws Exception {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
Servlet servlet = new JettyHttpHandlerAdapter(handler);
Server server = new Server();
ServletContextHandler contextHandler = new ServletContextHandler(server, "");
contextHandler.addServlet(new ServletHolder(servlet), "/");
contextHandler.start();
ServerConnector connector = new ServerConnector(server);
connector.setHost("localhost");
connector.setPort(port);
server.addConnector(connector);
return server;
}
@Bean
@Profile("default")
public Tomcat embeddedTomcatServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
Servlet servlet = new TomcatHttpHandlerAdapter(handler);
Tomcat tomcat = new Tomcat();
File base = new File(System.getProperty("java.io.tmpdir"));
Context rootContext = tomcat.addContext("", base.getAbsolutePath());
Tomcat.addServlet(rootContext, "main", servlet).setAsyncSupported(true);
rootContext.addServletMappingDecoded("/", "main");
tomcat.setHostname("localhost");
tomcat.setPort(this.port);
tomcat.setBaseDir(System.getProperty("java.io.tmpdir"));
return tomcat;
}
@Test
void enableSpringWebSessionConfiguresThings() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(GoodConfig.class);
this.context.refresh();
WebSessionManager webSessionManagerFoundByType = this.context.getBean(WebSessionManager.class);
Object webSessionManagerFoundByName = this.context.getBean(WebHttpHandlerBuilder.WEB_SESSION_MANAGER_BEAN_NAME);
assertThat(webSessionManagerFoundByType).isNotNull();
assertThat(webSessionManagerFoundByName).isNotNull();
assertThat(webSessionManagerFoundByType).isEqualTo(webSessionManagerFoundByName);
assertThat(this.context.getBean(ReactiveSessionRepository.class)).isNotNull();
}
private HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(DispatcherConfig.class, this.serverConfigClass);
context.register(getWebConfigClass());
context.refresh();
return WebHttpHandlerBuilder.applicationContext(context).build();
}
@Override
protected HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(TestConfiguration.class);
wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
@Override
protected HttpHandler createHttpHandler() {
this.wac = new AnnotationConfigApplicationContext();
this.wac.register(TestConfiguration.class);
this.wac.refresh();
DispatcherHandler webHandler = new DispatcherHandler();
webHandler.setApplicationContext(this.wac);
return WebHttpHandlerBuilder.webHandler(webHandler).build();
}
@Override
protected HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(WebConfig.class);
wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac))
.exceptionHandler(new ResponseStatusExceptionHandler())
.build();
}
@Override
protected HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(TestConfiguration.class);
wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
@Override
protected HttpHandler createHttpHandler() {
this.wac = new AnnotationConfigApplicationContext();
this.wac.register(TestConfiguration.class);
this.wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(this.wac)).build();
}
@Override
protected HttpHandler createHttpHandler() {
this.wac = new AnnotationConfigApplicationContext();
this.wac.register(TestConfiguration.class);
this.wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(this.wac)).build();
}
@Test
public void multipleWebFluxApps() throws Exception {
AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext();
context1.register(WebAppConfig.class);
context1.refresh();
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext();
context2.register(WebAppConfig.class);
context2.refresh();
HttpHandler webApp1Handler = WebHttpHandlerBuilder.applicationContext(context1).build();
HttpHandler webApp2Handler = WebHttpHandlerBuilder.applicationContext(context2).build();
ReactorHttpServer server = new ReactorHttpServer();
server.registerHttpHandler("/webApp1", webApp1Handler);
server.registerHttpHandler("/webApp2", webApp2Handler);
server.afterPropertiesSet();
server.start();
try {
RestTemplate restTemplate = new RestTemplate();
String actual;
String url = "http://localhost:" + server.getPort() + "/webApp1/test";
actual = restTemplate.getForObject(url, String.class);
assertEquals("Tested in /webApp1", actual);
url = "http://localhost:" + server.getPort() + "/webApp2/test";
actual = restTemplate.getForObject(url, String.class);
assertEquals("Tested in /webApp2", actual);
}
finally {
server.stop();
}
}
@Test
public void servletPathMapping() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(WebAppConfig.class);
context.refresh();
File base = new File(System.getProperty("java.io.tmpdir"));
TomcatHttpServer server = new TomcatHttpServer(base.getAbsolutePath());
server.setContextPath("/app");
server.setServletMapping("/api/*");
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
server.setHandler(httpHandler);
server.afterPropertiesSet();
server.start();
try {
RestTemplate restTemplate = new RestTemplate();
String actual;
String url = "http://localhost:" + server.getPort() + "/app/api/test";
actual = restTemplate.getForObject(url, String.class);
assertEquals("Tested in /app/api", actual);
}
finally {
server.stop();
}
}
@Bean
public DisposableServer disposableServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context)
.build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create();
httpServer.host("localhost");
httpServer.port(8080);
return httpServer.handle(adapter).bindNow();
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
private DefaultWebTestClientBuilder(@Nullable WebClient.Builder webClientBuilder,
@Nullable WebHttpHandlerBuilder httpHandlerBuilder, @Nullable ClientHttpConnector connector,
@Nullable Duration responseTimeout) {
Assert.isTrue(httpHandlerBuilder != null || connector != null,
"Either WebHttpHandlerBuilder or ClientHttpConnector must be provided");
this.webClientBuilder = (webClientBuilder != null ? webClientBuilder : WebClient.builder());
this.httpHandlerBuilder = (httpHandlerBuilder != null ? httpHandlerBuilder.clone() : null);
this.connector = connector;
this.responseTimeout = responseTimeout;
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Override
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies);
return WebHttpHandlerBuilder.webHandler(webHandler)
.filters(filters -> filters.addAll(this.handlerStrategies.webFilters()))
.exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers()))
.localeContextResolver(this.handlerStrategies.localeContextResolver());
}
@Bean
public HttpServer httpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
return HttpServer.create()
.host("localhost")
.port(this.port)
.handle(adapter);
}
@Override
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
return WebHttpHandlerBuilder.webHandler(exchange -> {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
String text = exchange.getAttributes().toString();
DataBuffer buffer = factory.wrap(text.getBytes(StandardCharsets.UTF_8));
return exchange.getResponse().writeWith(Mono.just(buffer));
});
}
@Override
public void afterConfigurerAdded(WebTestClient.Builder builder,
@Nullable WebHttpHandlerBuilder httpHandlerBuilder,
@Nullable ClientHttpConnector connector) {
Assert.notNull(httpHandlerBuilder, "Not a mock server");
httpHandlerBuilder.filters(filters -> {
filters.removeIf(filter -> filter instanceof IdentityFilter);
filters.add(0, this.filter);
});
}
private HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(DispatcherConfig.class, this.serverConfigClass);
context.register(getWebConfigClass());
context.refresh();
return WebHttpHandlerBuilder.applicationContext(context).build();
}
@Override
protected HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(TestConfiguration.class);
wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
@Override
protected HttpHandler createHttpHandler() {
this.wac = new AnnotationConfigApplicationContext();
this.wac.register(TestConfiguration.class);
this.wac.refresh();
DispatcherHandler webHandler = new DispatcherHandler();
webHandler.setApplicationContext(this.wac);
return WebHttpHandlerBuilder.webHandler(webHandler).build();
}
@Override
protected HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(TestConfiguration.class);
wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}