BAEL-6093 add spring boot 3 url matching code sample (#13744)
This commit is contained in:
parent
51caf72619
commit
d21e03938e
1
spring-boot-modules/spring-boot-3-url-matching/README.md
Normal file
1
spring-boot-modules/spring-boot-3-url-matching/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
101
spring-boot-modules/spring-boot-3-url-matching/pom.xml
Normal file
101
spring-boot-modules/spring-boot-3-url-matching/pom.xml
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>spring-boot-3</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-3</name>
|
||||||
|
<description>URL Matching in Spring Boot</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-3</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-3</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>6.0.6</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-core</artifactId>
|
||||||
|
<version>3.5.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-test</artifactId>
|
||||||
|
<version>3.5.4</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.sample;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
|
||||||
|
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
|
||||||
|
public class URLMatchingApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(URLMatchingApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.sample.config;
|
||||||
|
|
||||||
|
import com.baeldung.sample.filters.TrailingSlashRedirectFilterReactive;
|
||||||
|
import jakarta.servlet.Filter;
|
||||||
|
import org.springframework.web.server.WebFilter;
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import com.baeldung.sample.filters.TrailingSlashRedirectFilter;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig {
|
||||||
|
@Bean
|
||||||
|
public WebFilter trailingSlashRedirectReactiveFilter() {
|
||||||
|
return new TrailingSlashRedirectFilterReactive();
|
||||||
|
}
|
||||||
|
@Bean
|
||||||
|
public Filter trailingSlashRedirectFilter() {
|
||||||
|
return new TrailingSlashRedirectFilter();
|
||||||
|
}
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean<Filter> trailingSlashFilter() {
|
||||||
|
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
|
||||||
|
registrationBean.setFilter(trailingSlashRedirectFilter());
|
||||||
|
registrationBean.addUrlPatterns("/*");
|
||||||
|
return registrationBean;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.baeldung.sample.filters;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequestWrapper;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import jakarta.servlet.Filter;
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.ServletRequest;
|
||||||
|
import jakarta.servlet.ServletResponse;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TrailingSlashRedirectFilter implements Filter {
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
HttpServletRequest httpRequest = (HttpServletRequest) request;
|
||||||
|
String path = httpRequest.getRequestURI();
|
||||||
|
|
||||||
|
if (path.endsWith("/")) {
|
||||||
|
String newPath = path.substring(0, path.length() - 1);
|
||||||
|
HttpServletRequest newRequest = new CustomHttpServletRequestWrapper(httpRequest, newPath);
|
||||||
|
chain.doFilter(newRequest, response);
|
||||||
|
} else {
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
|
||||||
|
|
||||||
|
private final String newPath;
|
||||||
|
|
||||||
|
public CustomHttpServletRequestWrapper(HttpServletRequest request, String newPath) {
|
||||||
|
super(request);
|
||||||
|
this.newPath = newPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRequestURI() {
|
||||||
|
return newPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StringBuffer getRequestURL() {
|
||||||
|
StringBuffer url = new StringBuffer();
|
||||||
|
url.append(getScheme()).append("://").append(getServerName()).append(":").append(getServerPort())
|
||||||
|
.append(newPath);
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.sample.filters;
|
||||||
|
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import org.springframework.web.server.WebFilter;
|
||||||
|
import org.springframework.web.server.WebFilterChain;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TrailingSlashRedirectFilterReactive implements WebFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
String path = request.getPath().value();
|
||||||
|
|
||||||
|
if (path.endsWith("/")) {
|
||||||
|
String newPath = path.substring(0, path.length() - 1);
|
||||||
|
ServerHttpRequest newRequest = request.mutate().path(newPath).build();
|
||||||
|
return chain.filter(exchange.mutate().request(newRequest).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.sample.resources;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class GreetingsController {
|
||||||
|
|
||||||
|
@GetMapping("/some/greeting")
|
||||||
|
public String greeting() {
|
||||||
|
return "Hello";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/some/greeting/")
|
||||||
|
public String greetingTrailingSlash() {
|
||||||
|
return "Hello with slash";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.sample.resources;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class GreetingsControllerReactive {
|
||||||
|
|
||||||
|
@GetMapping("/some/reactive/greeting")
|
||||||
|
public Mono<String> greeting() {
|
||||||
|
return Mono.just("Hello reactive");
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/some/reactive/greeting/")
|
||||||
|
public Mono<String> greetingTrailingSlash() {
|
||||||
|
return Mono.just("Hello with slash reactive");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
spring:
|
||||||
|
mvc:
|
||||||
|
throw-exception-if-no-handler-found: true
|
||||||
|
jackson:
|
||||||
|
deserialization:
|
||||||
|
FAIL_ON_UNKNOWN_PROPERTIES: true
|
||||||
|
property-naming-strategy: SNAKE_CASE
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.baeldung.sample.resources;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
@WebMvcTest(controllers = GreetingsController.class)
|
||||||
|
class GreetingsControllerApiIntegrationTest {
|
||||||
|
|
||||||
|
private static final String BASEURL = "/some";
|
||||||
|
private static final String DEFAULT_MEDIA_TYPE = MediaType.APPLICATION_JSON_VALUE;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MockMvc mvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGreeting() throws Exception {
|
||||||
|
mvc.perform(get(BASEURL + "/greeting").accept(DEFAULT_MEDIA_TYPE))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string("Hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled("Disabled while TrailingSlashRedirectFilter is in use.")
|
||||||
|
public void testGreetingTrailingSlash() throws Exception {
|
||||||
|
mvc.perform(get(BASEURL + "/greeting/").accept(DEFAULT_MEDIA_TYPE))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string("Hello with slash"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGreetingTrailingSlashWithFilter() throws Exception {
|
||||||
|
mvc.perform(get(BASEURL + "/greeting/").accept(DEFAULT_MEDIA_TYPE))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string("Hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.sample.resources;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
||||||
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@WebFluxTest
|
||||||
|
public class GreetingsControllerReactiveApiIntegrationTest {
|
||||||
|
private static final String BASEURL = "/some/reactive";
|
||||||
|
@Autowired
|
||||||
|
private WebTestClient webClient;
|
||||||
|
@Test
|
||||||
|
public void testGreeting() {
|
||||||
|
webClient.get().uri( BASEURL + "/greeting")
|
||||||
|
.exchange()
|
||||||
|
.expectStatus().isOk()
|
||||||
|
.expectBody().consumeWith(result -> {
|
||||||
|
String responseBody = new String(result.getResponseBody());
|
||||||
|
assertTrue(responseBody.contains("Hello reactive"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
@Disabled("Disabled while TrailingSlashRedirectFilter is in use.")
|
||||||
|
public void testGreetingTrailingSlash() {
|
||||||
|
webClient.get().uri(BASEURL + "/greeting/")
|
||||||
|
.exchange()
|
||||||
|
.expectStatus().isOk()
|
||||||
|
.expectBody().consumeWith(result -> {
|
||||||
|
String responseBody = new String(result.getResponseBody());
|
||||||
|
assertTrue(responseBody.contains("Hello with slash reactive"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testGreetingTrailingSlashWithFilter() {
|
||||||
|
webClient.get().uri(BASEURL + "/greeting/")
|
||||||
|
.exchange()
|
||||||
|
.expectStatus().isOk()
|
||||||
|
.expectBody().consumeWith(result -> {
|
||||||
|
String responseBody = new String(result.getResponseBody());
|
||||||
|
assertTrue(responseBody.contains("Hello reactive"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user