reset password
Author Message
Victor
Posts: 23
Posted 13:11 Oct 11, 2018 |

Hi everyone.

For the HW3 Question 1 (User registration)

I'm trying to create a user with this input:
{
    "email": "test@test.com",
    "first_name": "test_name",
    "last_name": "test_lastname",
    "major_or_organizational_unit": "Computer Science",
    "password": "password",
    "title": "Event_organizer",
    "username": "test_username",
    "roles": [2]
}

I created a constructor for the Role entity like so:

@JsonCreator
    public Role(@JsonProperty("id") Long id)
    {
        this.id = id;
        this.name = "";
    }

but I get a 400 error with this description: The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

If I remove the @JsonProperty("id") like this:

@JsonCreator
    public Role(Long id)
    {
        this.id = id;
        this.name = "";
    }

it works but the Role in the database is updated such that it only has an id. The name is overwritten with an empty string.

Does anyone know how to pass only the role ( [2] ) and have the system create a role with that id and the corresponding name and attach it to the User object?

cysun
Posts: 2935
Posted 13:33 Oct 11, 2018 |

(a) Only regular users can be created through registration, and regular users don't need to have a "role", i.e. you can use roles just for special users like admins, event organizers etc.

(b) @JsonProperty("id") obviously shouldn't be there because then Jackson would expect a id field in the JSON object which doesn't have it.

(c) You probably has a CacascadeType.ALL for your roles property in User and you need to remove that -- roles themselves should not be implicitly added/changed when you do userDao.saveUser() (note that I said "roles themselves", not "roles of a user").

Victor
Posts: 23
Posted 13:43 Oct 11, 2018 |

Thank you very much, Professor.

It works perfectly well now.