在 Spring 数据 REST 中静态配置 rel

Statically configure rel in Spring Data REST

我正在分析 simple Spring Boot 1.5 Spring Data REST application. To my surprise, the Atteo Evo Inflector is a tremendous hot-spot at over half the CPU according to JProfiler:

您应该可以使用 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 应用程序以避免运行时变形开销吗?

@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 没有在实体上找到 @RestResource 注释,it delegates to the Atteo Evo Inflection rel provider, even if the entity's repository is a @RepositoryRestResource. Since both the entity and the repository now have rel and path (duplicate) information you need to be careful to keep these in sync. I've opened a Spring Data REST issue on this