java-使用RestTemplate解析本地JSON文件

我想解析一个本地JSON文件,并使用RestTemplate将其编组为模型,但无法确定是否可行.

我正在尝试在使用RestTemplate与服务器同步的Android应用程序上预填充数据库.我想,为什么不使用RestTemplate而不是自己解析本地JSON?它是专为将JSON解析为模型而设计的.

但是…我无法从文档中得知是否有任何方法可以做到这一点.有一个MappingJacksonHttpMessageConverter类似乎可以将服务器的http响应转换为模型…但是有什么办法可以破解与本地文件一起使用的方法?我试过了,但一直走得越来越深,没有运气.

解决方法:

想通了.除了使用RestTemplate,您还可以直接使用Jackson.没有任何理由需要RestTemplate参与其中.非常简单

try {
    ObjectMapper mapper = new ObjectMapper();

    InputStream jsonFileStream = context.getAssets().open("categories.json");

    Category[] categories = (Category[]) mapper.readValue(jsonFileStream, Category[].class);

    Log.d(tag, "Found "  + String.valueOf(categories.length) + " categories!!");
} catch (Exception e){
    Log.e(tag, "Exception", e);
}
上一篇:java RestTemplate.postForObject请求传参


下一篇:java-取消/中止/中断spring-android resttemplate请求