简体   繁体   English

SpringBoot:没有@Valid注解的验证

[英]SpringBoot: Validation without @Valid annotation

I have a web socket handler inherited from AbstractWebSocketHandler that handles text messages.我有一个从处理文本消息的AbstractWebSocketHandler继承的 web 套接字处理程序。 My DTOs use javax.validation.constraints for validation.我的 DTO 使用javax.validation.constraints进行验证。 So, in my REST endpoints, I simply can use the @Valid annotation to invoke the validator.因此,在我的 REST 端点中,我只需使用@Valid注释来调用验证器。 However, as far as I know, this annotation is not usable in my web socket handler.但是,据我所知,此注释在我的 web 套接字处理程序中不可用。 How can I invoke the SpringBoot validator programmatically without this annotation?如何在没有此注释的情况下以编程方式调用 SpringBoot 验证器?

Besides, is it possible to use the SpringBoot de-serializer for messages instead of JSON.parseObject ?此外,是否可以对消息使用 SpringBoot 反序列化器而不是JSON.parseObject

Example:例子:

import javax.validation.constraints.NotBlank;
import lombok.Data;

@Data
class CustomMessage {
    @NotBlank
    private String text;
}
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import lombok.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

@Component
@Slf4j
public class MyCustomWebSocketHandler extends AbstractWebSocketHandler {
    @Override
    protected void handleTextMessage(@NonNull WebSocketSession session, @NonNull TextMessage message) {
        CustomMessage customMessage = JSON.parseObject(message.getPayload(), CustomMessage.class);
        // Validate the message according to javax.validation annotations and throw MethodArgumentNotValidException if invalid
        log.debug("Received valid message {}", customMessage)
    }
}

You will use a Validator to fill a list of ConstraintViolation .您将使用Validator来填充ConstraintViolation列表。 An example could looks like this:一个示例可能如下所示:

public abstract class GenericService<T> {

    protected Validator validator;

    protected void validateDomainRecord(T object, String message) {
        Set<ConstraintViolation<T>> violations = validator.validate(object);
        if(!violations.isEmpty()) {
            throw new ConstraintViolationException(message, violations);
        }
    }
}

In your case, your code will looks something like this:在您的情况下,您的代码将如下所示:

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import lombok.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

@Component
@Slf4j
public class MyCustomWebSocketHandler extends AbstractWebSocketHandler {

    private Validator validator;
    
    @Override
    protected void handleTextMessage(@NonNull WebSocketSession session, @NonNull TextMessage message) {
        CustomMessage customMessage = JSON.parseObject(message.getPayload(), CustomMessage.class);
        // Validate the message according to javax.validation annotations and throw MethodArgumentNotValidException if invalid
        Set<ConstraintViolation<CustomMessage>> violations = validator.validate(customMessage);
        if(!violations.isEmpty()) {
            throw new ConstraintViolationException(message, violations);
        }

        log.debug("Received valid message {}", customMessage)
    }
}

Take look a this good tutorial for more details.看看这个很好的教程了解更多细节。 I guess it is also possible to customize your validation and your exception too.我想也可以自定义您的验证和异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM