我上课
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@Table(name = "Sheduler")
public @Data class Lesson {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
public int id;
String state;
String goal;
String hobby;
String result;
String interest;
@JsonProperty("fStudentRequest")
boolean fStudentRequest;
@JsonProperty("fTeacherConfirm")
boolean fTeacherConfirm;
@JsonProperty("fStudentConfirm")
boolean fStudentConfirm;
@OneToOne(
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY
)
@JoinColumn(name = "student_id")
AuthorisedUser student;
@OneToOne(
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY
)
@JoinColumn(name = "teacher_id")
AuthorisedUser teacher;
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY
)
@JoinColumn(name = "order_id")
List<Robokassa> robokassa = new ArrayList<>();
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.EAGER,
mappedBy = "sheduler"
)
List<LessonDays> days = new ArrayList<>();
}
要序列化结果,我使用其他类
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.Data;
import play.Logger;
public @Data class HttpJsonResponse<T> {
int status;
String message;
List<T> data = new ArrayList<>();
int code;
public static <T> String createUserResponse(T data,String message,int code,int status){
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
HttpJsonResponse<T> response = new HttpJsonResponse<T>();
response.setStatus(status);
response.setCode(code);
response.getData().add(data);
response.setMessage(message);
Logger.debug(mapper.writeValueAsString(response));
return mapper.writeValueAsString(response);
} catch (JsonProcessingException e) {
Logger.error(e.toString());
return "{\"status\":0,\"data\":[],\"code\":\"901\",\"message\":\"error\"}";
}
}
}
序列化Lesson类时,我得到重复的字段
String result = HttpJsonResponse
.createUserResponse(
lessons,
"find "+lessons.size(),
CODE_OK,
STAUS_OK
);
对此表示抱歉,但是我无法在自己的帖子中添加更多代码
{
"status":1,
"message":"find 1",
"data":[
[
{
"id":4565,
"state":"4",
"goal":"4",
"hobby":"hobby",
"result":"result",
"interest":"4",
"student":{
"id":0,
"email":null,
"password":null,
"md5":"e22175516bc91b167e80ceae7276d83b",
"hash":null,
"tags":null,
"emotions":null,
"balance":0.0,
"hibernateLazyInitializer":{
}
},
"teacher":{
"id":0,
"email":null,
"password":null,
"md5":"e22175516bc91b167e80ceae7276d83b",
"hash":null,
"tags":null,
"emotions":null,
"balance":0.0,
"hibernateLazyInitializer":{
}
},
"robokassa":null,
"days":[
{
"id":4558,
"from":"10:00",
"to":"10:45",
"fselect":true,
"fSelect":true
},
{
"id":4559,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4560,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4561,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4562,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4563,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4564,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4566,
"from":"10:00",
"to":"11:00",
"fselect":true,
"fSelect":true
},
{
"id":4567,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4568,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4569,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4570,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4571,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
},
{
"id":4572,
"from":null,
"to":null,
"fselect":false,
"fSelect":false
}
],
"fteacherConfirm":true,
"fstudentConfirm":true,
"fstudentRequest":true,
"fStudentRequest":true,
"fTeacherConfirm":true,
"fStudentConfirm":true
}
]
],
"code":500
}
我该怎么做才能删除重复项并获得fStudentConfirm之类的字段
答案 0 :(得分:2)
这些字段名称不相等。区别在于:f
t eacherConfirm
和f
T eacherConfirm
。可能您正在使用Lambok
,它会为您生成吸气剂和吸气剂。在这种特定的情况下,字段名称有一个奇怪的字母f
Lambok
可能会创建is-method
,如下所示:
public boolean isFstudentRequest() {
return fStudentRequest;
}
现在,Jackson
看到您对属性进行了注释,而其他is-method
则具有其他名称,它在JSON
中生成了两个相似(不相等)的属性。
解决方案:
f
或添加以f
开头的整个单词。Lambok
并手动生成is-method
,您可以完全控制生成的名称。is-methods
中禁用Jackson
,如下所示:ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
另请参阅:
答案 1 :(得分:0)
我在Spring Boot中面临类似的问题 我所做的是将@JsonProperty添加到每个getter和setter方法。这可能是一种解决方法。我尝试使用spring“ spring.jackson.mapper”,但没有帮助。
private String Name;
@JsonProperty("Name")
public Name getName() {
return Name;
}
@JsonProperty("Name")
public void setName(Name name) {
Name = name;
}
答案 2 :(得分:0)
忽略lombok getter生成的属性名称:
@Entity
@JsonIgnoreProperties(ignoreUnknown = true, value={"fstudentRequest", "fstudentConfirm"})
@Table(name = "Sheduler")
public @Data class Lesson {
// ...
}