【问题标题】:Cannot partially deserialize JSON into Immutables object with spring boot无法使用 Spring Boot 将 JSON 部分反序列化为 Immutables 对象
【发布时间】:2018-04-14 12:55:43
【问题描述】:

我有一个通过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();
    }
}

和我主要的命令行运行器:

@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默认输出)

【问题讨论】:

    标签: java spring-boot jackson immutables-library


    【解决方案1】:

    所以,经过一番折腾和阅读,问题似乎可以这样解决:

    应该使用生成器,而不是使用@JsonDeserialize(as = ImmutableMyChannels.class)

    @JsonDeserialize(builder = ImmutableMyChannels.Builder.class)

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 2018-04-08
      • 2021-06-08
      • 1970-01-01
      相关资源
      最近更新 更多