1
votes

How to achieve that some properties are ignored in the JSON-output (like XmlTransient for XML-output)?

@GET
@Path("/{companyId}")   
@Produces(MediaType.APPLICATION_JSON)
public PortfolioCompany getCompany(@PathParam("companyId") long id);

I've been playing around with the RESTeasy-support in Seam 2.3 deployed as an EAR on a JBoss 7.1. I started by adding the same dependencies to my ejb-project as in the Seam-restbay-example. It is basically working fine for @Produces(MediaType.APPLICATION_XML), where all properties annotated with @XmlTransient are ignored, in order to prevent some LazyInitialisationExceptions. But how to achieve this behavior for @Produces(MediaType.APPLICATION_JSON)?

I've read Seam uses Jettison by default, which uses the @XmlTransient annotation for both, XML and JSON (because technically it transforms from XML -> to JSON). But I get a "Caused by: org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer" which indicates that in fact it is using Jackson..? For Jackson there is the annotation like @JsonIgnore, but having the same maven dependencies like restbay - this "cannot to be resolved to a type".

/**
 * @return the contact
 */
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@XmlTransient  // working for produces XML but not for JSON
//@JsonIgnore  = unknown type
public Contact getContact() {
return contact;
}

Anyone any experiences or hints on that?

thanks

EDIT: Really no one having the need of realizing lazy collections for REST-services with Seam??

After some research:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property ="@id")

is what apparently would be needed. But that is only provided with Jackson 2.x. But the seam2.3/jboss7 setup is obviously using Jackson 1.9...

1

1 Answers

0
votes

The default annotation mode of @XmlType/@XmlRootElement is to capture everyfield, including the lazy initializer from hibernate. Use @XmlAccessorType(XmlAccessType.NONE) in your entities and then individually annotate the fiels with @XmlElement so that only the necessary fields are processeded