MappingJackson2HttpMessageConverter Can not find a (Map) Key deserializer for type
以下是我的项目的实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import java.util.Iterator; import java.util.Map; import java.util.Set; @Entity @Table(name="training") public class Training { @Id @GeneratedValue private long id; private String topic; @OneToMany(mappedBy="training") private Set sessions = new HashSet(); public Training(){ } public Training(String topic, TransitionLevel level, Set sessions) { this.topic = topic; this.level = level; this.sessions = sessions; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTopic() { return topic; } public void setTopic(String topic) { this.topic = topic; } public Set getSessions() { return sessions; } public void setSessions(Set sessions) { this.sessions = sessions; } } |
这是会议桌
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | @Entity @Table(name="session") public class Session { @Id @GeneratedValue private long id; private String location; @ManyToOne @JoinColumn(name="training_id") @JsonIgnore private Training training; private Date start; private Date end; @JoinTable(name="session_user", joinColumns = @JoinColumn(name="session_id"), inverseJoinColumns = @JoinColumn(name="trainingRole_id")) @MapKeyJoinColumn(name="user_id") @ElementCollection @JsonIgnore private Map<User, TrainingRole> users = new HashMap<User, TrainingRole>(); public long getId() { return id; } public void setId(long id) { this.id = id; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Training getTraining() { return training; } public void setTraining(Training training) { this.training = training; } public Date getStart() { return start; } public void setStart(Date start) { this.start = start; } public Date getEnd() { return end; } public void setEnd(Date end) { this.end = end; } public Map <User, TrainingRole> getUsers() { return users; } public void setUsers(Map<User, TrainingRole> users) { this.users = users; } } |
号
这是用户实体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | @Entity @Table(name="user") public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private long id; @Column(name="csl",unique=true) private String csl; @Column(name="fullName") private String fullName; @Column(name="email") private String email; public User() { } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getCsl() { return csl; } public void setCsl(String csl) { this.csl = csl; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } } |
我正在使用jparepository将培训和会话对象保存在mysql数据库中
但是每当我保存训练对象或会话对象时
我有错误
1 | c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [simple type, class Session]: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class User] |
。
我在谷歌上搜索了一下,发现我需要手动序列化和反序列化。但我不知道该怎么做……请帮帮我……
要使用您自己的类作为映射的键,您需要一个序列化程序和反序列化程序,如您所指出的。比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class CustomKeyDeserializer extends KeyDeserializer { private static ObjectMapper mapper = new ObjectMapper(); @Override public Object deserializeKey(String key, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { return mapper.readValue(key, User.class); } } public class CustomKeySerializer extends JsonSerializer<User> { private static ObjectMapper mapper = new ObjectMapper(); @Override public void serialize(User value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { gen.writeFieldName(mapper.writeValueAsString(value)); } } |
并对字段进行注释
1 2 3 | @JsonDeserialize(keyUsing = CustomKeyDeserializer.class) @JsonSerialize(keyUsing = CustomKeySerializer.class) private Map<User, TrainingRole> users = new HashMap<User, TrainingRole>(); |
号