Skip to content

Using @Valid on a container (java.util.List) is deprecated - #4250

Open
verbitan wants to merge 2 commits into
spring-cloud:mainfrom
verbitan:HV000271
Open

Using @Valid on a container (java.util.List) is deprecated#4250
verbitan wants to merge 2 commits into
spring-cloud:mainfrom
verbitan:HV000271

Conversation

@verbitan

Copy link
Copy Markdown

Summary

Fixes deprecation warnings from Hibernate Validator about applying @Valid directly to container types.

Detail

Spring Boot 4.1.0 upgraded to Hibernate Validator 9.1 which included a change deprecating the use of @Valid at the container level.

Meaning that this:

class MyBean {
    @Valid
    List<MyContainerElement> list;
}

Should be replaced by this:

class MyBean {
    List<@Valid MyContainerElement> list;
}

Else these warnings are raised:

o.h.v.i.m.a.CascadingMetaDataBuilder     : HV000271: Using `@Valid` on a container (java.util.List) is deprecated. You should apply the annotation on the type argument(s). Affected element: routes
o.h.v.i.m.a.CascadingMetaDataBuilder     : HV000271: Using `@Valid` on a container (java.util.List) is deprecated. You should apply the annotation on the type argument(s). Affected element: predicates
o.h.v.i.m.a.CascadingMetaDataBuilder     : HV000271: Using `@Valid` on a container (java.util.List) is deprecated. You should apply the annotation on the type argument(s). Affected element: filters

Signed-off-by: verbitan <verbitan@users.noreply.github.com>
@HDPark95

Copy link
Copy Markdown

One container-level @Valid in this change set looks like it was missed: GatewayMvcProperties.routesMap. It sits two fields below the routes you fixed in the same file and still triggers the same HV000271 — the deprecation applies to any container, Map included:

@NotNull
@Valid
private LinkedHashMap<String, RouteProperties> routesMap = new LinkedHashMap<>();

It's a live cascade target (bound as spring.cloud.gateway.server.webmvc.routes-map, iterated in RouterFunctionHolderFactory), so its RouteProperties values are validated. For a map the element annotation goes on the value type argument, mirroring the routes edit above:

@NotNull
private LinkedHashMap<String, @Valid RouteProperties> routesMap = new LinkedHashMap<>();

@NotNull stays on the field (it constrains the map reference, not the cascade). The three warnings quoted in the description are the webflux fields; the webmvc side logs the same for routesMap once those properties are bound and validated.

@spencergibb spencergibb changed the title HV000271: Using @Valid on a container (java.util.List) is deprecated Using @Valid on a container (java.util.List) is deprecated Jul 29, 2026
Signed-off-by: verbitan <verbitan@users.noreply.github.com>
@verbitan

Copy link
Copy Markdown
Author

One container-level @Valid in this change set looks like it was missed: GatewayMvcProperties.routesMap. It sits two fields below the routes you fixed in the same file and still triggers the same HV000271 — the deprecation applies to any container, Map included:

@NotNull
@Valid
private LinkedHashMap<String, RouteProperties> routesMap = new LinkedHashMap<>();

It's a live cascade target (bound as spring.cloud.gateway.server.webmvc.routes-map, iterated in RouterFunctionHolderFactory), so its RouteProperties values are validated. For a map the element annotation goes on the value type argument, mirroring the routes edit above:

@NotNull
private LinkedHashMap<String, @Valid RouteProperties> routesMap = new LinkedHashMap<>();

@NotNull stays on the field (it constrains the map reference, not the cascade). The three warnings quoted in the description are the webflux fields; the webmvc side logs the same for routesMap once those properties are bound and validated.

Good spot, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants