Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the property name of an embbed collection in Spring Hateos

I am using Spring hateoas to generate a HAL interface. My code looks like this:

@RequestMapping(method = RequestMethod.GET)
public Resources<Resource<Type>> all() {
    List<Resource<Type>> sdf = typeRepository.all().stream().map(type -> {
        return new Resource<Type>(type, ControllerLinkBuilder.linkTo(this.getClass()).slash(type.getId()).withSelfRel());
    }).collect(Collectors.toList());

    Resources<Resource<Type>> resourcesType = new Resources<>(sdf);
    resourcesType.add(ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(this.getClass()).all()).withSelfRel());
    return resourcesType;
}

And the generated json looks like this:

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/type"
    }
  },
  "_embedded": {
    "typeEntityList": [
      {
        "id": "4f7fa2da-20e2-4b42-9b45-2d1749825785",
        "version": 0,
        "name": "name1",
        "_links": {
          "self": {
            "href": "http://localhost:8080/type/4f7fa2da-20e2-4b42-9b45-2d1749825785"
          }
        }
      }
    ]
  }
}

I would like to changed the name of the "typeEntityList", but I can't find how or where it comes from. Anyone know how?

like image 641
Geert Olaerts Avatar asked Oct 22 '15 20:10

Geert Olaerts


People also ask

What is EntityModel in HATEOAS?

Enhancing the resource to return HATEOAS response EntityModel is a simple class wrapping a domain object and allows adding links to it.

What is _embedded in JSON?

resources. "_embedded": contains embedded resources. All other properties MUST be valid JSON, and represent the current state of the resource. The reserved "_links" property.

What is HATEOAS Hal?

HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API. The HAL format is strictly coupled to HATEOAS. The main target of HATEOAS is to decouple the API Consumer from the paths used in the API.

What is org Springframework HATEOAS?

Spring HATEOAS provides some APIs to ease creating REST representations that follow the HATEOAS principle when working with Spring and especially Spring MVC. The core problem it tries to address is link creation and representation assembly.


1 Answers

Just have a look here: http://docs.spring.io/spring-hateoas/docs/0.19.0.RELEASE/reference/html/#spis.rel-provider

What you are seeing is the default. If you put the EVO inflector on the classpath you will get something like "types". You can also put the @Relation annotation on your entity and change the rel names for collection and single resource.

like image 173
Mathias Dpunkt Avatar answered Sep 28 '22 01:09

Mathias Dpunkt