Skip to content

Commit 91b0efc

Browse files
mansi2392yetanotherallisonf
authored andcommitted
BAEL-944 Exploring the Spring MVC URL Matching Improvements (eugenp#1954)
* Example Code For Evaluation Article This is an example code for the evaluation article on "Different Types of Bean Injection in Spring" * Added unit tests * Minor changes to application context * Removed code committed for evaluation article * BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5 * BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5 * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Code Formatting and solving build issue * BAEL-944 Resolving build issue due to change in Spring version * BAEL-944 Resolving build issue * BAEL-944 Formatting code * BAEL-944 Moving tests to correct package * BAEL-944 Moving tests to correct package * BAEL-944 Replacing @RequestMapping by @GetMapping * BAEL-944 Remove unnecessary attribute name, "value" in annotations
1 parent ec46b8b commit 91b0efc

11 files changed

Lines changed: 323 additions & 4 deletions

‎spring-5/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
<junit.platform.version>1.0.0-M4</junit.platform.version>
178178
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
179179
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
180+
<spring.version>5.0.0.RC2</spring.version>
180181
</properties>
181182

182183
</project>

‎spring-5/src/main/java/com/baeldung/Spring5Application.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ComponentScan;
56

67
@SpringBootApplication
8+
@ComponentScan(basePackages = {"com.baeldung.web"})
79
public class Spring5Application {
810

911
public static void main(String[] args) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.functional;
2+
3+
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
4+
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
5+
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
6+
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
7+
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
8+
9+
import org.apache.catalina.Context;
10+
import org.apache.catalina.startup.Tomcat;
11+
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
12+
import org.springframework.boot.web.server.WebServer;
13+
import org.springframework.core.io.ClassPathResource;
14+
import org.springframework.http.server.reactive.HttpHandler;
15+
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
16+
import org.springframework.web.reactive.function.server.RouterFunction;
17+
import org.springframework.web.reactive.function.server.RouterFunctions;
18+
import org.springframework.web.reactive.function.server.ServerResponse;
19+
import org.springframework.web.server.WebHandler;
20+
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
21+
22+
public class ExploreSpring5URLPatternUsingRouterFunctions {
23+
24+
private RouterFunction<ServerResponse> routingFunction() {
25+
26+
return route(GET("/p?ths"), serverRequest -> ok().body(fromObject("/p?ths"))).andRoute(GET("/test/{*id}"), serverRequest -> ok().body(fromObject(serverRequest.pathVariable("id"))))
27+
.andRoute(GET("/*card"), serverRequest -> ok().body(fromObject("/*card path was accessed")))
28+
.andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromObject(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2"))))
29+
.andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromObject("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung"))))
30+
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")));
31+
}
32+
33+
WebServer start() throws Exception {
34+
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
35+
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
36+
.prependFilter(new IndexRewriteFilter())
37+
.build();
38+
39+
Tomcat tomcat = new Tomcat();
40+
tomcat.setHostname("localhost");
41+
tomcat.setPort(9090);
42+
Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
43+
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
44+
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
45+
rootContext.addServletMappingDecoded("/", "httpHandlerServlet");
46+
47+
TomcatWebServer server = new TomcatWebServer(tomcat);
48+
server.start();
49+
return server;
50+
51+
}
52+
53+
public static void main(String[] args) {
54+
try {
55+
new FunctionalWebApplication().start();
56+
} catch (Exception e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
}

‎spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.springframework.web.reactive.function.server.RouterFunction;
1616
import org.springframework.web.reactive.function.server.RouterFunctions;
1717
import org.springframework.web.reactive.function.server.ServerResponse;
18+
import org.springframework.web.server.WebHandler;
1819
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
1920
import reactor.core.publisher.Flux;
2021

@@ -58,7 +59,7 @@ private RouterFunction<ServerResponse> routingFunction() {
5859
@Bean
5960
public ServletRegistrationBean servletRegistrationBean() throws Exception {
6061
HttpHandler httpHandler = WebHttpHandlerBuilder
61-
.webHandler(toHttpHandler(routingFunction()))
62+
.webHandler((WebHandler) toHttpHandler(routingFunction()))
6263
.prependFilter(new IndexRewriteFilter())
6364
.build();
6465
ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new RootServlet(httpHandler), "/");

‎spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private RouterFunction<ServerResponse> routingFunction() {
5050
}
5151

5252
WebServer start() throws Exception {
53-
WebHandler webHandler = toHttpHandler(routingFunction());
53+
WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
5454
HttpHandler httpHandler = WebHttpHandlerBuilder
5555
.webHandler(webHandler)
5656
.prependFilter(new IndexRewriteFilter())

‎spring-5/src/main/java/com/baeldung/functional/IndexRewriteFilter.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain web
1919
.mutate()
2020
.request(builder -> builder
2121
.method(request.getMethod())
22-
.contextPath(request.getContextPath())
22+
.contextPath(request.getPath().toString())
2323
.path("/test"))
2424
.build());
2525
}

‎spring-5/src/main/java/com/baeldung/functional/RootServlet.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
2424
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;
2525
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
26+
import org.springframework.web.server.WebHandler;
2627

2728
public class RootServlet extends ServletHttpHandlerAdapter {
2829

2930
public RootServlet() {
3031
this(WebHttpHandlerBuilder
31-
.webHandler(toHttpHandler(routingFunction()))
32+
.webHandler((WebHandler) toHttpHandler(routingFunction()))
3233
.prependFilter(new IndexRewriteFilter())
3334
.build());
3435
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.web;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class PathPatternController {
10+
11+
@GetMapping("/spring5/{*id}")
12+
public String URIVariableHandler(@PathVariable String id) {
13+
return id;
14+
}
15+
16+
@GetMapping("/s?ring5")
17+
public String wildcardTakingExactlyOneChar() {
18+
return "/s?ring5";
19+
}
20+
21+
@GetMapping("/spring5/*id")
22+
public String wildcardTakingZeroOrMoreChar() {
23+
return "/spring5/*id";
24+
}
25+
26+
@GetMapping("/resources/**")
27+
public String wildcardTakingZeroOrMorePathSegments() {
28+
return "/resources/**";
29+
}
30+
31+
@GetMapping("/{baeldung:[a-z]+}")
32+
public String regexInPathVariable(@PathVariable String baeldung) {
33+
return baeldung;
34+
}
35+
36+
@GetMapping("/{var1}_{var2}")
37+
public String multiplePathVariablesInSameSegment(@PathVariable String var1, @PathVariable String var2) {
38+
return "Two variables are var1=" + var1 + " and var2=" + var2;
39+
}
40+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.baeldung.functional;
2+
3+
import org.junit.AfterClass;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import org.springframework.boot.web.server.WebServer;
7+
import org.springframework.test.web.reactive.server.WebTestClient;
8+
9+
public class ExploreSpring5URLPatternUsingRouterFunctionsTest {
10+
11+
private static WebTestClient client;
12+
private static WebServer server;
13+
14+
@BeforeClass
15+
public static void setup() throws Exception {
16+
server = new ExploreSpring5URLPatternUsingRouterFunctions().start();
17+
client = WebTestClient.bindToServer()
18+
.baseUrl("http://localhost:" + server.getPort())
19+
.build();
20+
}
21+
22+
@AfterClass
23+
public static void destroy() {
24+
server.stop();
25+
}
26+
27+
@Test
28+
public void givenRouter_whenGetPathWithSingleCharWildcard_thenGotPathPattern() throws Exception {
29+
client.get()
30+
.uri("/paths")
31+
.exchange()
32+
.expectStatus()
33+
.isOk()
34+
.expectBody(String.class)
35+
.isEqualTo("/p?ths");
36+
}
37+
38+
@Test
39+
public void givenRouter_whenMultipleURIVariablePattern_thenGotPathVariable() throws Exception {
40+
client.get()
41+
.uri("/test/ab/cd")
42+
.exchange()
43+
.expectStatus()
44+
.isOk()
45+
.expectBody(String.class)
46+
.isEqualTo("/ab/cd");
47+
}
48+
49+
@Test
50+
public void givenRouter_whenGetMultipleCharWildcard_thenGotPathPattern() throws Exception {
51+
52+
client.get()
53+
.uri("/wildcard")
54+
.exchange()
55+
.expectStatus()
56+
.isOk()
57+
.expectBody(String.class)
58+
.isEqualTo("/*card path was accessed");
59+
}
60+
61+
@Test
62+
public void givenRouter_whenGetMultiplePathVaribleInSameSegment_thenGotPathVariables() throws Exception {
63+
64+
client.get()
65+
.uri("/baeldung_tutorial")
66+
.exchange()
67+
.expectStatus()
68+
.isOk()
69+
.expectBody(String.class)
70+
.isEqualTo("baeldung , tutorial");
71+
}
72+
73+
@Test
74+
public void givenRouter_whenGetRegexInPathVarible_thenGotPathVariable() throws Exception {
75+
76+
client.get()
77+
.uri("/abcd")
78+
.exchange()
79+
.expectStatus()
80+
.isOk()
81+
.expectBody(String.class)
82+
.isEqualTo("/{baeldung:[a-z]+} was accessed and baeldung=abcd");
83+
84+
client.get()
85+
.uri("/1234")
86+
.exchange()
87+
.expectStatus()
88+
.is4xxClientError();
89+
}
90+
91+
@Test
92+
public void givenResources_whenAccess_thenGot() throws Exception {
93+
client.get()
94+
.uri("/files/test/test.txt")
95+
.exchange()
96+
.expectStatus()
97+
.isOk()
98+
.expectBody(String.class)
99+
.isEqualTo("test");
100+
101+
client.get()
102+
.uri("/files/hello.txt")
103+
.exchange()
104+
.expectStatus()
105+
.isOk()
106+
.expectBody(String.class)
107+
.isEqualTo("hello");
108+
}
109+
110+
}

0 commit comments

Comments
 (0)