我从angularjs传递json对象,该对象存储在数据
中我想逐个注册/保存这些数据,但控制器不会进入foreach循环。
如果我的JSON对象密钥和DTO属性名称相同,我是否必须设置DTO的每个属性,如何简单地转换它?
@ResponseBody
@RequestMapping(value = "/registerStudent.do", method = RequestMethod.POST)
public boolean registerStudent(@RequestBody String data) {
Gson googleJson = new Gson();
ArrayList<StudentDTO> stdList = googleJson.fromJson(data, ArrayList.class);
if (stdList != null) {
for (StudentDTO stdObj : stdList) {
registerService.register(stdObj);
}
}
return registerService.isStudentExist(stdList);
}
我的JSON对象是这样的:
[{fname=Karmesh, lname=Madhavi, gender=Male, dob=2016-11-07T18:30:00.000Z, email=abcd@gmail.com, contact=1234567890, yop=2020, degree=M.Sc, branch=CS, perc=78, state=Karnataka, city=Bangalore}]
我的StudentDTO是这样的:
@Entity
@Table(name = "student_table")
public class StudentDTO implements Serializable {
@Id
@GenericGenerator(name = "incre", strategy = "increment")
@GeneratedValue(generator = "incre")
@Column(name = "s_id")
@NotNull
private int sid;
@Column(name = "s_fname")
@NotNull
@Size(min = 3, max = 20)
private String fname;
@Column(name = "s_lname")
@NotNull
private String lname;
@Column(name = "s_gender")
@NotNull
private String gender;
@Column(name = "s_dob")
private Date dob;
@Column(name = "s_email")
@NotNull
private String email;
@Column(name = "_scontact")
@NotNull @Pattern(regexp="\\d{10}")
private String contact;
@Column(name = "s_yop")
@NotNull @Min(4) @Max(4)
private int yop;
@Column(name = "s_degree")
@NotNull
private String degree;
@Column(name = "s_branch")
@NotNull
private String branch;
@Column(name = "s_perc")
@NotNull @Digits(integer=2, fraction=2)
private double perc;
@Column(name = "s_state")
@NotNull
private String state;
@Column(name = "s_city")
@NotNull
private String city;
public StudentDTO() {
System.out.println(this.getClass().getSimpleName() + "created..");
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public int getYop() {
return yop;
}
public void setYop(int yop) {
this.yop = yop;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public double getPerc() {
return perc;
}
public void setPerc(double perc) {
this.perc = perc;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "StudentDTO [sid=" + sid + ", fname=" + fname + ", lname=" + lname + ", gender=" + gender + ", dob="
+ dob + ", email=" + email + ", yop=" + yop + ", degree=" + degree + ", branch=" + branch + ", perc="
+ perc + ", state=" + state + ", city=" + city + "]";
}
}
答案 0 :(得分:0)
我认为它会抛出以下异常。
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.model.StudentDTO
<强>解决方案: - 强>
更改此声明: -
ArrayList<StudentDTO> stdList = gson.fromJson(data, ArrayList.class);
要: - 强>
List<StudentDTO> stdList = gson.fromJson(data, new TypeToken<ArrayList<StudentDTO>>() {}.getType());
这应解决问题。它对我有用。