私はJAX-RS APIを設計しています。EntityManager#は識別不可能なエンティティとマージしてはいけませんか?
POST /myentities
PUT /myentities/{myentityId}
私はこのようにしました。
@PUT
@Path("/{myentityId: \\d+}")
@Consumes(...)
@Transactional
public Response updateMyentity(
@PathParam("myentityId") final long myentityId,
final Myentity myentity) {
if (!Objects.equal(myentitiyId, myentity.getId())) {
// throw bad request
}
entityManager.merge(myentity);
return Response.noContent().build();
}
突然私は自分自身に不思議と疑問を抱く。
クライアントは、次のリクエスト
PUT /myentities/101 HTTP/101
Host: ..
<myentity id=101>
</myentity>
を呼び出すには、要求が101
によって識別されていないリソースが存在しない場合でも処理されることは可能ですか?
私はテストを実行しました。
acceptEntityManager(entityManager -> {
// persist
final Device device1 = mergeDevice(entityManager, null);
entityManager.flush();
logger.debug("device1: {}", device1);
assertNotNull(device1.getId());
// remove
entityManager.remove(device1);
entityManager.flush();
assertNull(entityManager.find(Device.class, device1.getId()));
// merge again, with non existing id
final Device device2 = entityManager.merge(device1);
entityManager.flush();
logger.debug("device2: {}", device2);
});
2番目のマージが動作し、新しいIDが割り当てられていることがわかりました。 これは正常ですか? EntityManager#merge
操作を拒否してはいけませんか?
は、それがどのクライアントが
PUT /myentities/<any number>
を呼び出すことにより、APIを攻撃できることを意味しますか?