Reputation: 2071
When producing links using this line of code:
indexResource.add(linkTo(IndexController.class).withSelfRel());
This JSON is produced:
{
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080"
} ]
}
However, resource links produced by Spring Data Rest produce this JSON:
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons{?page,size,sort}",
"templated" : true
}
}
}
Particularly, I would like to imitate the one produced by Spring Data Rest. How do I go about this?
I am using Spring Boot with the following configuration:
@Configuration
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@ComponentScan
public class Application { ... }
Retaining or removing @EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
doesn't seem to change anything.
I also have the following gradle dependencies:
compile "org.springframework.boot:spring-boot-starter-data-rest"
compile "org.springframework.boot:spring-boot-starter-data-jpa"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-jetty"
compile "org.springframework.data:spring-data-envers:0.2.0.RELEASE"
compile "org.hibernate:hibernate-envers:4.3.6.Final"
runtime "mysql:mysql-connector-java:5.1.32"
testCompile "junit:junit"
Upvotes: 2
Views: 1195
Reputation: 6915
If you make the request with the following request header
Accept: application/alps+json
You should get the desired result, of...
{ "links" : [ { "rel" : "self", "href" : "http://localhost:8080" } ] }
If you make the request with:
Accept: application/json
or
Accept: application/hal+json
Then you'll get the HAL (currently the default, see link below)
{ "_links" : { "self" : { "href" : "http://localhost:8080/persons{?page,size,sort}", "templated" : true } } }
Upvotes: 0
Reputation: 261
In order to produce the HAL formatted JSON, your HTTP request must accept application/hal+json (i.e. the Accept header is application/hal+json).
The default content type of your application may also be application/json. You can change the default content type to application/hal+json through the following configuration class:
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer c) {
c.defaultContentType(MediaTypes.HAL_JSON);
}
}
Upvotes: 1
Reputation: 26828
Spring Data Rest uses the HAL format. It should be the default for newer versions of Spring HATEOAS. You can activate it with an annotation on a Configuration class:
@EnableHypermediaSupport(type= {HypermediaType.HAL})
UPDATE
I ran into a similar siutation with Spring Boot. I had to add the following to my my pom.xml
:
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
Upvotes: 1
Reputation: 240
I would suggest creating two classes like Link and Self.Link has Self. Self has href and templated. Create a class that does the formation of links as Java object. Then use a Java to Json library like GSON for a similar output as above
Upvotes: 0