我在我的应用程序中使用jax-rs restful web服务和子资源定位器.但是,在将entityManager传递给子资源后,我无法在此子资源中保留任何新对象.
然而,entityManager让我可以查询数据.
这是我的主要资源:
@Path("/registrations")
@Stateless
public class RegistrationsResource {
@Context
private UriInfo context;
@PersistenceContext(unitName="pctx")
private EntityManager em;
public RegistrationsResource() {
}
//POST method ommited
@Path("{regKey}")
public RegistrationResource getRegistrationResource(@PathParam("regKey")
String regKey) {
return RegistrationResource.getInstance(regKey, em);
}
Run Code Online (Sandbox Code Playgroud)
}
这是我的子资源:
public class RegistrationResource {
private String regKey;
private EntityManager em;
private RegistrationResource(String regKey, EntityManager em) {
this.regKey = regKey;
this.em = em;
}
@Path("securityQuestion")
@GET
public String getQuestion() {
return "iamahuman"+regKey;
}
@Path("securityQuestion")
@POST
public void postSecurityAnswer(String answer) {
if(!answer.equals("iamahuman"+regKey)){
throw …Run Code Online (Sandbox Code Playgroud)