the*_*ddy 1 spring-boot spring-webflux
我是 Spring WebFlux 的新手。我想读取存储在其中的 JSON 文件classpath resource并将其转换为 POJO 类。文件夹结构是resources/defaults/myjson.json. 我正在使用 Jackson 进行转换。阅读后,这将转换为MyJson.java. 截至目前,我正在执行以下方法。
@Service
public class MyService {
private final ObjectMapper objectMapper;
private MyJson myJson;
@Autowired
public MyService(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
this.myJson = MyFileUtils.readFile(objectMapper, "classpath:/defaults/myjson.json", MyJson.class);
}
public MyJson getMyJson() {
return this.myJson;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他办法?
您可以使用ObjectMapper
Pojo pojo = new ObjectMapper().readValue(new ClassPathResource("./defaults/myjson.json").getFile(), Pojo.class);
Run Code Online (Sandbox Code Playgroud)
这里 Pojo 定义你的 pojo 类。
| 归档时间: |
|
| 查看次数: |
7059 次 |
| 最近记录: |