2

我正在使用 jsonschema2pojo 从 json 模式生成 POJO。我想使用 jsr303/349 bean 验证的东西。我将必要的项目添加到类路径中,添加了必要的 bean 以触发验证,但是 jsonschema2pojo 不会添加@org.springframework.validation.annotation.Validated到生成的类中,因此当请求进入我的 Spring Boot 应用程序时不会触发验证。

@RequestBody通过像这样编写一个空类并将类型更改为新类型,我能够确认我的验证器设置正确:

@Validated
class SomeClass extends SomeGeneratedClass {

}

当我这样做时,验证按预期工作。但是,我们正在研究数十个(如果不是可能有一百个或更多)这些扩展对象,并且其中有一堆是湿(IE,而不是 DRY)代码的缩影,因此这不是理想的解决方案。

所以我的问题是:如果有问题的对象没有用 注释,是否有办法在传入请求中触发 bean 验证@Validated?请注意,jsonschema2pojo 目前对 Spring 没有依赖关系,我发现作者不太可能接受添加一个的拉取请求。

-- 有帮助的代码

一个示例 JSON 模式:

{
  "type": "object",
  "properties": {
    "userIds": {
      "type": "array",
      "items": { "type": "string" },
      "minSize": 1
    }
  },
  "additionalProperties": false,
  "required": ["userIds"]
}

生成的类:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "userIds"
})
public class ExampleSchema {

    /**
     * 
     * (Required)
     * 
     */
    @JsonProperty("userIds")
    @Valid
    @NotNull
    private List<String> userIds = new ArrayList<String>();

    /**
     * 
     * (Required)
     * 
     */
    @JsonProperty("userIds")
    public List<String> getUserIds() {
        return userIds;
    }

    /**
     * 
     * (Required)
     * 
     */
    @JsonProperty("userIds")
    public void setUserIds(List<String> userIds) {
        this.userIds = userIds;
    }

    public ExampleSchema withUserIds(List<String> userIds) {
        this.userIds = userIds;
        return this;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(userIds).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof ExampleSchema) == false) {
            return false;
        }
        ExampleSchema rhs = ((ExampleSchema) other);
        return new EqualsBuilder().append(userIds, rhs.userIds).isEquals();
    }
}

我的 WebConfig 中的验证 bean 设置:

    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor(LocalValidatorFactoryBean validator) {
        final MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
        methodValidationPostProcessor.setValidator(validator);

        return methodValidationPostProcessor;
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

我的控制器方法:

@RequestMapping(value = "", method = RequestMethod.POST)
public void postExample(@RequestBody @Valid ExampleSchema example) {
    //Perform actions on validated object
}
4

3 回答 3

1

在您的课程中,您在同一行中定义和实例化数组。

private List<String> userIds = new ArrayList<String>();

因此,当 Spring 验证对象时,List userIds 不为空。

您在这里有两个选择。

  1. 您可以删除 userIds 的实例化。

    私有列表用户 ID;

  2. 您可以更改验证。@NotNull你可以使用或者你可以同时使用它们而不是 @Size(min=1)使用它们。你也可以使用@NotEmpty,但你需要休眠验证器。

于 2017-04-27T19:38:56.420 回答
0

您可以注释ControllerService使用@Validated而不是对所有需要验证的对象进行注释,如下所示:

@Validated
@Controller
public class Controller {

    @RequestMapping(value = "", method = RequestMethod.POST)
    public void postExample(@RequestBody @Valid ExampleSchema example){
       //Perform actions on validated object
    }
}
于 2017-04-27T16:48:54.203 回答
0

在你的控制器中创建一个 initbinder,如下所示:

@Autowired
YourValidator validator;

@InitBinder()
protected void initBinder(WebDataBinder binder) {

    binder.setValidator(validador);

}

在你的端点

public OrdenServicioTo guardarOrden(@ModelAttribute("dto")@Validated @RequestBody Object dto)
{
    ///YOurCode
}
于 2017-04-27T20:08:58.713 回答