Java 在Spring中生成一致的资源链接
使用此代码行生成链接时:Java 在Spring中生成一致的资源链接,java,spring,spring-mvc,hateoas,spring-hateoas,Java,Spring,Spring Mvc,Hateoas,Spring Hateoas,使用此代码行生成链接时: indexResource.add(linkTo(IndexController.class).withSelfRel()); 此JSON生成于: { "links" : [ { "rel" : "self", "href" : "http://localhost:8080" } ] } 但是,Spring Data Rest生成的资源链接生成以下JSON: { "_links" : { "self" : { "hre
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数据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)
似乎不会改变任何事情
我还具有以下渐变依赖项:
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"
我建议创建两个类,比如Link和Self。Link有Self。Self已经href和模板化了。 创建一个类,该类将链接的形成作为Java对象。
然后使用类似于GSON的Java-to-Json库进行类似的输出,如上所述Spring数据Rest使用HAL格式。它应该是较新版本的Spring HATEOAS的默认设置。您可以通过配置类上的注释激活它:
@EnableHypermediaSupport(type= {HypermediaType.HAL})
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer c) {
c.defaultContentType(MediaTypes.HAL_JSON);
}
}
更新
我用弹簧靴遇到了类似的问题。我必须将以下内容添加到我的pom.xml
:
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
org.springframework.plugin
spring插件核心
1.1.0.1发布
为了生成HAL格式的JSON,您的HTTP请求必须接受application/HAL+JSON(即接受头是application/HAL+JSON)
应用程序的默认内容类型也可以是application/json。您可以通过以下配置类将默认内容类型更改为application/hal+json:
@EnableHypermediaSupport(type= {HypermediaType.HAL})
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer c) {
c.defaultContentType(MediaTypes.HAL_JSON);
}
}
如果您使用以下请求头发出请求 接受:应用程序/alps+json 你应该得到想要的结果,当然 { "links" : [ { "rel" : "self", "href" : "http://localhost:8080" } ] } { “链接”:[{ “rel”:“self”, “href”:”http://localhost:8080" } ] }
如果您通过以下方式提出请求: 接受:application/json 或 接受:应用程序/hal+json 然后您将获得HAL(当前为默认值,请参阅下面的链接) { “_链接”:{ “自我”:{ “href”:”http://localhost:8080/persons{?页面、大小、排序}“, “模板化”:正确 } } } 看
删除或添加此项似乎不会改变任何事情。我用的是弹簧靴,这和它有什么关系吗?更新了上面的问题以反映配置和依赖关系。@我更新了答案。它应该可以在没有
@EnableHypermediaSupport
的情况下工作。即使在我的类路径中包含spring插件核心
,仍然会遇到问题