我有一个带有CommonsMultipartFile类型字段的bean,如下所示:
public class Placement implements Serializable {
private static final long serialVersionUID = 1L;
private long placementId;
private String type;
private String placement;
private transient CommonsMultipartFile fileData;
我已将CommonsMultipartFile字段标记为瞬态并尝试使用jackson库序列化为json。但是得到以下错误:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: nextag.travel.dashboard.model.Placement["fileData"]->org.springframework.web.multipart.commons.CommonsMultipartFile["inputStream"])
任何帮助/建议都将受到高度赞赏。
答案 0 :(得分:5)
目前还不清楚杰克逊是如何被使用的,因为原始问题中没有提供任何代码或描述。
默认情况下,Jackson会在序列化过程中跳过所有瞬态字段。
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
System.out.println(mapper.writeValueAsString(new Placement()));
// output: {"placementId":42,"type":"OK","placement":"left"}
// transient fields are skipped by default
}
}
class Placement implements Serializable
{
private static final long serialVersionUID = 1L;
private long placementId = 42;
private String type = "OK";
private String placement = "left";
private transient CommonsMultipartFile fileData = new CommonsMultipartFile();
}
class CommonsMultipartFile
{
private String name = "Fred";
}
如果瞬态场有吸气剂,那么默认情况下杰克逊会在序列化过程中包含它。
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
System.out.println(mapper.writeValueAsString(new Placement()));
// output: {"placementId":42,"type":"OK","placement":"left","fileData":{"name":"Fred"}}
// transient fields with getters are not skipped by default
}
}
class Placement implements Serializable
{
private static final long serialVersionUID = 1L;
private long placementId = 42;
private String type = "OK";
private String placement = "left";
private transient CommonsMultipartFile fileData = new CommonsMultipartFile();
public CommonsMultipartFile getFileData() {return fileData;}
}
class CommonsMultipartFile
{
private String name = "Fred";
}
跳过getter的一个配置选项是仅应用@JsonIgnore注释。
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
System.out.println(mapper.writeValueAsString(new Placement()));
// output: {"placementId":42,"type":"OK","placement":"left"}
// getters marked with @JsonIgnore are ignored
}
}
class Placement implements Serializable
{
private static final long serialVersionUID = 1L;
private long placementId = 42;
private String type = "OK";
private String placement = "left";
private transient CommonsMultipartFile fileData = new CommonsMultipartFile();
@JsonIgnore
public CommonsMultipartFile getFileData() {return fileData;}
}
class CommonsMultipartFile
{
private String name = "Fred";
}
如果编辑原始类定义以添加@JsonIgnore注释是不可能或不可取的,则可以使用Mix-In。
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
mapper.getSerializationConfig().addMixInAnnotations(Placement.class, SkipFileDataMixIn.class);
System.out.println(mapper.writeValueAsString(new Placement()));
// output: {"placementId":42,"type":"OK","placement":"left"}
// getters marked with @JsonIgnore are ignored
}
}
abstract class SkipFileDataMixIn
{
@JsonIgnore
public abstract CommonsMultipartFile getFileData();
}
class Placement implements Serializable
{
private static final long serialVersionUID = 1L;
private long placementId = 42;
private String type = "OK";
private String placement = "left";
private transient CommonsMultipartFile fileData = new CommonsMultipartFile();
public CommonsMultipartFile getFileData() {return fileData;}
}
class CommonsMultipartFile
{
private String name = "Fred";
}
另一种方法是使用@JsonIgnoreType
标记要跳过的类型。
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonIgnoreType;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
System.out.println(mapper.writeValueAsString(new Placement()));
// output: {"placementId":42,"type":"OK","placement":"left"}
// Types marked with @JsonIgnoreType are ignored during serialization.
}
}
class Placement implements Serializable
{
private static final long serialVersionUID = 1L;
private long placementId = 42;
private String type = "OK";
private String placement = "left";
private transient CommonsMultipartFile fileData = new CommonsMultipartFile();
public CommonsMultipartFile getFileData() {return fileData;}
}
@JsonIgnoreType
class CommonsMultipartFile
{
private String name = "Fred";
}
答案 1 :(得分:4)
如果您不想序列化多部分文件,请将注释@JsonIgnore
添加到字段中。
@JsonIgnore
private CommonsMultipartFile fileData;
您可以阅读更多abot jackson annotations here。