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

2

9

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?

Miscellaneous answered 22/10, 2015 at 20:6 Comment(0)
J
20

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.

Jodhpurs answered 22/10, 2015 at 21:0 Comment(1)
Thansk the @Relation(collectionRelation = "types") did it, thanksMiscellaneous
S
2
@RepositoryRestResource(path = "somePath",collectionResourceRel="proper_list_of_items_name")

String collectionResourceRel() default "";

The rel value to use when generating links to the collection resource.

Serval answered 16/4, 2017 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.