-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Description
I defined a REST endpoint that accepts a MatrixVariable
. I did some testing, and concluded that the MatrixVariable
is not processed the same way depending on the container. The v1 endpoint ends with a "static" path, whereas the v2 endpoint ends with a PathVariable
.
@RestController
public class BugResource {
@GetMapping(value = "/v1/{a}/plus/{b}/equal", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> bug(
@PathVariable String a,
@PathVariable String b,
@MatrixVariable(required = false) String matrix
) {
return ResponseEntity.ok(a + " " + b + " " + matrix);
}
@GetMapping(value = "/v2/{a}/plus/{b}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> bug2(
@PathVariable String a,
@PathVariable String b,
@MatrixVariable(required = false) String matrix
) {
return ResponseEntity.ok(a + " " + b + " " + matrix);
}
}
Then I tried to call the REST endpoint:
with Tomcat
http://localhost:8080/v1/a;matrix=matrix/plus/b/equal => 200
http://localhost:8080/v1/a/plus/b;matrix=matrix/equal => 200
http://localhost:8080/v2/a/plus/b;matrix=matrix => 200
http://localhost:8080/v2/a;matrix=matrix/plus/b => 200
with Undertow
http://localhost:8080/v1/a;matrix=matrix/plus/b/equal => 404
http://localhost:8080/v1/a/plus/b;matrix=matrix/equal => 404
http://localhost:8080/v2/a/plus/b;matrix=matrix => 200
http://localhost:8080/v2/a;matrix=matrix/plus/b => 404
So it seems that Undertow only deals with MatrixVariable
when the endpoint ends with a PathVariable
, and the MatrixVariable
is placed at the end of the endpoint
To reproduce, I just created a Spring Initializr maven project:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>matrixparam</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>matrixparam</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The configuration to enable MatrixVariable
:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}