1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > api分层内部外部 spring_java - Spring boot restful API分层架构验证 - SO中文参考 - www.soinside.com...

api分层内部外部 spring_java - Spring boot restful API分层架构验证 - SO中文参考 - www.soinside.com...

时间:2020-05-23 14:37:04

相关推荐

api分层内部外部 spring_java - Spring boot restful API分层架构验证 - SO中文参考 - www.soinside.com...

[今天,我与我们的一位团队成员就Controller和Service层中的RESTful API输入的验证进行了大讨论,我觉得这是提出更大论点的糟糕日子。因此,我们有一个具有分层体系结构的spring boot microservice应用程序Controller --> Service --> Repository

参数是在每一层进行验证还是仅在Controller层进行验证,例如,我们具有Controller和POST请求,并且对输入请求主体使用JSR-380验证

Controller:@PostMapping(value = "/", consumes = {"application/json"}, produces = {"application/json"})

public ResponseEntity createConfig(

@NotNull(message = "{error.message.config_detail}")

@RequestBody @Validated Config config) {

return new ResponseEntity<>(configService.create(config.getConfigId(), config.getTaskId()), HttpStatus.CREATED);

}

Config:请求正文public class Config {

@ApiModelProperty(name = "config_id", example = "1", required = true)

@NotNull(message = "{error.message.config_id}")

private Long configId;

@ApiModelProperty(name = "task_id", example = "11", required = true)

@NotNull(message = "{error.message.task_id}")

@Min(value = 0, message = "{error.message.task_id}")

@Max(value = 9999, message = "{error.message.task_id}")

private Integer taskId;

// bunch of addition fields with validations

}

如果验证成功,则使用Service中的某些属性调用Config方法>

服务:public Detail create(@Valid @NotNull Long configId, @NotNull Integer taskId) {

// some business logic to convert to entity and saving to database

return repository.save(entity));

}

因此,如果我们看到上面的代码,则在Controller和Service处进行相同的验证,因此我认为无需在Service层进行验证,请在控制器层执行验证,并且如果输入错误然后向用户抛出400或500。 但是团队中的另一个人也建议在每个块中对块中使用的内容进行验证,以使每段代码都是安全的(专注于单元而不是集成路径)。]

我知道在这种情况下我可能是错的,但是仍然无法理解每一层的验证(我同意无效检查,因此建议在每个级别进行验证的方法Controller --> validation call service

Service ---> validation and call business

Business ---> validation and call repository

Repository --> save根据我的说法,

但是首选的验证方式是什么?如果Controller输入是有效的,请调用Service并执行业务逻辑并调用Repository。如果我错了,请纠正我,以便我可以遵循推荐的模式

今天,我与我们的一位团队成员就控制器和服务层中的RESTful API输入的验证进行了广泛的讨论,我觉得这是提出更大论点的糟糕日子。所以我们有一个...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。