Search code examples
springrestspring-bootrestful-architecturespring-rest

add role to user RESTful [Spring-Rest]


I have to add a User one or multiple Role(s) in my Application. Currently i add one Role to a User at a time with this method:

UserController.java

@RequestMapping(value = "users/{id}/{roleId}", method = RequestMethod.POST)
public User assignRole(@PathVariable Long id, @PathVariable Long roleId) throws NotFoundException {
    log.info("Invoked method: assignRole with User-ID: " + id + " and Role-ID: " + roleId);
    User existingUser = userRepository.findOne(id);
    if(existingUser == null){
        log.error("Unexpected error, User with ID " + id + " not found");
        throw new NotFoundException("User with ID " + id + " not found");
    }
    Role existingRole = roleRepository.findOne(roleId);
    if(existingRole == null) {
        log.error("Unexpected error, Role with ID " + id + " not found");
        throw new NotFoundException("Role with ID " + id + " not found");
    }
    Set<Role> roles = existingUser.getRoles();
    roles.add(existingRole);
    existingUser.setRoles(roles);
    userRepository.saveAndFlush(existingUser);
    log.info("User assigned. Sending request back. ID of user is " + id + existingUser.getRoles());
    return existingUser;
}

This Method works fine but the problem is:

  1. The method can only add one Role to one User at a time
  2. The method is not RESTful

My Question is:

How can i add one or multiple Roles to a User in the concept of REST ? Should i even have a specific method to add roles to a user? or should i add the roles to the user in my update-method via PUT?


Solution

  • I found this as a valid proposal:

    @RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
    @ResponseBody
    public String test(@PathVariable List<Integer> firstNameIds) {
        //Example: pring your params
        for(Integer param : firstNameIds) {
            System.out.println("id: " + param);
        }
        return "Dummy";
    }
    

    what corresponds to a URL like that: GET http://localhost:8080/public/test/1,2,3,4

    Insteaf of Integer Long should also work.

    POST or PUT? .. None of them I would say. PATCH is the right one to use since you are not creating a new object/entity and you are not updating a whole object. Instead you update only one field of a object (see here: https://spring.io/understanding/REST). Your PATCH-call is also idempotent, what means that the same call done repeatedly always returns the same result.

    If you want to use a parameter for roleIds in your request (would fit better to the "Update only the specified fields of an entity at a URI." requirement of PATCH) it should look like this:

    @RequestMapping(value="users/{id}", method = RequestMethod.PATCH)
    public void action(@PathVariable Long id, @RequestParam(value = "param[]") String[] roleIds) {
       ...
    }
    

    You must try out with List<Long>.

    The corresponding client call (in jQuery) looks like:

    $.ajax({
        type: "PATCH",
        data: { param:roleIds }
        ...
    });