小编MiK*_*Kom的帖子

如何使用jax-rs子资源定位器处理持久化上下文(EntityManager)?

我在我的应用程序中使用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)

java rest jpa jax-rs

4
推荐指数
2
解决办法
4482
查看次数

标签 统计

java ×1

jax-rs ×1

jpa ×1

rest ×1