引用类型为嵌套字段的验证对象返回FeignException

时间:2018-07-30 18:59:18

标签: java spring validation custom-validators netflix-feign

我对自定义验证程序有疑问。

项目是基于spring boot和netflix虚拟客户端实现的

我的网络服务很少:

1)全人服务

@RestController
@RequestMapping(value = "/fullApp/{fullAppId}/person")
public class FullAppPersonController  extends CrudController<FullAppPerson> implements FullAppPersonServiceApi {

    @Autowired
    private FullAppPersonService personService;

    @Autowired
    private Validator validator;

    @Override
    @PutMapping(value = "/list")
    public Collection<FullAppPerson> updateFullAppPersonList(@PathVariable(value = "id") Long id, @RequestBody Collection<FullAppPerson> fapList) {
        validateCollectionFullAppPerson(fapList, Update.class);
        return personService.updateFullAppPersonList(id, fapList);
    }


    private void validateCollectionFullAppPerson(Collection<FullAppPerson> fullAppPersonList, Class<?>... validationGroups){
        Set<ConstraintViolation<FullAppPerson>> violations = new HashSet<>();
        fullAppPersonList.forEach(fullAppPerson -> violations.addAll(validator.validate(fullAppPerson, validationGroups)));
        if (!violations.isEmpty()) {
            throw new FullAppPersonValidationException("List is not passed validation:", violations);
        }
    }

/**other code***/
}

DTO FullAppPerson:

public class FullAppPerson {

    @NotNull(message = "Person is empty")
    @Valid
    private Person person;

    /**other field*/
    /*constructors and ALL getters and setters*/ 
}

2)个人服务

DTO人员:

public class Person {

    private Long partnerId;

    @InnValidation(groups = {Create.class, Update.class})
    private String inn;
}

当我打电话给执行验证的全人服务的Controller并使用Debugger连接到代码时,我可以逐步看到验证的所有阶段。

在控制器获取有效对象的情况下-可以。 如果人员服务获取的对象无效-我会收到以下消息:

{
    "timestamp": "2018-07-27",
    "status": 500,
    "error": "Internal Server Error",
    "exception": "java.lang.RuntimeException",
    "message": "person-service conection is failed feign.FeignException: status 500 reading PersonServiceApi#update(Long,Person); content:\n{\"notifications\":[{\"notificationCode\":\"ER_UNSPECIFIED\",\"notificationMessage\":\"validation exception\",\"notificationType\":\"ERROR\",\"notificationDetails\":{\"complexMessage\":\"JSR-303 validated property 'inn.inn' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)\"}}],\"hasErrors\":true,\"data\":{\"requestUrl\":\"http://13.234.23.168:8080/person/592\",\"method\":\"PUT\",\"exceptionName\":\"java.lang.IllegalStateException\",\"causesMessages\":[\"JSR-303 validated property 'inn.inn' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)\",\"Invalid property 'inn.inn' of bean class [com.ps.dto.Person]: Bean property 'inn.inn' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?\"]}}",
    "path": "/fullApp/240/person/list"
}

请帮助我理解这种行为

(我想如果3party服务中的嵌套类返回约束违例集,则会出错。)

0 个答案:

没有答案