将Java对象转换为JSON对象时出错

时间:2017-11-29 14:42:13

标签: java json

使用jackson API,我试图将java对象转换为json字符串。我已经完成了几个帖子,但无法通过下面的堆栈跟踪。

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.imc.idm.data.request.DirectoryDetailData["logoImage"]->org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:284)
    at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1110)
    at com.fasterxml.jackson.databind.SerializerProvider.reportMappingProblem(SerializerProvider.java:1135)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:69)

我的代码与下面的代码类似:

public class Infomation {
    private MultipartFile img;
    private Manufacture manufacture;
    private List<Long> otherVal1;
    private List<MyCustomClass> otherVal2;
}

组件:

ObjectMapper objectMapper = new ObjectMapper();
final String jsonString = objectMapper.writeValueAsString(infomationObject);
final MyDTO myDto = MyDTO.newBuilder().withValue(jsonString).build();
dataRepository.save(myDto);

1 个答案:

答案 0 :(得分:1)

MultipartFile 中未实现可序列化界面/标记。

如果您的依赖项中有 Spring Web ,则可以使用 CommonsMultipartFile ,因为它实现了Serializable和MultipartFile。

有关详细信息,请参阅Commons Multipart File Java API