【问题标题】:How to pass entity object to controller from custom validation annotation如何从自定义验证注释将实体对象传递给控制器
【发布时间】:2018-03-16 11:08:46
【问题描述】:

我有一个请求 data transfer object(DTO) 用于接受 API 的 POST 正文。该 DTO 有一个 X 实体的 ENUM 键。我创建了一个自定义验证注释,并通过其实体存储库验证 ENUM 是否存在于我的系统中,该存储库根据其存在返回实体或 null。

我想将经过 X 验证的实体传递给我的控制器。我不想在我的业务逻辑中再次通过 ENUM 获取 X 实体,因为我已经在验证中完成了。

我将该实体保存在注释验证器类的静态变量中,稍后当我需要在业务逻辑中访问它但如果 API 被同时命中时,我的静态变量将被第二个请求和我的第一个业务逻辑覆盖请求获取X实体,由于覆盖问题,它获取第二个请求的实体。

我希望 X 实体对象的范围在其自己的请求周期内,并在请求完成后像其他对象一样被销毁或 GC。我怎样才能做到这一点?

我的自定义验证器:

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ValidateXimpl.class)
@Documented
public @interface ValidateX {
    String message()  default "Message";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default{};
}

自定义验证器实现:

public class ValidateXimpl implements ConstraintValidator<ValidateX, String> {

    @Autowired
    DBRepo dbRepo;

    @Override
    public void initialize(ValidateX annotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext ctx) {
        try {
            XEntity X = dbRepo.findByEnum(value); // I want this entity in my controller
            return (!value.isEmpty() && X != null);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

我放置验证的 DTO:

public class DTO{
    ....
    @ValidateX
    String xEntityEnum
    ....
}

我的控制器:

@RequestMapping(value = '/x', method = RequestMethod.POST)
public @ResponseBody
Map<String, Object> createX(@Validated @RequestBody DTO dto, BindingResult errors) {
    // I want that entity here which I get from db repo in my validator
}

【问题讨论】:

  • 如果您使用表单对象来接收您的请求参数,您可以验证该对象中的字段并将其传递给控制器​​方法。
  • 不,我在自定义验证器中创建了另一个对象,我希望将该对象传递给控制器​​。
  • 嗯好的。一种方法可能是仅验证 indata,然后在 dto 中的该字段的 get 方法中创建新对象。那行得通吗?顺便说一句,一些代码会有所帮助
  • 您的项目符号列表使您的问题无法阅读。请改写你的问题。除非您确实列出项目,否则不要使用项目符号列表。对于问题,请使用普通散文。

标签: java spring spring-mvc spring-boot spring-security


【解决方案1】:

我刚刚从RequestContextHolder解决了这个问题

解决方案代码:

验证器实现

public class ValidateXimpl implements ConstraintValidator<ValidateX, String> {

@Autowired
DBRepo dbRepo;

@Override
public void initialize(ValidateX annotation) {
}

@Override
public boolean isValid(String value, ConstraintValidatorContext ctx) {
    try {
        XEntity X = dbRepo.findByEnum(value); // I want this entity in my controller
        RequestContextHolder.getRequestAttributes().setAttribute(
                "XEntity", X, RequestAttributes.SCOPE_REQUEST
        ); // I saved it in RequestContextHolder
        return (!value.isEmpty() && X != null);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
}

我的控制器

@RequestMapping(value = '/x', method = RequestMethod.POST)
public @ResponseBody
Map<String, Object> createX(@Validated @RequestBody DTO dto, BindingResult errors) {
     // I want that entity here which I get from db repo in my validator
    System.out.println(
            RequestContextHolder.getRequestAttributes().getAttribute(
                    "XEntity",
                    RequestAttributes.SCOPE_REQUEST
            )
    ); // I get that from RequestContextHolder
}

这是我真正需要的。 RequestContextHolder 在请求完成时会被清除,并且由于 setAttribute 函数中的请求范围设置,它也只能在其自己的请求线程中使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2022-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多