[英]How can I force Spring DATA REST @PathVariable entity exists
对于注入@PathVariable
实体,我的意思是
@RequestMapping(method = RequestMethod.PUT, path = "{project-id}")
@Transactional
@PreAuthorize("#user.id == #project.userId")
public Object update(@P("user") @Current User user,
@P("project") @PathVariable Project entity,
@RequestBody @Valid ProjectPost request) {
setProjectPostToEntity(entity, request);
return ResponseEntity.ok(ImmutableMap.of("message", "Project update successful"));
}
但是,如果在存储库中找不到project-id
则@PathVariable Project entity
将为null,我想要的是这样的东西
@RequestMapping(method = RequestMethod.PUT, path = "{project-id}")
@Transactional
@PreAuthorize("#user.id == #project.userId")
public Object update(@P("user") @Current User user,
@P("project") @PathVariable @Valid @NotNull(message="Update project not exists") Project entity,
@RequestBody @Valid ProjectPost request) {
setProjectPostToEntity(entity, request);
return ResponseEntity.ok(ImmutableMap.of("message", "Project update successful"));
}
如果未在存储库中找到project-id
,则会返回一条消息: Update project not exists
,但@Valid @NotNull(message="Update project not exists")
在这里不起作用,我该怎么做?
使用setProjectPostToEntity(entity, request)
方法创建服务,并使用@Transactional
和@PreAuthorize
对其进行注释。 这种方法确保您的变量将存在。 关于控制器方法上的@Transactional
: https : //stackoverflow.com/a/23120647/5649869
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.