1

By default when I hit my exposed repository I'm getting a JSON that looks like this:

{
    _links: {}, 
    _embedded: {}, //actual content
    page: {} //paging enabled
}

Is there a way to get rid of the underscore character that is generated by default at the beginning of the word links & embedded? There are nested appearances of _links inside _embedded so I guess they are all configured in the same place.

I need to have something like this instead

{
    links: {},
    embedded: {},
    page: {}
}

I'm using default config

@Import(RepositoryRestMvcConfiguration.class)

Thanks in advance.

felipe
  • 1,039
  • 1
  • 13
  • 27
  • See HAL - Hypertext Application Language http://stateless.co/hal_specification.html – Ali Dehghani May 09 '16 at 22:29
  • it does not seem to allow customization and _ is not optional for those two elements... – felipe May 10 '16 at 00:19
  • Have a look to this [post](http://stackoverflow.com/q/25709672/5873923) where the [collection+json](http://amundsen.com/media-types/collection/examples/) media type is mentionned. – Marc Tarin May 10 '16 at 07:54

2 Answers2

2

Since this is how HAL represents it (see the Link in Ali's comment), "probably no" (I don't claim to know more than 1% of Spring Data Rest's capabilities)... and yes, since there is a configuration property defaultMediaType , which you can set to application/json via spring.data.rest.defaultMediaType=application/json instead, so your Spring Data Rest will produce that instead of application/hal+json

This will not do exactly what you want, but change some other things as well, but could be close enough. But of course, you should think very good if getting rid of some underscores is worth getting rid of hal+json completely.

Perhaps the better question would be, "why" you want to get rid of the underscores, but that's probably more of a discussion than a question.

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • Fair point. The 'why' is because I'm using a proprietary framework that does not allow _ as attribute names when defining objects to be able to do the mapping directly. – felipe May 11 '16 at 00:10
  • Although your suggestion is applicable. It does not remove the _ from the json response, at least in 2.4.4.RELEASE – felipe May 11 '16 at 00:49
  • Just tried it with a simple demo application, the only thing I configured in application.properties was that single line `spring.data.rest.defaultMediaType=application/json`, I get names like `"links"` (instead of `"_links"`), `"content"` (instead of `"_embedded"`). So works for me, sure that you don't have some other setting somewhere interfering? – Florian Schaetz May 11 '16 at 05:25
  • I'm not using spring boot. Are you? Mine was an existing MVC web spring project that later was enriched with spring data rest. I'm using @Component CustomizedRestMvcConfiguration as per documentation to do the setting you suggested instead of a .properties. Let's mark it as answered, I trust you ;) – felipe May 11 '16 at 08:06
  • My bad, I was assuming Spring Boot. Of course, without Spring Boot, you will have to do the configuration more manually... So the only thing I can say is that with Spring Boot it works that way, sorry ;-) – Florian Schaetz May 11 '16 at 10:38
1

You should not want to do this, right now you're at rest level 3 (HAL/HATEOAS) which is a good thing. This has many benefits if you know how to use it. http://martinfowler.com/articles/richardsonMaturityModel.html#level3

You can always go to level 2 by excluding spring-hateoas from spring-data-rest-core, but I would not recommend it.

Formal specs for HAL: https://datatracker.ietf.org/doc/html/draft-kelly-json-hal-07

Community
  • 1
  • 1