您好,我正在尝试将vavr添加到我的项目中,现在,我正在为Vavr.List对象的正确序列化而苦苦挣扎。以下是我的 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 的回复是:
{
    "empty": false,
    "lazy": false,
    "async": false,
    "traversableAgain": true,
    "sequential": true,
    "singleValued": false,
    "distinct": false,
    "ordered": false,
    "orNull": {
        "deleted": true
    },
    "memoized": false
}

我期望对象的JSON列表。下面是我的配置:
@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
 <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 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();
    }
}

和 Controller 映射如下:
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

09-05 21:12