Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ You can combine multiple route predicate factories with the `RequestPredicate.an

The `After` route predicate factory takes one parameter, a `datetime` (which is a java `ZonedDateTime`).
This predicate matches requests that happen after the specified datetime.
The datetime can be specified as a ZonedDateTime string or as epoch milliseconds.
The following example configures an after route predicate:

.application.yml
Expand All @@ -27,6 +28,22 @@ spring:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
----

The datetime can also be specified as epoch milliseconds:

.application.yml
[source,yaml]
----
spring:
cloud:
gateway:
mvc:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=1484968967789
----

.GatewaySampleApplication.java
[source,java]
----
Expand Down Expand Up @@ -56,6 +73,7 @@ This route matches any request made after Jan 20, 2017 17:42 Mountain Time (Denv

The `Before` route predicate factory takes one parameter, a `datetime` (which is a java `ZonedDateTime`).
This predicate matches requests that happen before the specified `datetime`.
The datetime can be specified as a ZonedDateTime string or as epoch milliseconds.
The following example configures a before route predicate:

.application.yml
Expand All @@ -72,6 +90,22 @@ spring:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
----

The datetime can also be specified as epoch milliseconds:

.application.yml
[source,yaml]
----
spring:
cloud:
gateway:
mvc:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=1484968967789
----

.GatewaySampleApplication.java
[source,java]
----
Expand Down Expand Up @@ -103,6 +137,7 @@ The `Between` route predicate factory takes two parameters, `datetime1` and `dat
which are java `ZonedDateTime` objects.
This predicate matches requests that happen after `datetime1` and before `datetime2`.
The `datetime2` parameter must be after `datetime1`.
The datetimes can be specified as ZonedDateTime strings or as epoch milliseconds.
The following example configures a between route predicate:

.application.yml
Expand All @@ -119,6 +154,22 @@ spring:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
----

The datetimes can also be specified as epoch milliseconds:

.application.yml
[source,yaml]
----
spring:
cloud:
gateway:
mvc:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=1484968967789, 1485055367789
----

.GatewaySampleApplication.java
[source,java]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
import org.springframework.cloud.gateway.server.mvc.invoke.reflect.ReflectiveOperationInvoker;
import org.springframework.cloud.gateway.server.mvc.predicate.PredicateBeanFactoryDiscoverer;
import org.springframework.cloud.gateway.server.mvc.predicate.PredicateDiscoverer;
import org.springframework.cloud.gateway.server.mvc.support.StringToZonedDateTimeConverter;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.Environment;
import org.springframework.core.log.LogMessage;
Expand All @@ -82,6 +84,7 @@
*
* @author Spencer Gibb
* @author Jürgen Wißkirchen
* @author raccoonback
*/
public class RouterFunctionHolderFactory {

Expand Down Expand Up @@ -112,7 +115,7 @@ public String toString() {

private final PredicateDiscoverer predicateDiscoverer = new PredicateDiscoverer();

private final ParameterValueMapper parameterValueMapper = new ConversionServiceParameterValueMapper();
private final ParameterValueMapper parameterValueMapper;

private final BeanFactory beanFactory;

Expand Down Expand Up @@ -140,6 +143,12 @@ public RouterFunctionHolderFactory(Environment env, BeanFactory beanFactory,
else {
this.conversionService = DefaultConversionService.getSharedInstance();
}

if (this.conversionService instanceof ConfigurableConversionService configurableConversionService) {
configurableConversionService.addConverter(new StringToZonedDateTimeConverter());
}

this.parameterValueMapper = new ConversionServiceParameterValueMapper(this.conversionService);
Comment on lines +147 to +151
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom StringToZonedDateTimeConverter is not registered in the shared ConversionService instance, so this PR explicitly registers it in the actual ConversionService used by WebMVC. This ensures that datetime predicates can correctly parse both ISO-8601 strings and epoch millisecond values.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public static RequestPredicate before(ZonedDateTime dateTime) {
return request -> ZonedDateTime.now().isBefore(dateTime);
}

// TODO: accept and test datetime predicates (including yaml config)
@Shortcut
public static RequestPredicate between(ZonedDateTime dateTime1, ZonedDateTime dateTime2) {
return request -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2025-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.server.mvc.support;

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import org.springframework.core.convert.converter.Converter;

/**
* Converter for converting String to ZonedDateTime. Supports both ISO-8601 format and
* epoch milliseconds.
*
* @author raccoonback
*/
public class StringToZonedDateTimeConverter implements Converter<String, ZonedDateTime> {

@Override
public ZonedDateTime convert(String source) {
try {
long epoch = Long.parseLong(source);
return Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.ofTotalSeconds(0)).toZonedDateTime();
}
catch (NumberFormatException e) {
// try ZonedDateTime instead
return ZonedDateTime.parse(source);
}
}

}
Comment on lines +31 to +45
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading
Loading