1

The below object mapper configuration is not working when I add jjwt security to spring boot application.

@Configuration
public class CustomObjectMapper extends ObjectMapper {

/**
 * Default serial version id generated.
 */
private static final long serialVersionUID = 1L;

public CustomObjectMapper() {
    this.setSerializationInclusion(Include.NON_EMPTY);
    this.registerModule(new ThreeTenModule());
    this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }
}

Security dependencies added here

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.7.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-jwt</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
</dependency>

where as the below Jackson annotations are working on class/field levels.

@JsonInclude(Include.NON_EMPTY) 

Why the bean configured custom object mapper not been used for serialization & deserialization? Any other libraries configured object mapper overriding my custom mapper?

  • Tried like below but no luck. `@Component public class CustomObjectMapper { @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(Include.NON_EMPTY); objectMapper.registerModule(new ThreeTenModule()); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); return objectMapper; } }` – Visanth Rajamohan Sep 22 '18 at 12:22

1 Answers1

0

After a long investigation, i have noticed @EnableWebMvc annotated configuration bean available in one dependent library. And got to know from here that @EnableWebMvc disables Spring Boot's MVC auto-configuration, thus giving complete control to provide customer MVC configuration. HTTP Message Convertors will also be included in Spring MVC component which in turn disables my custom jackson object mapper configuration.

PS: As jjwt imports jackson databind dependency by default, it fell in my suspect list. Feel good that i could RCA. Thanks.