使用这行代码生成链接时:
indexResource.add(linkTo(IndexController.class).withSelfRel());
生成此 JSON:
{
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080"
} ]
}
但是,Spring Data Rest 生成的资源链接会生成以下 JSON:
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons{?page,size,sort}",
"templated" : true
}
}
}
特别是,我想模仿 Spring Data Rest 生成的一个。我该怎么办?
我使用 Spring Boot 并进行以下配置:
@Configuration
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@ComponentScan
public class Application { ... }
保留或删除 @EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
似乎不会改变任何内容。
我还有以下 gradle 依赖项:
compile "org.springframework.boot:spring-boot-starter-data-rest"
compile "org.springframework.boot:spring-boot-starter-data-jpa"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-jetty"
compile "org.springframework.data:spring-data-envers:0.2.0.RELEASE"
compile "org.hibernate:hibernate-envers:4.3.6.Final"
runtime "mysql:mysql-connector-java:5.1.32"
testCompile "junit:junit"
最佳答案
Spring Data Rest 使用 HAL 格式。它应该是较新版本的 Spring HATEOAS 的默认值。您可以使用配置类上的注释来激活它:
@EnableHypermediaSupport(type= {HypermediaType.HAL})
更新
我在 Spring Boot 中遇到了类似的情况。我必须将以下内容添加到我的 pom.xml
中:
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
关于java - 在Spring中生成一致的资源链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25926286/