2014-06-07 12 views
5

मैं सर्वलेट में कुछ परेशानी हो रहा है जिसमें हर मैं एक ड्रॉपडाउन मेनू में विकल्प नहीं बदलेंगे, भिन्न मान सर्वलेट को दे दिया जाएगा और फिर इसे एक अनंत लूप में परिणाम है। जब मैं ड्रॉपडाउन में विकल्प (मूल्य में कोई परिवर्तन नहीं) बदल रहा हूं, तो कोई त्रुटि नहीं है।सर्वलेट Gson()। ToJson अनंत लूप

मेरे जावास्क्रिप्ट:

<script> 

function loadStaff(){ 
//dropdown 
var positionDropDown = document.getElementById("positionsDropdown"); 
//value of the drop down 
var positionID = positionDropDown.options[positionDropDown.selectedIndex].value; 

    $.getJSON('loadStaff?positionID=' + positionID, function(data) { 
      -- no populate code yet 
}); 
} 
</script> 

मेरे AjaxServlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     String userPath = request.getServletPath(); 

    if (userPath.equals("/loadStaff")) { 

     String positionID = request.getParameter("positionID"); 
     Position position = positionFacade.find(Integer.parseInt(positionID)); 
     Collection staffCollection = position.getStaffCollection(); 
     List<Staff> staffList = new ArrayList(staffCollection); 

     String staffListJson = new Gson().toJson(staffList); 
     response.setContentType("application/json"); 
     response.setCharacterEncoding("UTF-8"); 
     response.getWriter().write(staffListJson); 

    } 

} 

debuggig पर

यहाँ mycode है। त्रुटि पंक्ति पर आता है:

String staffListJson = new Gson().toJson(staffList); 

आउटपुट त्रुटि:

> INFO: WebModule[null] ServletContext.log(): The server side 
> component of the HTTP Monitor has detected a 
> java.lang.StackOverflowError. This happens when there is an infinite 
> loop in the web module. Correct the cause of the infinite loop before 
> running the web module again. 
> 
> INFO: The server side component of the HTTP Monitor has detected a 
> java.lang.StackOverflowError. This happens when there is an infinite 
> loop in the web module. Correct the cause of the infinite loop before 
> running the web module again. WARNING: 
> StandardWrapperValve[AjaxServlet]: Servlet.service() for servlet 
> AjaxServlet threw exception java.lang.StackOverflowError 

> WARNING: StandardWrapperValve[AjaxServlet]: Servlet.service() for 
> servlet AjaxServlet threw exception java.lang.StackOverflowError at 
> sun.util.calendar.ZoneInfo.getOffsets(ZoneInfo.java:248) at 
> java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2276) 
> at 
> java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2248) 
> at java.util.Calendar.setTimeInMillis(Calendar.java:1140) at 
> java.util.Calendar.setTime(Calendar.java:1106) at 
> java.text.SimpleDateFormat.format(SimpleDateFormat.java:955) at 
> java.text.SimpleDateFormat.format(SimpleDateFormat.java:948) at 
> java.text.DateFormat.format(DateFormat.java:336) at 
> com.google.gson.internal.bind.DateTypeAdapter.write(DateTypeAdapter.java:90) 
> at 
> com.google.gson.internal.bind.DateTypeAdapter.write(DateTypeAdapter.java:41) 
> at 
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195) 
> at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892) at 
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195) 
> at 
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195) 
> at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892) at 
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195) 
> at 
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89) 
> at 
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195) 
> at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892) 

मैंने यह भी देखा है कि इस निशान सिर्फ स्टैकट्रेस के उत्पादन में दोहरा कर रहे हैं;

संपादित करें: स्टाफ वर्ग

@Entity 
public class Staff implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "id") 
    private Integer id; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "last_name") 
    private String lastName; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "first_name") 
    private String firstName; 
    @Size(max = 45) 
    @Column(name = "middle_name") 
    private String middleName; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 6) 
    @Column(name = "gender") 
    private String gender; 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "date_of_birth") 
    @Temporal(TemporalType.DATE) 
    private Date dateOfBirth; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "nationality") 
    private String nationality; 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "date_hired") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateHired; 
    @Size(max = 20) 
    @Column(name = "status") 
    private String status; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "staff") 
    private Collection<StaffApointments> staffApointmentsCollection; 
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "staff") 
    private StaffContact staffContact; 
    @JoinColumn(name = "account_id", referencedColumnName = "id") 
    @ManyToOne(optional = false) 
    private Account accountId; 
    @JoinColumn(name = "position_id", referencedColumnName = "id") 
    @ManyToOne(optional = false) 
    private Position positionId; 

    public Staff() { 
    } 

    public Staff(Integer id) { 
     this.id = id; 
    } 

    public Staff(Integer id, String lastName, String firstName, String gender, Date dateOfBirth, String nationality, Date dateHired) { 
     this.id = id; 
     this.lastName = lastName; 
     this.firstName = firstName; 
     this.gender = gender; 
     this.dateOfBirth = dateOfBirth; 
     this.nationality = nationality; 
     this.dateHired = dateHired; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getMiddleName() { 
     return middleName; 
    } 

    public void setMiddleName(String middleName) { 
     this.middleName = middleName; 
    } 

    public String getGender() { 
     return gender; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 

    public Date getDateOfBirth() { 
     return dateOfBirth; 
    } 

    public void setDateOfBirth(Date dateOfBirth) { 
     this.dateOfBirth = dateOfBirth; 
    } 

    public String getNationality() { 
     return nationality; 
    } 

    public void setNationality(String nationality) { 
     this.nationality = nationality; 
    } 

    public Date getDateHired() { 
     return dateHired; 
    } 

    public void setDateHired(Date dateHired) { 
     this.dateHired = dateHired; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    @XmlTransient 
    public Collection<StaffApointments> getStaffApointmentsCollection() { 
     return staffApointmentsCollection; 
    } 

    public void setStaffApointmentsCollection(Collection<StaffApointments> staffApointmentsCollection) { 
     this.staffApointmentsCollection = staffApointmentsCollection; 
    } 

    public StaffContact getStaffContact() { 
     return staffContact; 
    } 

    public void setStaffContact(StaffContact staffContact) { 
     this.staffContact = staffContact; 
    } 

    public Account getAccountId() { 
     return accountId; 
    } 

    public void setAccountId(Account accountId) { 
     this.accountId = accountId; 
    } 

    public Position getPositionId() { 
     return positionId; 
    } 

    public void setPositionId(Position positionId) { 
     this.positionId = positionId; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Staff)) { 
      return false; 
     } 
     Staff other = (Staff) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entity.Staff[ id=" + id + " ]"; 
    } 

} 

