1 year ago
#381294
smileis2333
can i controll the @Valid, @Transactional order in the web tier
@RestController
public class GoodsController {
@Autowired
private GoodsDao goodsDao;
@Autowired
private GoodsService goodsService;
@PostMapping("test1")
@Transactional
public String test1(@RequestBody @Valid GoodsSaveParam goodsSaveParam) {
goodsDao.selectOne(new QueryWrapper<Goods>().eq("code", goodsSaveParam.getGoodsCode()));
return "test1";
}
@PostMapping("test2")
@Transactional
public String test2(@RequestBody GoodsSaveParam goodsSaveParam) {
goodsService.updateById(goodsSaveParam);
return "test2";
}
}
@Data
public class GoodsSaveParam {
@GC
private String goodsCode;
private String goodsName;
}
@Component
public class GCValidator implements ConstraintValidator<GC, String> {
@Autowired
private GoodsDao goodsDao;
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
goodsDao.selectOne(new QueryWrapper<Goods>().eq("code", value));
return true;
}
}
@Service
@Validated
public class GoodsService {
@Autowired
private GoodsDao goodsDao;
public void updateById(@Valid GoodsSaveParam goodsSaveParam) {
goodsDao.selectOne(new QueryWrapper<Goods>().eq("code", goodsSaveParam.getGoodsCode()));
}
}
I have a GoodsController
and write 2 test method(test1 and test2) implement the same logic(each logic query the same thing twice) except the annotation location, i mean the @Transational
and @Valid
,in the method test1, the validator and test1's login is not hit the cache. in the test2, i wrap the query login into a class and put @Valid
into its'method signature, so the second can hit the session cache. the test2 is obvious call that the validator must be in the transanction. So if there have any method for user to implement same effect in form.
spring-boot
bean-validation
spring-transactions
0 Answers
Your Answer