【问题标题】:Statically configure rel in Spring Data REST在 Spring Data REST 中静态配置 rel
【发布时间】:2017-05-11 16:30:30
【问题描述】:

我正在分析simple Spring Boot 1.5 Spring Data REST application。令我惊讶的是,Atteo Evo Inflector 是一个巨大的热点,根据JProfiler 超过一半的 CPU:

您应该可以使用Apache Bench 重现此内容:

ab2 -c 1 -n 10000 http://localhost:8080/people

存储库是:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository 
    extends PagingAndSortingRepository<Person, Long> {
    List<Person> findByLastName(@Param("name") String name);
}

和 (Lombok-ed) 数据:

@Data
@NoArgsConstructor
@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String firstName;
    private String lastName;

    @JsonSerialize(using = MyLocalDateSerializer.class)
    private LocalDate birthDate;

    public Person(String firstName, String lastName, String birthDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = LocalDate.parse(birthDate);
    }
}

当存储库静态定义 collectionResourceRel 时,为什么 Spring HATEOAS 试图影响 @RepositoryRestResource 存储库?任何想法正确的注释是什么来配置我的 Spring Data REST 应用程序以避免运行时变形开销?

【问题讨论】:

    标签: java spring-data-rest spring-hateoas


    【解决方案1】:

    @RestResource(rel="people", path="people") 添加到您的实体: `

    @Data
    @NoArgsConstructor
    @Entity
    @RestResource(rel="people", path="people")
    public class Person {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;
    
        private String firstName;
        private String lastName;
    
        @JsonSerialize(using = MyLocalDateSerializer.class)
        private LocalDate birthDate;
    
        public Person(String firstName, String lastName, String birthDate) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.birthDate = LocalDate.parse(birthDate);
        }
    }
    

    不幸的是,如果 Spring HATEOUS 没有在实体 it delegates to the Atteo Evo Inflection rel provider 上找到 @RestResource 注释,即使实体的存储库是 @RepositoryRestResource。由于实体和存储库现在都有relpath(重复)信息,因此您需要小心保持这些信息同步。我打开了a Spring Data REST issue on this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2019-09-13
      • 2019-06-15
      • 1970-01-01
      • 2015-07-05
      • 2016-04-17
      • 2019-10-10
      相关资源
      最近更新 更多