ฉันมีปัญหาใน SpringMVC RestController
ที่เขียนผลลัพธ์ JSON:
1) เมื่อฉันส่งคืน อ็อบเจ็กต์โดเมนด้านล่าง ActivitiesT ฉันได้รับข้อผิดพลาดเซิร์ฟเวอร์ภายใน AJAX 500 บนฝั่งไคลเอ็นต์
@RequestMapping("/participant/activityForEvent")
public ActivitiesT getActivityForGuiEventId() throws Exception {
ActivitiesT activitiesT = participantService.getActivity();
return activitiesT;
}
อ็อบเจ็กต์โดเมน ActivitiesT ที่สร้างโดย Hiberate:
@Entity
@Table(name = "activities_t", schema = "public")
public class ActivitiesT implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
//... etc...
}
2) แต่เมื่อฉันส่งคืน POJO แบบกำหนดเอง วิธี AJAX ก็ใช้งานได้ มันเกือบจะเหมือนกัน
@RequestMapping("/participant/activityForEvent")
public ActivitiesT getActivityForGuiEventId() throws Exception {
ActivitiesT activitiesT = participantService.getActivity();
// Create a custom Activity POJO and return it
return new Activity(activitiesT.id, activitiesT.title);
}
POJO ที่กำหนดเองของกิจกรรม:
public class Activity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public int id;
public String title;
//etc.
ข้อความแสดงข้อผิดพลาด
HTTP Status 500 - Could not write JSON: could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy -
ไม่มีเซสชัน (ผ่านห่วงโซ่การอ้างอิง: com.myapp")
บางสิ่งที่ควรทราบ:
- วัตถุทั้งสองใช้
Serializable
ก็ไม่เป็นไร - ปัญหาไม่ใช่ DAO ฉันเห็นว่า DAO ส่งคืนข้อมูลที่ถูกต้องสำหรับทั้งคู่เสมอ เราสามารถเพิกเฉยต่อส่วน DAO ได้
- คอนโทรลเลอร์มีคำอธิบายประกอบด้วย
@RestController
ดังนั้นจึงเอาต์พุต JSON เสมอ ฉันไม่ต้องการ ResponseBody หรือแท็กที่เก่ากว่า
มีความคิดใด ๆ เกี่ยวกับปัญหาที่อาจเกิดขึ้น? ปัญหาเกิดขึ้นดังนี้:
- ฉันไม่สามารถส่งคืน JSON จากวิธี RestController บน @Entity POJO ที่สร้างโดย Hibernate ได้
- แต่ฉันสามารถส่งคืน JSON จากวิธี RestController บน POJO ที่กำหนดเองของฉันเองได้
500 Internal Server Error
) ได้ไหม - person lzagkaretos   schedule 18.12.2017ActivitiesT
เป็นวัตถุที่เรียบง่ายหรือซับซ้อน (แค่เดา) โดยมีข้อยกเว้นบางประเภทเกิดขึ้นหลังจากที่ตัวควบคุมส่งคืนค่า - person lzagkaretos   schedule 18.12.2017HTTP Status 500 - Could not write JSON: could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.myapp")
- person gene b.   schedule 18.12.2017