Reputation: 731
I have developed an rest service using spring-boot and Spring-boot-starter hateoas. And I am facing an issue with customizing ObjectMapper. The code goes below for the same:
Application.java
@Configuration
@Import(BillServiceConfig.class)
@EnableAutoConfiguration
@EnableEurekaClient
@ComponentScan({"com.bill"})
@EnableWebMvc
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application extends WebMvcConfigurerAdapter{
@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true).dateFormat(new SimpleDateFormat("MM-yyyy-dd"));
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
builder.configure(objectMapper);
return builder;
}
Dependencies:
dependencies {
compile "org.springframework.boot:spring-boot-starter-hateoas"
compile "org.springframework.boot:spring-boot-starter-ws"
compile "org.springframework.boot:spring-boot-starter-actuator"
Bill.java:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName("bills")
public class Bill{
BillController.java:
public ResponseEntity<Resources<Resource<Bill>>> getBills(){
The output I am getting is:
{
_embedded: {
billList:
But I require "bills" in place of "billList". It is because of ObjectMapper is not getting customized. Am I missing any configuration, Kindly help me out in this issue. Thanks in advance.
Upvotes: 3
Views: 2514
Reputation: 465
Root of this problem - default ObjectMapper from Spring MVC is used instead of one configured by author. This happens because of @EnableWebMvc.
Quote from Spring Boot guide
Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath.
However if you one puts it, Spring MVC will create its own set of MessageConverters and won't use yours ObjectMapper.
PS even though I post this answer so late, may be it will help others.
Upvotes: 0
Reputation: 6280
I'm using spring-boot 1.5 RC1. If you remove the @EnableHypermediaSupport annotation spring-boot should configure spring-hateoas with ISO 8601 dates for you so long as you have java time module on the classpath.
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
This worked for me anyway.
If you want further custom configuration see the solutions at http://github.com/spring-projects/spring-hateoas/issues/333
Upvotes: 1