Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jakarta ee 在自定义反序列化程序中将EntityManager与JAX-RS和Jackson一起使用_Jakarta Ee_Jpa_Jackson_Jax Rs - Fatal编程技术网

Jakarta ee 在自定义反序列化程序中将EntityManager与JAX-RS和Jackson一起使用

Jakarta ee 在自定义反序列化程序中将EntityManager与JAX-RS和Jackson一起使用,jakarta-ee,jpa,jackson,jax-rs,Jakarta Ee,Jpa,Jackson,Jax Rs,我有JSON格式的客户数据: { "name":"My Name", "company":{ "id":9 } } 我的实体看起来像: @Entity public class Customer { @Id Integer id; String name; @Reference Company company; /* getters and setters */ } 我想与Jackson反序列化此数

我有JSON格式的客户数据:

{
    "name":"My Name",
    "company":{
        "id":9
    }
}
我的实体看起来像:

@Entity
public class Customer {
    @Id
    Integer id;

    String name;

    @Reference
    Company company;

    /* getters and setters */
}
我想与Jackson反序列化此数据,并通过
EntityManager.getReference(company.class,id)
获取
公司的引用。注释
@Reference
是一个自定义注释,表示我希望使用自定义反序列化程序并检索引用。目标是透明地访问我的JAX-RS服务端点中的
公司
信息

我的问题是:在反序列化过程中,我应该如何注入或创建EntityManager

下面是一些代码:

@Provider
public class ObjectMapperContext implements ContextResolver<ObjectMapper> {

    private ObjectMapper mapper;

    public ObjectMapperContext() {
        super();
        mapper = new ObjectMapper();
        mapper.registerModule(new CustomModule());
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }
}
最后,在
CustomAnnotationIntrospector.java中:

public class CustomAnnotationIntrospector extends AnnotationIntrospector {
    @Override
    public Object findDeserializer(Annotated am) {
        Reference reference = am.getAnnotation(Reference.class);
        if(reference == null){
            return null;
        }
        return CustomDeserializer.class;
    }
}

在反序列化过程中创建或注入EntityManager似乎是一个奇怪的想法。为什么不在通过Web服务提供服务之前检索公司?我有很多“参考资料”要检索。如果它是自动完成的,那就太好了。但事实上,在反序列化过程中注入EntityManager可能不是一个好主意。我想我会再次解析我的反序列化对象来解析引用。我需要做完全相同的事情。你有没有想出一个解决办法?
public class CustomAnnotationIntrospector extends AnnotationIntrospector {
    @Override
    public Object findDeserializer(Annotated am) {
        Reference reference = am.getAnnotation(Reference.class);
        if(reference == null){
            return null;
        }
        return CustomDeserializer.class;
    }
}