moved webflux static content examples from spring-5-reactive to spring-5-reactive-2

This commit is contained in:
fejera
2019-09-29 16:19:12 +02:00
parent c4c59a08dd
commit c391435c3c
10 changed files with 11 additions and 2 deletions

View File

@@ -1,28 +0,0 @@
package com.baeldung.staticcontent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import java.util.Collections;
@SpringBootApplication
public class StaticContentApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(StaticContentApplication.class);
app.setDefaultProperties(Collections.singletonMap("server.port", "8084"));
app.run(args);
}
@Bean
public SecurityWebFilterChain staticContentSpringSecurityFilterChain(ServerHttpSecurity http) {
http.authorizeExchange()
.anyExchange()
.permitAll();
return http.build();
}
}

View File

@@ -1,35 +0,0 @@
package com.baeldung.staticcontent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
@Configuration
public class StaticContentConfig {
@Bean
public RouterFunction<ServerResponse> htmlRouter(@Value("classpath:/public/index.html") Resource html) {
return route(
GET("/"),
request -> ok()
.contentType(MediaType.TEXT_HTML)
.syncBody(html)
);
}
@Bean
public RouterFunction<ServerResponse> imgRouter() {
return RouterFunctions.resources("/img/**", new ClassPathResource("img/"));
}
}

View File

@@ -1,3 +0,0 @@
# Use in Static content Example
spring.webflux.static-path-pattern = /assets/**
spring.resources.static-locations = classpath:/assets/

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

View File

@@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Baeldung: Static Content in Spring WebFlux</title>
</head>
<body>
Example HTML file
</body>
</html>

View File

@@ -1,39 +0,0 @@
package com.baeldung.staticcontent;
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.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("assets-custom-location")
public class StaticContentCustomLocationIntegrationTest {
@Autowired
private WebTestClient client;
@Test
public void whenRequestingHtmlFileFromCustomLocation_thenReturnThisFileAndTextHtmlContentType() throws Exception {
client.get()
.uri("/assets/index.html")
.exchange()
.expectStatus().isOk()
.expectHeader().valueEquals(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML_VALUE);
}
@Test
public void whenRequestingMissingStaticResource_thenReturnNotFoundStatus() throws Exception {
client.get()
.uri("/assets/unknown.png")
.exchange()
.expectStatus().isNotFound();
}
}

View File

@@ -1,46 +0,0 @@
package com.baeldung.staticcontent;
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.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class StaticContentDefaultLocationIntegrationTest {
@Autowired
private WebTestClient client;
@Test
public void whenRequestingHtmlFileFromDefaultLocation_thenReturnThisFileAndTextHtmlContentType() throws Exception {
client.get()
.uri("/index.html")
.exchange()
.expectStatus().isOk()
.expectHeader().valueEquals(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML_VALUE);
}
@Test
public void whenRequestingPngImageFromImgLocation_thenReturnThisFileAndImagePngContentType() throws Exception {
client.get()
.uri("/img/example-image.png")
.exchange()
.expectStatus().isOk()
.expectHeader().valueEquals(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE);
}
@Test
public void whenRequestingMissingStaticResource_thenReturnNotFoundStatus() throws Exception {
client.get()
.uri("/unknown.png")
.exchange()
.expectStatus().isNotFound();
}
}