listaDecretoSingolo = new ArrayList<>();
listaDettaglioDecretoSingolo = new ArrayList<>();
for(int i = 0; i < 1022; i++){
decretoSingoloTemp = new DecretoSingoloViewObject("Roma", "ABCDE3593CXXE",
"decreto singolo", date,new BigDecimal(56000), new BigDecimal(343434),
new BigDecimal(55656), new BigDecimal(9999));
for(int j = 0; j < 111; j++){
listaDettaglioDecretoSingolo.add(new DettaglioDecretoSingoloViewObject(22,
date, "numero dec pag",
"tipo pag", new BigDecimal(45),
"nota dec"));
}
decretoSingoloTemp.setDetails(listaDettaglioDecretoSingolo);
listaDecretoSingolo.add(decretoSingoloTemp);
}
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String jsonList = null;
jsonList = ow.writeValueAsString(listaDecretoSingolo);
我想测试上面这段代码的速度。但是,当我运行它,并尝试将列表转换为JSON字符串时,我得到了这个异常:
at com.fasterxml.jackson.core.util.TextBuffer.carr(TextBuffer.java:703) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.util.TextBuffer.expand(TextBuffer.java:664) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.util.TextBuffer.append(TextBuffer.java:455) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.io.SegmentedStringWriter.write(SegmentedStringWriter.java:67) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._flushBuffer(WriterBasedJsonGenerator.java:1910) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeRawLong(WriterBasedJsonGenerator.java:496) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeRaw(WriterBasedJsonGenerator.java:430) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.util.DefaultPrettyPrinter$Lf2SpacesIndenter.writeIndentation(DefaultPrettyPrinter.java:472) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.util.DefaultPrettyPrinter.beforeObjectEntries(DefaultPrettyPrinter.java:265) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._writePPFieldName(WriterBasedJsonGenerator.java:297) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._writeFieldName(WriterBasedJsonGenerator.java:227) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeFieldName(WriterBasedJsonGenerator.java:116) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:539) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:114) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:809) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:684) [jackson-databind-2.3.3.jar:2.3.3]
at it.istruzione.hp.scuoleSicure.controller.ConsultaPagamentiController.ricercaConsultaImpegni(ConsultaPagamentiController.java:116) [classes:]
at it.istruzione.hp.scuoleSicure.controller.ConsultaPagamentiController$$FastClassBySpringCGLIB$$8a5debc0.invoke(<generated>) [spring-core-4.0.5.RELEASE.jar:]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708) [spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
当列表尺寸较小(如10,20,30 ......)时,我不会收到此错误。但是,当我增加数量,接近数千或更少,我有这个问题......
有什么方法可以解决它吗?
答案 0 :(得分:1)
您正在创建大量对象,因此需要大量内存。尝试使用-Xmx选项来增加最大堆大小。
答案 1 :(得分:1)
尝试将json写为ObjectWritter#writeValueAsBytes或ObjectWritter#writeValue(OutputStream out,Object value)
写作字符串非常无效
这样的事情:
Bar.java
package org.ibm.jackson.example;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Bar {
private String id;
@JsonCreator
public Bar(@JsonProperty("id") String id) {
this.id = id;
}
public String getId() {
return this.id;
}
@Override
public String toString() {
return "Bar [id=" + id + "]";
}
}
JacksonMain.java
package org.ibm.jackson.example;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
public class JacksonMain {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String json = "{\"id\": \"foo\"}";
ObjectMapper mapper = new ObjectMapper();
final Bar bar = mapper.readValue(json, Bar.class);
//Read json
System.out.println(bar);
ObjectWriter objectWriter = mapper.writerWithDefaultPrettyPrinter();
byte[] barWritten = objectWriter.writeValueAsBytes(bar);
//Written json
System.out.println(new String(barWritten)); //Just to show - you should write something like ByteArrayInputStream bis = new ByteArrayInputStream(barWritten);
}
}