【发布时间】:2021-08-11 01:06:18
【问题描述】:
配置中的yamlObjectMapper
@Bean
public ObjectMapper yamlObjectMapper() {
ObjectMapper yamlObjectMapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature
.WRITE_DOC_START_MARKER));
yamlObjectMapper.findAndRegisterModules();
return yamlObjectMapper;
}
解析yaml文件的服务
@Service
public class CustomerService {
@Autowired
@Qualifier("yamlObjectMapper")
private ObjectMapper yamlObjectMapper;
public Customer get() {
try {
InputStream inputStream = ResourceUtils.getURL("classpath:/files/test.yaml").openStream();
return yamlObjectMapper.readValue(inputStream, Customer.class);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
@Data
public static class Customer {
private String name;
private String surname;
private String email;
}
}
我猜 IO 操作是阻塞的,如何使用响应式方式来完成?
【问题讨论】:
-
我不认为你可以轻易做到非阻塞,所以你最好的办法是用
Mono.fromCallable包装代码块并将工作移动到一个用于阻塞的线程池,如.subscribeOn(Schedulers.boundedElastic()) -
@MartinTarjányi 谢谢,你能展示完整的例子吗
标签: spring-boot spring-webflux project-reactor jackson-dataformat-yaml