java - 无法使用 Spring Boot 将 JSON 部分反序列化为 Immutables 对象

标签 java spring-boot jackson immutables-library

我有一个通过 restTemplate 读取的 JSON 文件,并且只想将某些字段存储到我的对象中。调用成功,但我的对象不包含 JSON 中的信息。 This是文件,我只对 my-channels 对象感兴趣。

我的pom.xml:

<!-- SNIP standard stuff generated by start.spring.io -->
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jdk8</artifactId>
            <version>2.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.immutables</groupId>
            <artifactId>value</artifactId>
            <version>2.5.6</version>
            <scope>provided</scope>
        </dependency>

我的 JacksonConfig 禁用了缺失字段的失败并启用了 kebab-case:

@Configuration
public class JacksonConfig {
    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        final ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.registerModule(new Jdk8Module());

        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);

        return objectMapper;
    }
}

保存我的数据的对象。

@Value.Immutable
@JsonSerialize(as = com.example.demo.ImmutableGist.class)
@JsonDeserialize(as = com.example.demo.ImmutableGist.class)
public interface AbstractGist {
    List<ImmutableMyChannels> myChannels();

    @Value.Immutable
    @JsonSerialize(as = ImmutableMyChannels.class)
    @JsonDeserialize(as = ImmutableMyChannels.class)
    interface AbstractMyChannels {
        String channelId();

        String locale();
    }
}

以及我的 main 和 commandlinerunner:

@SpringBootApplication
public class DemoApplication {

    private static final String URL = "https://rawgit.com/Gregsen/936f0dc5f1a83687ada6b64a0fe20a0c/raw/1ee44d3e7aa94851a811108b2606e803f8734c8d/example.json";
    private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);

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


    @Bean
    public CommandLineRunner run() throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        return args -> {
            ImmutableGist gist = restTemplate.getForObject(URL, ImmutableGist.class);

            log.info(gist.toString());
        };
    }
}

应用程序编译并运行,但是输出只是 Gist{salesChannels=[]} (减去整个 spring 默认输出)

最佳答案

因此,经过更多的摆弄和阅读,似乎问题可以这样解决:

不应使用@JsonDeserialize(as = ImmutableMyChannels.class),而应使用同样生成的构建器。

@JsonDeserialize(builder = ImmutableMyChannels.Builder.class)

关于java - 无法使用 Spring Boot 将 JSON 部分反序列化为 Immutables 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49831675/

相关文章:

java - 编辑.java文件并重新编译

java - Jackson Joda 模块将 ISO 日期反序列化为 Joda DateTime

java - 如何将多态json字符串转换为java bean类

java - 使用 Jackson 在 Java 中定义的 POJO 将 JSON 摄取到 Spark RDD 中

java - Lombok,仅使用 getter setter 不等于或 tostring

java - log4j 不使用 websphere 登录 AIX

java - 格式日期Java

java - 在 JavaFX 应用程序中将 Spring Security 附加功能与 Spring Boot 结合使用

grails - 一个组件需要一个名为 'dataSource_dbCreate' 的 bean,但找不到

database - @JoinColumn 和 mappedBy 用于拥有方和反方