我认为我需要重写我的应用程序的一些模块,因为当实体的数量被渲染的增加,它失败和错误。现在,我在利用杰克逊httpcliend.尽管我信任杰克逊,但直觉告诉我问题出在第二个lib。可以Httpcliend处理大的响应?(如。这一个大概有400行)
除此之外,在我的应用程序中,我解析请求的方式是这样的:
公共对象处理(HttpResponse response, String rootName){试一试{String json = EntityUtils.toString(response.getEntity());//更好的"new BasicResponseHandler().handleResponse(response)"????intstatusCode = response.getStatusLine () .getStatusCode ();如果(statusCode > =200& & statusCode <300){返回createObject (json, rootName);}其他的{返回零;}}抓(异常e) {扔新RuntimeException (e);}}公共对象createObject(String json, String rootName){试一试{这.root =这.mapper.readTree (json);String className = findclassname (rootName);类clazz =这.getObjectClass(名称);返回mapper.treeToValue (root.get (rootName) clazz);}抓(异常e) {扔新RuntimeException (e);}}
我如何改进这段代码,以更有效的大响应?
没有必要创建Json字符串,因为objectmap # readtree可以接受一个inputStream也例如,这样会更有效率:
公共对象处理(HttpResponse response, String rootName){试一试{intstatusCode = response.getStatusLine () .getStatusCode ();如果(statusCode > =200& & statusCode <300){返回createObject (response.getEntity () .getContent (), rootName);}其他的{返回零;}}抓(异常e) {扔新RuntimeException (e);}}公共对象createObject(InputStream json, String rootName){试一试{这.root =这.mapper.readTree (json);String className = findclassname (rootName);类clazz =这.getObjectClass(名称);返回mapper.treeToValue (root.get (rootName) clazz);}抓(异常e) {扔新RuntimeException (e);}}