新しいユーザーはPOST
メソッドで作成され、その後、データベースにトークンを作成するために必要な一意のID
が添付されます。JavaサービスのPOST後にコンテンツを返すためのREST
トークンを作成する唯一の方法は、ユーザー作成のプロセスの後です。私はそれをデータベースから照会して、今はID
を持っているはずですが、それを行う方法はわかりません。
エンティティを作成した直後にデータベースからエンティティを取得する方法はありますか?この同様の質問を見たが、答えを見つけることができなかった場合は
: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();
}
}
がsuper.method作成は
public void create(T entity) {
getEntityManager().persist(entity);
}
私はこのチュートリアル https://netbeans.org/kb/docs/websvc/rest.html
私は私の質問にedditedいる –
をリストsuper.create()メソッドを追加してください:たぶん、あなたはより良い
create
メソッドを記述する必要があります。ありがとうございます – coinhndp'' ' super.create(newUser); super.getEntityManager()。flush(); super.getEntityManager()。find(User.class、newUser.getUId()) '' ' –