As the title says, I have a resource object Product
extending ResourceSupport
. However, the responses I receive have the property "_links" instead of "links" and have a different structure.
{
"productId" : 1,
"name" : "2",
"_links" : {
"self" : {
"href" : "http://localhost:8080/products/1"
}
}
}
Based on the HATEOAS Reference, the expected is:
{
"productId" : 1,
"name" : "2",
"links" : [
{
"rel" : "self"
"href" : "http://localhost:8080/products/1"
}
]
}
Was this intended? Is there a way to change it, or at leas the "link" if not the structure?
I added the selfLink through the following snippet:
product.add(linkTo(ProductController.class).slash(product.getProductId()).withSelfRel());
I am using spring boot with the following build file:
dependencies {
compile ("org.springframework.boot:spring-boot-starter-data-rest") {
exclude module: "spring-boot-starter-tomcat"
}
compile "org.springframework.boot:spring-boot-starter-data-jpa"
compile "org.springframework.boot:spring-boot-starter-jetty"
compile "org.springframework.boot:spring-boot-starter-actuator"
runtime "org.hsqldb:hsqldb:2.3.2"
testCompile "junit:junit"
}
Another option would be to disable the whole hypermedia auto-configuration feature (this is how it's done in one of the spring-boot
+ REST examples here):
@EnableAutoConfiguration(exclude = HypermediaAutoConfiguration.class)
As far as I know, HypermediaAutoConfiguration
doesn't really do much except for configuring HAL, so it should be perfectly fine to disable it.
I have notice the problem appears at least when you trying to return an object extends ResourseSupport vs object containing object extends ResourseSupport. You may return even List or Array of object(s) extends ResourseSupport and have the same effect. See the example :
@RequestMapping(method = GET, value = "/read")
public NfcCommand statusPayOrder() {
return generateNfcCommand();
}
have response:
{
"field": "123",
"_links": {
"self": {
"href": "http://bla_bla_bla_url"
}
}
}
When try to wrap as List:
@RequestMapping(method = GET, value = "/read")
public List<NfcCommand> statusPayOrder() {
return Arrays.asList(generateNfcCommand());
}
getting:
[
{
"field": 123
"links": [
{
"rel": "self",
"href": "http://bla_bla_bla_url"
}
]
}
]
Changing structure of an answer is not the right decision, but we can try to think further this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With