To get only the annotated properties use XmlAccessType.NONE.
Up vote 2 down vote favorite share g+ share fb share tw.
I have a java bean without any annotations. And I have a class inherited from this bean with JAXB annotations. Jersey (JAX-RS) serialize the second class to JSON.
And inherited properties occur in JSON twice: with name from XmlElement annotation and with 'camel-case' name of java-bean property. Here is a code which illustrates this: class MyBean { private Integer beanField; public Integer getBeanField() { return beanField; } public void setBeanField(Integer value) { this. BeanField = value; } } @XmlRootElement class AnnotatedBean extends MyBean { @Override @XmlElement(name="field") public Integer getBeanField() { return super.getBeanField(); } } } After serialization I get the next JSON: { "field" : 5, "beanField" : 5 } (while I want it to contain only one field with name field).
I investigated JAXB marshaller implementation and found that it marshals properties from all superclasses of the given class (and that means that it's impossible to get rid of the odd beanField property in my example). But I still hope that I could miss something. Is there a way to serialize only annotated properties?
Java jaxb jersey jax-rs link|improve this question asked Mar 10 '11 at 11:48Roman13.5k13093 74% accept rate.
To get only the annotated properties use XmlAccessType. NONE: @XmlAccessorType(XmlAcessType. NONE) @XmlRootElement class AnnotatedBean extends MyBean { ... } UPDATE Mapping the 3rd Party Class Using Externalized Metadata You could use the external metadata extension in EclipseLink JAXB (MOXy), I'm the tech lead.
It allows you to provide metadata for 3rd party classes. For this example the metadata will look like: To use MOXy you need to add a file named jaxb. Properties in with your model classes with the following entry: javax.xml.bind.context.
Factory=org.eclipse.persistence.jaxb. JAXBContextFactory The following article has instructions on configuring MOXy to work with Jersery: http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-35.html Context Resolver - Leveraging the Metadata You would need to use a ContextResolver to get your JAXBContext to leverage the external bindings file. The metadata is specified through a property when the JAXBContext is instantiated: import java.io.
InputStream; import java.util. HashMap; import java.util. Map; import javax.ws.rs.
Produces; import javax.ws.rs.ext. ContextResolver; import javax.ws.rs.ext. Provider; import javax.xml.bind.
JAXBContext; import javax.xml.transform. Source; import javax.xml.transform.stream. StreamSource; import org.eclipse.persistence.jaxb.
JAXBContextFactory; @Provider @Produces({"application/xml", "application/json"}) public class AnnotatedBeanContextResolver implements ContextResolver { private JAXBContext jaxbContext; public PurchaseOrderContextResolver() { try { Map properties = new HashMap(1); properties. Put(JAXBContextFactory. ECLIPSELINK_OXM_XML_KEY, new File("src/blog/bindingfile/binding.
Xml")); jaxbContext = JAXBContext. NewInstance(new Class {AnnotatedBean. Class}, properties); } catch(Exception e) { throw new RuntimeException(e); } } public JAXBContext getContext(Class clazz) { if(AnnotatedBean.
Class == clazz) { return jaxbContext; } return null; } }.
I've already tried this option. Unfortunately it didn't help. – Roman Mar 10 '11 at 13:23 @Roman - Can you annotate the super type?
– Blaise Doughan Mar 10 '11 at 13:27 no, it's a 3rd-party code. – Roman Mar 10 '11 at 13:33.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.