我在将数据从 List 转换为 Json(Gson) 时遇到问题。我有 2 个与 OneToMany 和 ManyToOne 关系的实体。我尝试使用其他帖子中答案的解决方案,但没有帮助。
关于我的实体和错误的一些信息。
实体
@Entity
@Table(name = "Question")
public class Question implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String question;
@OneToMany(mappedBy="question")
private List<Answer> answers;
.....getters/setters
@Override
public String toString() {
return "Question [id=" + id + ", question=" + question + ", answers="
+ answers + "]";
}
回答实体
@Entity
public class Answer{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
// @GeneratedValue
private int id;
private String answer;
private byte isCorrect;
@ManyToOne
@JoinColumn(name="QuestionID")
private Question question;
.....getters/setters
@Override
public String toString() {
return "Answer {id:" + id + ", answer:" + answer + ", isCorrect:"
+ isCorrect + "}";
}
道
@Repository
@Transactional
public class QuestionDAOImpl implements QuestionDAO {
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
// @Transactional
public List<Question> getQuestion() {
// TODO Auto-generated method stub
return sessionFactory.getCurrentSession().createQuery("from Question").list();
}
}
控制器
@Controller
public class QuestionController {
@Autowired
private QuestionService questionService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model){
List<Question> qu =questionService.getQuestion();
Gson gson = new Gson();
String m = gson.toJson(qu);
//Gson gs = new GsonBuilder().registerTypeAdapter(Question.class, myAdapter).create();
System.out.println(qu);
return "home";
}
}
得到错误
java.lang.StackOverflowError
com.google.gson.internal.$Gson$Types.equals($Gson$Types.java:158)
com.google.gson.reflect.TypeToken.equals(TypeToken.java:284)
java.util.HashMap.getEntry(Unknown Source)
java.util.HashMap.get(Unknown Source)
java.util.Collections$SynchronizedMap.get(Unknown Source)
com.google.gson.Gson.getAdapter(Gson.java:332)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
现在有新的错误
java.lang.IllegalArgumentException
com.google.gson.internal.$Gson$Preconditions.checkArgument($Gson$Preconditions.java:42)
com.google.gson.GsonBuilder.registerTypeAdapter(GsonBuilder.java:448)
ua.home.rusliakov.web.QuestionController.home(QuestionController.java:71)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
也许我做错了什么。
谢谢!