我有一个读取的 JSON 文件,restTemplate
并且只想将某些字段存储到我的对象中。调用成功,但我的对象不包含来自 JSON 的信息。这是文件,我只对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 默认输出)