0

I am building spring (spring boot) based api. Is it possible to remove _embedded keyword from generated by hateoas library json output? I would like to have collection of my items displayed not under the _embedded. I know it breaks the specification however I was wondering if there are easy ways to customize the output but still use spring hateoas? In case that it is not possible, should I use different api building library that would allow more flexibility in terms of the generated output, in that case what would you suggest?

My controller code:

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
HttpEntity<Resources<Author>> getAllAuthors() {
 Resources<Author> resources = new Resources<>(this.authorsService.findAll());
 resources.add(this.entityLinks.linkToCollectionResources(Author.class));
 return new ResponseEntity<>(resources, HttpStatus.OK)
AKK
  • 1
  • 1
  • Here is an answer: http://stackoverflow.com/questions/28808220/how-to-remove-the-embedded-property-in-spring-hateoas – branko terzic Jan 04 '17 at 13:04
  • I've tried that already, it does not work. Added custom header and set the hateoas option to false. – AKK Jan 04 '17 at 13:05

2 Answers2

0

Also here are some known issues:https://github.com/spring-projects/spring-hateoas/issues/175

branko terzic
  • 654
  • 1
  • 8
  • 27
0

You need to write a custom Serializer that takes the hateoas objects e transforms to the wished objects.

See an example for pagination custom response:

@Component
public class CustomPageResponseSerializer extends JsonSerializer<Page> {

    @Autowired
    private PagedResourcesAssembler<Page> pagedResourcesAssembler;

    @Override
    public void serialize(Page page, JsonGenerator gen, SerializerProvider provider) throws IOException {
        PagedModel pageModel = pagedResourcesAssembler.toModel(page);

        CustomPageResponse pageResponse = CustomPageResponse.builder()
                .data(page.toList())
                .page(PaginationDataResponse.builder()
                        .page(page.getNumber())
                        .pageSize(page.getSize())
                        .totalPages(page.getTotalPages())
                        .totalElements(Math.toIntExact(page.getTotalElements()))
                        .links(
                                PaginationLinksResponse.builder()
                                        .first(getLink(pageModel.getLink(IanaLinkRelations.FIRST)))
                                        .last(getLink(pageModel.getLink(IanaLinkRelations.LAST)))
                                        .previous(getLink(pageModel.getPreviousLink()))
                                        .next(getLink(pageModel.getNextLink()))
                                        .build()
                        )
                        .build())
                .build();

        new PaginationObjectMapper().writeValue(gen, pageResponse);
    }

    private String getLink(Optional link) {
        return link.isPresent() ? ((Link)link.get()).getHref() : "";
    }
}