作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的项目的实体类
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<Session> sessions = new HashSet<Session>();
public Training(){
}
public Training(String topic, TransitionLevel level, Set<Session> 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<Session> getSessions() {
return sessions;
}
public void setSessions(Set<Session> sessions) {
this.sessions = sessions;
}
}
这是 session 表
@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>();</div>
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;
}
}
这是用户实体
@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 将培训和 session 对象保存在我的 mysql 数据库中
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]
最佳答案
要将您自己的类用作映射的键,您需要一个序列化器和反序列化器,就像您指出的那样。就像是:
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));
}
}
@JsonDeserialize(keyUsing = CustomKeyDeserializer.class)
@JsonSerialize(keyUsing = CustomKeySerializer.class)
private Map<User, TrainingRole> users = new HashMap<User, TrainingRole>();
关于hibernate - MappingJackson2HttpMessageConverter 找不到类型的(映射)键反序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39445767/
我正在使用 Spring MVC、AJAX/JSON 和 Hibernate 从 MySQL 数据库中获取所有人。我编写了 JUnit 集成测试来验证我的服务,一切正常。 现在我这样调用它: @Req
我是一名优秀的程序员,十分优秀!