2

For inject @PathVariable entity I mean

@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"));
}

But the @PathVariable Project entity will be null if project-id not found in repository, what I want is something like this

@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"));
}

If project-id not found in repository will return a message Update project not exists, but @Valid @NotNull(message="Update project not exists") not works here, how can I do something like this ?

wener
  • 7,191
  • 6
  • 54
  • 78

1 Answers1

0

Create service with method setProjectPostToEntity(entity, request) and annotate it with @Transactional and @PreAuthorize. That approach garantee you that your variables will exists. And about @Transactional on controller methods: https://stackoverflow.com/a/23120647/5649869

Community
  • 1
  • 1