我正在使用带有spring-data-rest的springboot 2.0.1.RELEASE,并按照此处提到的解决方法进行操作,但仍然没有调用我的Validator。以下是详细信息:
ValidatorRegistrar:Workaround for a bug
@Configuration
public class ValidatorRegistrar implements InitializingBean {
private static final List<String> EVENTS;
static {
List<String> events = new ArrayList<String>();
events.add("beforeCreate");
events.add("afterCreate");
events.add("beforeSave");
events.add("afterSave");
events.add("beforeLinkSave");
events.add("afterLinkSave");
events.add("beforeDelete");
events.add("afterDelete");
EVENTS = Collections.unmodifiableList(events);
}
@Autowired
ListableBeanFactory beanFactory;
@Autowired
ValidatingRepositoryEventListener validatingRepositoryEventListener;
@Override
public void afterPropertiesSet() throws Exception {
Map<String, Validator> validators = beanFactory.getBeansOfType(Validator.class);
for (Map.Entry<String, Validator> entry : validators.entrySet()) {
EVENTS.stream().filter(p -> entry.getKey().startsWith(p)).findFirst()
.ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue()));
}
}
}
验证员类:
@Component("beforeSaveBidValidator")
public class BeforeSaveBidValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Bid.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
Bid bid = (Bid)target;
if (!bid.getAddendaAcknowledged()) {
errors.rejectValue("addendaAcknowledged",
"addendaAcknowledged is not true");
}
}
}
自定义RestController for Bids:
@RestController
@RequestMapping(path = "/bids")
public class BidController {
private BidRepository bidRepository;
@Autowired
public BidController(
BidRepository bidRepository) {
this.bidRepository = bidRepository;
}
@PutMapping("{id}")
public Bid update(@RequestBody @Valid Bid bid) {
return bidRepository.save(bid);
}
}
Rest客户测试代码:
Bid bid = new Bid()
...
bid.setAddendaAcknowledged(false)
Map<String, String> uriVariables = new HashMap<String, String>()
uriVariables.put("id", bid.id)
HttpHeaders headers = new HttpHeaders()
headers.setContentType(MediaType.APPLICATION_JSON)
HttpEntity<Bid> entity = new HttpEntity<>(bid, headers)
ResponseEntity<String> response = restTemplate.exchange(
"/bids/{id}", HttpMethod.PUT, entity, Bid.class, bid.id)
// Expected: response.statusCode == HttpStatus.BAD_REQUEST
// Found: response.statusCode == HttpStatus.OK
// Debugger showed that Validator was never invoked.
知道我缺少什么吗?
答案 0 :(得分:1)
您正在尝试将验证器与自定义控制器配合使用,而不是SDR控制器。在这种情况下,您只需使用@InitBinder
注释将其添加到控制器:
@RestController
@RequestMapping("/bids")
public class BidController {
//...
@InitBinder("bid") // add this parameter to apply this binder only to request parameters with this name
protected void bidValidator(WebDataBinder binder) {
binder.addValidators(new BidValidator());
}
@PutMapping("/{id}")
public Bid update(@RequestBody @Valid Bid bid) {
return bidRepository.save(bid);
}
}
验证器上的 @Component
注释不需要ValidatorRegistrar
类。
如何使用SDR控制器的验证器,您可以在我的另一个answer中阅读。