BAEL-2713 Serve Static Content in Spring WebFlux (#6404)

This commit is contained in:
Karol 2019-02-25 17:54:55 +00:00 committed by maibin
parent 053e7c4e98
commit f0a76420b6
9 changed files with 193 additions and 0 deletions

View File

@ -124,6 +124,28 @@
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/assets</directory>
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>${basedir}/target/classes/assets</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

View File

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

View File

@ -0,0 +1,28 @@
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

@ -0,0 +1,35 @@
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

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -0,0 +1,10 @@
<!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

@ -0,0 +1,39 @@
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

@ -0,0 +1,46 @@
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();
}
}