New users are created with the POST
method, and after this the database will attach it an unique ID
, which is necessary to create a token.
The only way to create a token, is after the process of user creation. I query it from the database and now it should have ID
on it, but I can't figure how to do that.
Is there any way to retrieve the entity from database right after creating it?
If looked at this similar question, but couldn't find an answer: Is it ok by REST to return content after POST?
@POST
@Consumes({MediaType.APPLICATION_JSON})
public Response create(@QueryParam("email") String email, @QueryParam("username") String userName, @QueryParam("password") String password) {
if (TextUtil.isEmpty(userName) || TextUtil.isEmpty(password) || TextUtil.isEmpty(email)) {
return Response.status(Response.Status.BAD_REQUEST).build();
} else {
User newUser = new User();
newUser.setEmail(email);
newUser.setUsername(userName);
newUser.setPass(password);
super.create(newUser); //ths line create user in database
String id = newUser.getUid() + ""; //the newUser has no id yet so it is null but I want this id from database
return Response.status(Response.Status.OK).entity("id: " + id).build();
}
}
Create super.method
public void create(T entity) {
getEntityManager().persist(entity);
}
I generate REST api using this tutorial https://netbeans.org/kb/docs/websvc/rest.html