Skip to content

Commit bca0bc1

Browse files
committed
#1260 - Adjust CustomHypermediaType so it produces HAL-like JSON.
By having this custom hypermedia type register ALL the parts of the HAL Jackson module, it provides a better example to users that wish to serve a custom media type that is HAL-like. Related issues: #1259, #1253
1 parent 63e1c84 commit bca0bc1

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/test/java/org/springframework/hateoas/support/CustomHypermediaType.java

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
import java.util.List;
2020

2121
import org.springframework.hateoas.config.HypermediaMappingInformation;
22+
import org.springframework.hateoas.mediatype.MessageResolver;
23+
import org.springframework.hateoas.mediatype.hal.CurieProvider;
24+
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule;
25+
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalHandlerInstantiator;
26+
import org.springframework.hateoas.server.core.EvoInflectorLinkRelationProvider;
2227
import org.springframework.http.MediaType;
2328

2429
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -47,6 +52,9 @@ public List<MediaType> getMediaTypes() {
4752
@Override
4853
public ObjectMapper configureObjectMapper(ObjectMapper mapper) {
4954

55+
mapper.registerModule(new Jackson2HalModule());
56+
mapper.setHandlerInstantiator(new HalHandlerInstantiator(new EvoInflectorLinkRelationProvider(),
57+
CurieProvider.NONE, MessageResolver.DEFAULTS_ONLY));
5058
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
5159
mapper.enable(SerializationFeature.INDENT_OUTPUT);
5260

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name" : "Frodo Baggins",
33
"role" : "ring bearer",
4-
"links" : [ {
5-
"rel" : "self",
6-
"href" : "http://localhost/employees/1"
7-
} ]
8-
}
4+
"_links" : {
5+
"self" : {
6+
"href" : "http://localhost/employees/1"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name" : "Frodo Baggins",
33
"role" : "ring bearer",
4-
"links" : [ {
5-
"rel" : "self",
6-
"href" : "http://localhost/employees/1"
7-
} ]
8-
}
4+
"_links" : {
5+
"self" : {
6+
"href" : "http://localhost/employees/1"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)