0

This is an example of what I would like to achieve:

@RepositoryRestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private TaskRepository taskRepository;

    @PostMapping("/addCompletedTask")
    public void addCompletedTask(User user, Task task) {
        user.getCompletedTasks().add(task);
        task.incrementCompletedBy();
        userRepository.save(user);
        taskRepository.save(task);
    }
}

Then I would make a request like this:

POST http://localhost:8080/api/users/addCompletedTask
{
 "user": "http://localhost:8080/api/db/users/59fa19bfd58dcf25e82082b2",
 "task": "http://localhost:8080/api/db/tasks/59fa19bfd58dcf22d2322312"
}

I tried to wrap arguments to the method with Resource<User>, add @RequestBody, nothing works, everything is null.

I don't know if it's possible, but I've seen examples of people writing code like this, so maybe I'm missing something (here for example). Or maybe is there some way to do that through the repository in one call? I can add a task in one call, and increment counter in another, but that requires two calls from the client. Also I'm pretty sure I will encounter another situation similiar to this, so if there is a solution that would be great.

Shadov
  • 5,421
  • 2
  • 19
  • 38
  • You need to ask about your real problem which can maybe be addressed by an event handler? https://docs.spring.io/spring-data/rest/docs/current/reference/html/#events https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Alan Hay Nov 01 '17 at 23:55
  • This is just an example, what I want is to simply give resource URI's in the request JSON and then have them in the controller as java objects. I would even be happy with knowing if that is possible without doing things manually, extending Spring classes etc, because maybe it's not possible. – Shadov Nov 02 '17 at 12:57

0 Answers0