本文介绍了返回的json意外,具有“链接"拼写为"_links"和结构不同,在春季的仇恨中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我有一个扩展了ResourceSupport的资源对象Product.但是,我收到的响应的属性是"_links"而不是"links",并且结构不同.

As the title says, I have a resource object Product extending ResourceSupport. However, the responses I receive have the property "_links" instead of "links" and have a different structure.

{
  "productId" : 1,
  "name" : "2",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/products/1"
    }
  }
}

基于 HATEOAS参考,是:

{
  "productId" : 1,
  "name" : "2",
  "links" : [
    {
      "rel" : "self"
      "href" : "http://localhost:8080/products/1"
    }
  ]
}

这是故意的吗?有没有办法更改它,或者如果没有结构,请至少保留链接"?

我通过以下代码段添加了selfLink:

I added the selfLink through the following snippet:

product.add(linkTo(ProductController.class).slash(product.getProductId()).withSelfRel());

我正在使用带有以下构建文件的spring boot:

I am using spring boot with the following build file:

dependencies {
    compile ("org.springframework.boot:spring-boot-starter-data-rest") {
        exclude module: "spring-boot-starter-tomcat"
    }

    compile "org.springframework.boot:spring-boot-starter-data-jpa"
    compile "org.springframework.boot:spring-boot-starter-jetty"
    compile "org.springframework.boot:spring-boot-starter-actuator"

    runtime "org.hsqldb:hsqldb:2.3.2"

    testCompile "junit:junit"
}

推荐答案

Spring Boot现在(版本= 1.3.3.RELEASE)具有控制PagedResources的输出JSON格式的属性.

Spring Boot now (version=1.3.3.RELEASE) has a property that controls the output JSON format of the PagedResources.

只需将以下配置添加到您的application.yml文件:

Just add the following config to your application.yml file:

spring.hateoas.use-hal-as-default-json-media-type: false

如果您需要输出类似(根据问题):

if you need the output to be like (based on question):

{
  "productId" : 1,
  "name" : "2",
  "links" : [
    {
      "rel" : "self"
      "href" : "http://localhost:8080/products/1"
    }
  ]
}

通过这种方式,您只需要@EnableSpringDataWebSupport注释即可.

By the way, you only need @EnableSpringDataWebSupport annotation in this way.

这篇关于返回的json意外,具有“链接"拼写为"_links"和结构不同,在春季的仇恨中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:57