यहाँ स्थिति वर्ग के रूप में अच्छी तरह से है:

@Entity 
public class Position implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "id") 
    private Integer id; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "name") 
    private String name; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "positionId") 
    private Collection<Staff> staffCollection; 

    public Position() { 
    } 

    public Position(Integer id) { 
     this.id = id; 
    } 

    public Position(Integer id, String name) { 
     this.id = id; 
     this.name = name; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @XmlTransient 
    public Collection<Staff> getStaffCollection() { 
     return staffCollection; 
    } 

    public void setStaffCollection(Collection<Staff> staffCollection) { 
     this.staffCollection = staffCollection; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Position)) { 
      return false; 
     } 
     Position other = (Position) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entity.Position[ id=" + id + " ]"; 
    } 

} 

संपादित करें 2:

मैं में विशेषताओं में @expose जोड़ा कर्मचारियों के चयन के अलावा मेरे स्टाफ वर्ग। लेकिन मुझे अभी भी 1 समस्या है। प्रत्येक बार ड्रॉपडाउन में पहला मान चुना जाता है (मान = 1) यह अभी भी अनंत लूप त्रुटि देता है। क्या कोई मेरी मदद कर सकता है?

संपादित करें 3:

यह तय! मैंने अंतिम जीसनबिल्डर बिल्डर = नया जीसनबिल्डर() जोड़ा; builder.excludeFieldsWithoutExposeAnnotation(); पूरा का पूरा। क्योंकि यह कभी नहीं वाला रोक अब यह काम करता है

+0

सर्वलेट जावा की तरह लगता है और आपकी त्रुटियां उस विचार को मान्य करने के लिए देखती हैं ... शायद जावा टैग का उपयोग करना सहायक होगा? –

+0

कच्चे प्रकार का उपयोग यहां 'संग्रह कर्मचारी चयन' के रूप में नहीं किया जाता है, फिर बिना किसी जांच के जेनेरिक 'सूची' में परिवर्तित किया जाता है। * शायद यह होगा यदि आपके पास गोलाकार संदर्भ है। * जिसके परिणामस्वरूप पार्सिंग के दौरान अनंत लूप में परिणाम होता है। 'Posiiton' और' Staff' कक्षाओं को भी साझा करें। – Braj

+0

@ ब्राज मेरे वर्ग डेटाबेस से इकाई कक्षाओं के रूप में उत्पन्न होते हैं। "कर्मचारी" तालिका "स्थिति" तालिका से इसकी position_id का संदर्भ देती है। मुझे क्षमा करें यदि मैं स्पष्ट रूप से आपको समझने में असमर्थ हूं, कर्मचारियों के संग्रह के संदर्भ में, आप क्या सुझाव देंगे? – MLDS

उत्तर

7

समस्या यह है कि हर Staff वस्तु एक स्थिति उद्देश्य यह है कि एक Staff की Collection वस्तुओं, जो प्रत्येक एक Staff की Collection वस्तुओं फिर से शामिल होता है होता है, आदि GSON इस पेड़ हमेशा के लिए चलने के लिए जारी रहेगा ।

इसे हल करने के लिए आप this प्रश्न के उत्तर पर एक नज़र डाल सकते हैं।

+0

मुझे पुनर्निर्देशित करने के लिए धन्यवाद। मुझे समझने में थोड़ा परेशानी है कि क्या पेश करना है। क्या मुझे स्टाफ क्लास में स्थिति स्थिति आईडी का पर्दाफाश करना चाहिए? यह अभी भी मुझे त्रुटि देता है। क्या आप स्पष्ट कर सकते हैं? – MLDS

+0

नहीं, आपको उन सभी तत्वों को बेनकाब करना चाहिए जिन्हें आप अपने JSON –

+0

में नहीं चाहते हैं, क्या यह मेरे सीआरयूडी परिचालन को प्रभावित करेगा? – MLDS

संबंधित मुद्दे