Spring HATEOAS链接以单个对象的HAL格式返回,以及对象数组的spec格式

时间:2017-03-07 20:27:43

标签: arrays json spring-hateoas

我正在使用Spring HATEOAS 0.20(也尝试过0.23)并且在返回单个对象与对象数组时似乎存在非常奇怪的行为。返回单个对象时,生成的JSON返回通过HAL规范格式化的HATEOAS链接(“_links”,我不想要):

{
  "_Id": 161,
  ...
  "_links": {
    "self": {
      "href": "http://localhost:8080/library/161"
    }
  }
}

返回一个对象数组时,生成的JSON以规范格式返回HATEOAS链接(“链接”,我确实想要):

[
  {
    "_Id": 277,
    ...
    "links": [
      {
        "rel": "self",
        "href": "http://localhost:8080/library/277"
      }
    ]
  }
]

我没有指定@EnableHypermediaSupport,因此不应支持HAL。但是,当我返回一个对象时,我仍然会得到HAL格式。

有谁知道为什么会发生这种情况? 需要做什么才能通过HATEOAS规范返回带有HATEOAS链接的单个对象?

=====根据更多信息要求更新帖子=====

如果我使用以下代码返回JSON加载,我会得到HAL链接:

//
//------------------- Get Single Membership As Single Object ----------------------------------------------
//
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = REQUEST_GET_MEMBERSHIP, method = RequestMethod.GET,  produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<MembershipResource> getMembership(@PathVariable("membership-id") Integer membershipId) {
    Membership membership = membershipRepository.findOne(membershipId);

    if (membership == null) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("error", "Membership with ID of " + membershipId + " does not exist.");
        return new ResponseEntity<MembershipResource>(headers, HttpStatus.NOT_FOUND);
    }

    MembershipResourceAssembler assm = new MembershipResourceAssembler();
    MembershipResource membershipResource = assm.toResource(membership);

    System.out.println("membership id: " + membership.getId());
    return new ResponseEntity<MembershipResource>(membershipResource, HttpStatus.OK);
}

以上将产生HAL规范样式链接:

{
  "_Id": 277,
  "member": {
    "_Id": 112,
    "title": "Mr.",
    "firstName": "ABC",
    "middleName": "",
    "lastName": "XYZ",
    "active": true,
    "_links": {
      "self": {
        "href": "http://localhost:8080/members/112"
      }
    }
  },
  "membershipTermType": "2016",
  "card": {
    "_Id": 27,
    "name": "27",
    "graphicUrl": "",
    "number": 27,
  },
  "active": false,
  "paid": true,
  "renewal": true,
  "termsAndConditionsAcknowledged": true,
  "_links": {
    "self": {
      "href": "http://localhost:8080/memberships/277"
    }
  }
}

如果我修改方法以返回单个成员的List,则返回的JSON加载是使用HATEOAS规范链接:

//
//------------------- Get Single Membership As List --------------------------------------------------------
//
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = REQUEST_GET_MEMBERSHIP, method = RequestMethod.GET,  produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<List<MembershipResource>> getMembership(@PathVariable("membership-id") Integer membershipId) {
    Membership membership = membershipRepository.findOne(membershipId);

    if (membership == null) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("error", "Membership with ID of " + membershipId + " does not exist.");
        return new ResponseEntity<List<MembershipResource>>(headers, HttpStatus.NOT_FOUND);
    }

    MembershipResourceAssembler assm = new MembershipResourceAssembler();
        MembershipResource membershipResource = assm.toResource(membership);

    ArrayList<MembershipResource> list = new ArrayList<MembershipResource>();
    list.add(membershipResource);

    System.out.println("membership id: " + membership.getId());
    return new ResponseEntity<List<MembershipResource>>(list, HttpStatus.OK);
}

以上将产生HATEOAS规范样式链接:

[
  {
    "_Id": 277,
    "member": {
      "_Id": 112,
      "title": "Mr.",
      "firstName": "ABC",
      "middleName": "",
      "lastName": "XYZ",
      "active": true,
      "links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/members/112"
        }
      ]
    },
    "membershipTermType": "2016",
    "card": {
      "_Id": 27,
      "name": "27",
      "graphicUrl": "",
      "number": 27,
      "links": []
    },
    "active": false,
    "renewal": true,
    "paid": true,
    "termsAndConditionsAcknowledged": true,
    "links": [
      {
        "rel": "self",
        "href": "http://localhost:8080/memberships/277"
      }
    ]
  }
]

有谁知道为什么会发生这种情况? 需要做什么才能通过HATEOAS规范返回带有HATEOAS链接的单个对象?

1 个答案:

答案 0 :(得分:1)

由于我使用Spring Boot作为我的应用程序服务器,@zeroflagL建议我尝试在您的应用程序中添加@EnableAutoConfiguration(exclude = HypermediaAutoConfiguration.class)或在您的属性文件中添加spring.hateoas.use-hal-as-default-json-media-type=false

@EnableAutoConfiguration(exclude = HypermediaAutoConfiguration.class)的配置注释添加到我的应用程序类中解决了这个问题。