Bug 205394 - StackOverFlowError when generating RESTful representation using Entities
StackOverFlowError when generating RESTful representation using Entities
Status: RESOLVED FIXED
Product: webservices
Classification: Unclassified
Component: REST
7.1
PC Mac OS X
: P3 (vote)
: 7.2
Assigned To: Denis Anisimov
issues@webservices
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-11-22 00:36 UTC by arungupta
Modified: 2012-01-12 16:00 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description arungupta 2011-11-22 00:36:07 UTC
Create a new Web project
Generate RESTful entities for Customer database using "RESTful Web services from Database..."
Access the complete set of customers at:

http://localhost:8080/CustomerREST/restful/customers/

The default representation is "application/xml" and looks good.

Change the @Produces for 


    @GET
    @Override
    @Produces({"application/json", "application/xml"})
    public List<Customer> findAll() {
        return super.findAll();
    }


to


    @GET
    @Override
    @Produces({"application/json"})
    public List<Customer> findAll() {
        return super.findAll();
    }


Access the list again and a StackOverFlow error is thrown as:

org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: entities.MicroMarket["customerCollection"]->org.eclipse.persistence.indirection.IndirectList[0]->entities.Customer["zip"]->entities.MicroMarket["customerCollection"]->org.eclipse.persistence.indirection.IndirectList[0]->entities.Customer["zip"]->entities.MicroMarket["customerCollection"]->org.eclipse.persistence.indirection.IndirectList[0]->entities.Customer["zip"]->entities.MicroMarket["customerCollection"]->org.eclipse.persistence.indirection.Indirect
Comment 1 David Konecny 2011-11-22 02:01:15 UTC
This sounds familiar - I'm been prototyping some JavaScript app and I think I bumped into the same problem. I cannot remember where the problem was - sounds like Java code generated does not indicate where to stop object traversal and marshalling? But if it works for XML then why JSON fails?
Comment 2 David Konecny 2011-11-22 02:01:58 UTC
Something wrong with BugZilla right now - it does not allow me to move the issue to webservices/rest category.
Comment 3 Denis Anisimov 2011-11-22 09:25:15 UTC
@org.codehaus.jackson.annotate.JsonIgnore annotation 
( http://jackson.codehaus.org/1.0.1/javadoc/org/codehaus/jackson/annotate/JsonIgnore.html ) similar to javax.xml.bind.annotation.XmlTransient.
The latter annotation is used to prevent recursive navigation over XML elements.
F.e. DiscountCode class has method getCustomerCollection() annotated with @XmlTransient.
That's why the problem is absent for XML mime type.
@JsonIgnore should be used at the same places where @XmlTransient.

It seems this issue is related newer Jersey libraries. I believe all works fine 
without @JsonIgnore annotation with old Jersey 1.3.
Comment 4 japod 2012-01-02 14:08:05 UTC
To avoid the necessity for adding @JsonIgnore everywhere
you might want to provide your customized Jackson object mapper.
Then Jackson should take @XmlTransient annotation into account
when serializing JSON.

Details are available at [1]. A complete example is available at [2],
look at the MyObjectMapperProvider class.

I believe such a provider could be auto-generated by NetBeans.

[1]http://wiki.fasterxml.com/JacksonJAXBAnnotations
[2]http://search.maven.org/remotecontent?filepath=com/sun/jersey/samples/jacksonjsonprovider/1.11/jacksonjsonprovider-1.11-project.zip
Comment 5 Denis Anisimov 2012-01-11 12:37:22 UTC
The generation process is automatic and already exist for XmlTransient.
So it is easier here to add just one more annotation in the process of generation 
instead of generate additional Mapper provider class.
web-main#1ec54cbb74df
Comment 6 Quality Engineering 2012-01-12 16:00:53 UTC
Integrated into 'main-golden'
Changeset: http://hg.netbeans.org/main-golden/rev/1ec54cbb74df
User: Denis Anisimov <ads@netbeans.org>
Log: Fix for BZ#205394 - StackOverFlowError when generating RESTful representation using Entities


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo