Description
The Spring R2DBC URL parser fails when handling certain database URLs, specifically throwing an "Illegal character in path at index 88" error. This appears to be related to URL length and character positioning, as shorter URLs with similar patterns work correctly.
Environment
- Spring Boot: 3.3.1
- Spring Framework: 6.1.10
- Java: JDK 22
Steps to Reproduce
- Configure R2DBC with the following URL (FAILING URL):
r2dbc:postgresql://app-service-alpha-stg.abc123def456.sa-east-1.rds.amazonaws.com:37039/mydb1
- Start the application
Error Message
Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 88: r2dbc://app-service-alpha-stg.abc123def456.sa-east-1.rds.amazonaws.com:37039/mydb1 at java.base/java.net.URI.create(URI.java:932) at io.r2dbc.spi.ConnectionUrlParser.parseQuery(ConnectionUrlParser.java:93) at io.r2dbc.spi.ConnectionFactoryOptions.parse(ConnectionFactoryOptions.java:138)
Expected Behavior
The URL should be parsed successfully, as it's a valid PostgreSQL connection URL that works with JDBC and can connect successfully using other tools like DBeaver.
Actual Behavior
The application fails to start with an IllegalArgumentException about an illegal character at index 88. However, important to note: 1. The same exact URL works in DBeaver and other JDBC clients 2. The database is accessible using this connection string 3. Spring's R2DBC parser is the only component that fails to handle this URL
Interesting Observations
-
Similar but shorter URLs work fine. For example (WORKING URL): r2dbc:postgresql://app-service-beta.xyz789uvw321.us-east-1.rds.amazonaws.com:37039/mydb1
-
Key differences between working and failing URLs:
- Failing URL is 99 characters long
- Working URL is 95 characters
- Failing URL has additional segments in hostname (-stg)
-
These differences cause different character positioning
-
The error occurs specifically at index 88, where in the failing URL a '/' character is positioned
Workaround
The issue can be worked around by implementing a custom connection factory that bypasses Spring's URL parser:
```java @Configuration class R2dbcConfig { @Value("${spring.r2dbc.url}") private String url;
@Value("${spring.r2dbc.username}")
private String username;
@Value("${spring.r2dbc.password}")
private String password;
@Bean
public ConnectionFactory connectionFactory() {
String urlWithoutPrefix = url.replace("r2dbc:postgresql://", "");
String[] parts = urlWithoutPrefix.split("/");
String hostAndPort = parts[0];
String database = parts[1].trim();
String[] hostParts = hostAndPort.split(":");
String host = hostParts[0];
int port = Integer.parseInt(hostParts[1]);
return new PostgresqlConnectionFactory(
PostgresqlConnectionConfiguration.builder()
.host(host)
.port(port)
.database(database)
.username(username)
.password(password)
.build()
);
}
}
Comment From: quaff
It's thrown by java.net.URI.create(), it may be caused by outdated JDK, you should test it against JDK directly.
Comment From: dev-jonghoonpark
I tested it with the following settings and had no problems. - temurin 21.0.4 - spring-r2dbc 6.2.1 - spring-data-r2dbc 3.4.1 - r2dbc-postgresql1.0.7.RELEASE
I tested using the URL provided as is (by modifying the hosts file)
r2dbc:postgresql://app-service-alpha-stg.abc123def456.sa-east-1.rds.amazonaws.com:37039/mydb1