Java RepositoryRestMVC配置';s ObjectMapper与Spring引导默认ObjectMapper?

Java RepositoryRestMVC配置';s ObjectMapper与Spring引导默认ObjectMapper?,java,spring-boot,jackson,spring-data,spring-data-rest,Java,Spring Boot,Jackson,Spring Data,Spring Data Rest,我正在使用RepositoryRestMvcConfiguration对rest存储库行为进行微调: @Configuration public class WebConfig extends RepositoryRestMvcConfiguration { @Override protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config

我正在使用
RepositoryRestMvcConfiguration
对rest存储库行为进行微调:

@Configuration
public class WebConfig extends RepositoryRestMvcConfiguration {
    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
      config.setReturnBodyOnCreate(true);
}
缺点是扩展类引入了自己的ObjectMapperbean,导致了前面描述的冲突。推荐的解决方法是使用扩展类将ObjectMapper bean标记为
@Primary
,但是
RepositoryRestMvcConfiguration
中的bean在序列化嵌套实体时具有不同的行为

让我们假设以下情况:

@Entity class Parent {
    @Id Long id;
    @OneToMany @JsonManagedReference List<Child> children;
    // usual getters and setters for fields...
}

@Entity class Child {
    @Id Long id;
    @ManyToOne @JsonBackReference Parent parent;
    @ManyToOne @JsonBackReference School school;
    public getSchooldId() { return school.getId(); }
    // usual getters and setters for fields...
}

@Entity class School {
    @Id Long id;
    @OneToMany @JsonManagedReference List<Child> children;
    // usual getters and setters for fields...
} 
但是,
RepositoryRestMvcConfiguration
中的ObjectMapper忽略子实体:

{"id": 1}

配置
RepositoryRestMvcConfiguration
ObjectMapper以实现与Spring启动默认值相同的行为的正确方法是什么?

RepositoryRestMvcConfiguration
创建两个ObjectMapper对象

  • 用于内部框架的objectMapper
  • HaloObjectMapper负责呈现集合
    资源
    和链接
  • 通过使用限定符自动连接objectMapper,可以尝试获得所需的结果:

    @Qualifier('_halObjectMapper')
    
    编辑:用于呈现关联/嵌套属性

    SpringDataREST在默认情况下不会呈现关联(),因为它们在HATEOAS规范(json的
    \u链接部分)下可用。
    如果要渲染关联,只需使用投影即可

    此人有几个属性:id是主键,firstName和lastName是数据属性,address是指向另一个域对象的链接

    将提供:

    {"id": 1, "children":[{"id":2, "schoolId":7},{"id":3, "schooldId":8}]}
    
       {
      "firstName" : "Frodo",
      "lastName" : "Baggins",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/persons/1"
        },
        "address" : {
          "href" : "http://localhost:8080/persons/1/address"
        }
        }
        }
    
    默认情况下,SpringDataREST将导出此域对象,包括其所有属性。firstName和lastName将作为它们所在的普通数据对象导出。关于地址属性,有两个选项。一个选项是还为地址定义存储库

    还有另一条路。如果地址域对象没有自己的存储库定义,SpringDataREST将直接在Person资源中内联数据字段


    不幸的是,这没有帮助。1) objectMapper也是通过其他Spring引导代码(例如org.springframework.Boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration中的构造函数)自动连接的。2)可以使用
    @Override@Bean@Primary public objectMapper objectMapper(){return super.haloObjectMapper();}将HaloObjectMapper设置为默认值
    但这会产生与其他objectMapper相同的结果-嵌套实体不会呈现给JSONOk,现在我了解了您的问题。这对于Spring Data Rest存储库端点是正确的,但对于objectMapper的独立使用(例如,控制器中的autowired)则不正确。默认的ObjectMapper(在未扩展
    RepositoryRestMVCCConfiguration
    时通过自动关联可用)按预期呈现嵌套实体,即使没有投影。只要出于配置目的扩展了
    RepositoryRestMvcConfiguration
    ,objectMapper/HaloObjectMapper在独立模式下使用时就不再呈现嵌套实体。我修复了限定符,它从_
       {
      "firstName" : "Frodo",
      "lastName" : "Baggins",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/persons/1"
        },
        "address" : {
          "href" : "http://localhost:8080/persons/1/address"
        }
        }
        }