Spring Boot Sping Boot 3.2:查询参数的WebMVC数据绑定的行为更改

9fkzdhlc  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(55)

当尝试将我们的Sping Boot + Spring Web based REST API从Sping Boot 3.1.6升级到3.2.0时,我们在查询参数的数据绑定中遇到了意外的行为更改。在某些情况下,它会导致错误,在其他情况下,它可能会导致棘手的bug。
下面的例子展示了我们的情况:一个REST控制器,它有一个简单的GET端点,该端点将其参数作为一个包含嵌套对象的对象进行检索:

@RestController
@RequestMapping("/builder")
public class DemoLombokDataBuilder {

   @Data
   @Builder
   static class Dto {
      String field;
      Nested nested;
   }

   @Data
   static class Nested {
      String nestedField;
   }

   @GetMapping
   public String demo(Dto dto) {
      String result = dto.getField();

      if (dto.getNested() != null)
         result += dto.getNested().getNestedField();
      else
         result += ", nested is empty";

      return result;
   }

字符串
在Sping Boot 3.1.6中,使用或不使用嵌套字段调用端点的结果是:
| URL|结果|
| --|--|
| http://localhost:8080/builder?field = foo| foo,nested是空的|
| http://localhost:8080/builder?field = foo & nested. nestedField = bar| foobar|
然而,使用Sping Boot 3.2.0,结果是:
| URL|结果|
| --|--|
| http://localhost:8080/builder?field = foo|foonull|
| http://localhost:8080/builder?field = foo & nested. nestedField = bar| foobar|
因此,Spring 3.2不是在没有提供字段时根本不示例化嵌套对象,而是使用空值示例化嵌套对象。这可能导致bug-当使用验证并且嵌套对象具有用@NotNull注解的强制字段时,情况更糟。在这种情况下,Sping Boot 3.2抛出HTTP 400,而请求在3.1中工作正常。

**其他人是否遇到过类似的问题,或者我们是否错过了文档或升级指南中的某些内容?**现在,这对我们来说似乎是一个bug,因为它可能会由于运行时行为的改变而导致棘手的bug。

  • 更多详情 *

在我进一步的测试中,只有当DTO类只能通过all-args-constructor构造时才会出现这个问题。使用这个DTO:

@Data
   static class Dto {

      String field;
      Nested nested;
   }


3.1和3.2的结果相同:
| URL|结果|
| --|--|
| http://localhost:8080/builder?field = foo| foo,nested是空的|
| http://localhost:8080/builder?field = foo & nested. nestedField = bar| foobar|
我把完整的例子推到了这个Github仓库:https://github.com/flpa/springboot-databinder-change-tests

s71maibg

s71maibg1#

这是Spring Framework中的一个bug,它已被修复并标记为在Spring Framework版本6.1.2中发布。
有关更多信息,请参阅Github issue https://github.com/spring-projects/spring-framework/issues/31821

相关问题