本文介绍了Vavr 对象的序列化器/反序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试将 vavr 添加到我的项目中,现在我正在努力解决 Vavr.List 对象的正确序列化问题.下面是我的控制器:

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 是我的自定义对象,List 是 Vavr 集合,如 import 语句所示.我在 Postman 中得到的回应是:

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
}

我期望我的对象的 JSON 列表.下面是我的配置:

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());
    }
}

和一点 pom.xml

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>

推荐答案

Spring Boot 检索 com.fasterxml.jackson.databind.Module 类的所有实例并初始化 ObjectMapper与他们.不需要额外的魔法.

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

我的依赖如下(Spring Boot 1.5.7.RELEASE):

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'
}

应用程序配置如下:

@SpringBootApplication
public class BootvavrApplication {

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

    @Bean
    Module vavrModule() {
        return new VavrModule();
    }
}

和控制器映射如下:

import io.vavr.collection.List;
@RestController
class TestController {

    @GetMapping("/test")
    List<String> testing() {
        return List.of("test", "test2");
    }
}

输出为:

["test","test2"]

您可以在此处查看代码:https://github.com/mihn/bootvavr

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

这篇关于Vavr 对象的序列化器/反序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 04:52