简体   繁体   English

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

[英]Cannot partially deserialize JSON into Immutables object with spring boot

I have a JSON file that I read via restTemplate and want to store only certain fields in to my object. 我有一个通过restTemplate读取的JSON文件,并且只想将某些字段存储到我的对象中。 The call is successfull but my object does not contain the informations from the JSON. 调用成功,但是我的对象不包含来自JSON的信息。 This is the file and I am only interested in the my-channels object. 是文件,我只对my-channels对象感兴趣。

my pom.xml: 我的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>

my JacksonConfig that disables failing on missing fields and has kebab-case enabled: 我的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;
    }
}

The object to hold my data. 保存我的数据的对象。

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

and my main with the commandlinerunner: 和我的主要与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());
        };
    }
}

The application compiles and runs, the output, however, is simply Gist{salesChannels=[]} (minus the whole spring default output) 该应用程序编译并运行,但是输出只是Gist{salesChannels=[]} (减去整个spring默认输出)。

So, after some more fiddling around and reading, it seems, the issue can be resolved as such: 因此,经过一些摆弄和阅读之后,看来可以通过以下方式解决该问题:

instead of using @JsonDeserialize(as = ImmutableMyChannels.class) one should use the builder, that is also generated. 而不是使用@JsonDeserialize(as = ImmutableMyChannels.class)应该使用生成器,该生成器也会生成。

@JsonDeserialize(builder = ImmutableMyChannels.Builder.class) . @JsonDeserialize(builder = ImmutableMyChannels.Builder.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM