获取输入流对象

时间:2016-07-19 08:36:04

标签: java excel apache serialization jackson

尝试使用jackson序列化时,我收到以下异常

  

com.fasterxml.jackson.databind.JsonMappingException:没有序列化程序   找到类java.io.ByteArrayInputStream,没有属性   发现创建BeanSerializer(以避免异常,禁用   SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过参考链:   com.olacabs.deviceplatform.common.entities.WrappedInputStream [" WB"] - > org.apache.poi.xssf.usermodel.XSSFWorkbook ["的PackagePart"] - >有机apache.poi.openxml4j.opc.ZipPackagePart [" zipArchive"] - > org.apache.poi.openxml4j.util.FakeZipEntry ["的inputStream"])

我按照http://www.baeldung.com/jackson-jsonmappingexception中的说明尝试了解决方案,其中我们使用以下代码将可见性视为全部mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

之后导致stackoverflow错误。

  

com.fasterxml.jackson.databind.JsonMappingException:无限   递归(StackOverflowError)(通过引用链:   org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels" ] - GT; org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container" ] - GT; org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels&# 34;] - > org.a pache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache。 poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - >有机apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - >有机apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.open xml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache。 poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - >有机apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache。 poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - >有机apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - >有机apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.ope nxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache。 poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi。 openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache。 poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - >有机apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache。 poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - >有机apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4 j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j。 opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi。 openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache。 poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi。 openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache。 poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - >有机apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache。 poi.openxml4j.opc.PackageP artCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j。 opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi。 openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j。 opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi。 openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache。 poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi。 openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache。 poi.openxml4j.opc.ZipPack年龄[" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j。 opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc。 PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j。 opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi。 openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j。 opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi。 openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache。 poi.openxml4j.opc.ZipPackagePa室温[" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [ " / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc。 ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j。 opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc。 PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j。 opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi。 openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j。 opc.PackagePartCollection [&#34 ; / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [& #34; PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [& #34; / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc .ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc .ZipPackage ["&PARTLIST #34;] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [&#34 ; _container" - ] - >; org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST&#34] GT org.apache.poi.openxml4j.opc.PackagePartCollection [" / _rels /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [&#34 ; PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [& #34; _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [&#34 ; / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container"] - > org.apache.poi.openxml4j.opc.ZipPackage [& #34; PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /的.rels"] - > org.apache.poi.openxml4j.opc.ZipPackagePart [" _container "] - > org.apache.poi.openxml4j.opc.ZipPackage [" PARTLIST"] - > org.apache.poi.openxml4j.opc.PackagePartCollection [" / _ RELS /.rels"]->org.apache.poi.openxml4j.opc.ZipPackagePart["_container"]->org.apache.poi.openxml4j.opc.ZipPackage[" partList"] ....剪裁

代码 自定义WrappedInputStreamclass

@Data
@NoArgsConstructor
@AllArgsConstructor
public class WrappedInputStream implements Serializable  {

    private static final long serialVersionUID = 12358903454875L;

    private String transactionKey;
    private Workbook wb;
}

Workbook在某个级别上有inputStream ..即使我直接从multipartFile对象包含inputStream,也会导致同样的问题。

发生错误的代码()

public boolean uploadData(MultipartFile inventoryData) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            wb = WorkbookFactory.create(inventoryData.getInputStream());
        } catch (InvalidFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        WrappedInputStream wrappedInputStream = new WrappedInputStream(key, wb);
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        String wrappedInputStreamString = mapper.writeValueAsString(wrappedInputStream); //exception occurs at this place

//process the string further
}

任何人都知道解决方案吗?

1 个答案:

答案 0 :(得分:0)

您需要编辑DTO并在子类中添加注释@JsonIgnore,其中提及对父类的引用。

@JsonIgnore
private Member member;

这可以解决您的问题。