Search code examples
javaspring-bootjacksonvavr

Serializer/Deserializer for Vavr objects


Hello Im trying to add vavr to my projet, right now Im struggling with proper serializaition of Vavr.List objects. Below is my controller:

import io.vavr.collection.List;

 @GetMapping(value = "/xxx")
    public List<EntityDeleted> getFile() {
        return List.of(new EntityDeleted(true),new EntityDeleted(true),new EntityDeleted(true),new EntityDeleted(true));
}

EntityDeleted is my custom object, List is Vavr collection as shown in import statement. The response Im getting in Postman is:

{
    "empty": false,
    "lazy": false,
    "async": false,
    "traversableAgain": true,
    "sequential": true,
    "singleValued": false,
    "distinct": false,
    "ordered": false,
    "orNull": {
        "deleted": true
    },
    "memoized": false
}

where I expect JSON list of my objects. Below is my config:

@SpringBootApplication
public class PlomberApplication {

    public static void main(String[] args) {
        SpringApplication.run(PlomberApplication.class, args);
    }

    @Bean
    public ObjectMapper jacksonBuilder() {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.registerModule(new VavrModule());
    }
}

and bit of pom.xml

 <dependency>
            <groupId>io.vavr</groupId>
            <artifactId>vavr</artifactId>
            <version>0.9.0</version>
        </dependency>
        <dependency>
            <groupId>io.vavr</groupId>
            <artifactId>vavr-jackson</artifactId>
            <version>0.9.0</version>
        </dependency>

Solution

  • Spring Boot retrieves all instances of com.fasterxml.jackson.databind.Module class and initializes ObjectMapper with them. There's no need for additional magic.

    My dependencies are as follows (Spring Boot 1.5.7.RELEASE):

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web')
        testCompile('org.springframework.boot:spring-boot-starter-test')
        compile group: 'io.vavr', name: 'vavr', version: '0.9.1'
        compile group: 'io.vavr', name: 'vavr-jackson', version: '0.9.1'
    }
    

    With application configurured like this:

    @SpringBootApplication
    public class BootvavrApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BootvavrApplication.class, args);
        }
    
        @Bean
        Module vavrModule() {
            return new VavrModule();
        }
    }
    

    and controller mapped like this:

    import io.vavr.collection.List;
    @RestController
    class TestController {
    
        @GetMapping("/test")
        List<String> testing() {
            return List.of("test", "test2");
        }
    }
    

    output is:

    ["test","test2"]
    

    You can check the code out here: https://github.com/mihn/bootvavr