I've been using Metro and have come across a problem whereby wsgen seems to ignore any JAXB annotation that I put on web method response classes.
I'm trying to return a set of entity classes that I want to expose to external clients via a web service, however I only want to make certain class attributes and methods visible to external clients, so have set the class access type to be PROPERTY, and have annotated all attributes I want to be part of the XML version of the class with #XmlElement.
I can create the XML schema for the entity class using the schemagen ant task, which correctly ignores any attribute or method that I have not annotated with #XmlElement, however when I run wsgen to create the wsdl for the web service which has a method which returns a set of the entity class, then I get a java.lang.NoClassDefFoundError. This appears to be because wsgen is not ignoring a public method on the entity class that it should do, with the class referenced by the exception being a parameter to this method.
I've tried to annotate the method with #XmlTransient so that wsgen ignores it, but to no avail. If anyone can suggest what I should do to get wsgen to pay attention to my annotations then that would be great.
An annotated field/property will always be processed regardless of accessor type. You need to specify #XmlAccessorType(XmlAccessType.NONE) if you only want annotated fields/properties to be treated as mapped.
For More Information
http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html
Related
I read that every POJO used in Groovy gets a MetaClass associated with it. These meta-classes are stored in the application wide metaclass registry. How are these meta-classes generated and placed in the meta-class registry? If each POJO gets its own meta-class, is there some sort of template that is used to generate these meta-classes?
I read that every POJO used in Groovy gets a MetaClass associated with
it.
FYI... It isn't just POJO. Every class has a meta class associate with it.
These meta-classes are stored in the application wide metaclass
registry.
That registry is represented in groovy.lang.MetaClassRegistry. You can get a reference to the registry from GroovySystem.html#getMetaClassRegistry().
How are these meta-classes generated and placed in the meta-class
registry?
There is no one way. You can write your own custom meta class and register it, Groovy registers default meta classes, you could replace a metaClass in the registry momentarily (while a unit test runs, for example) and then replace it with the original one after.
At runtime you could use methods like MetaClassRegistry.html#getMetaClass(java.lang.Class), MetaClassRegistry.html#setMetaClass(java.lang.Class,groovy.lang.MetaClass), and MetaClassRegistry.html#removeMetaClass(java.lang.Class) to manipulate the registry.
If each POJO gets its own meta-class, is there some sort of template
that is used to generate these meta-classes?
docs.groovy-lang.org/latest/html/api/groovy/lang/MetaClass is the closest thing to a template but aside from satisfying standard compile time restrictions of having to implement all the abstract methods, there could be any arbitrary logic in a meta class one might write.
i am using jaxb to generate code from an xsd.
The generated code contains a lot of annotations; for classes and fields.
I am trying to use com.sun.tools.internal.xjc.Plugin to modify the generated code.
In the plugin run() method we are given an Outline class from which we can get ClassOutline. ClassOutline has an JDefinedClass final member which has the info about actual class which will be generated.
If i want to add anything, there are apis in JDefinedClass which can be used. But if i want to remove something, there is no way.
e.g. i cannot clear annotations, because the JDefinedClass.annotations() method returns an UnmodifiableCollection. so i cannot clear it or remove anything from it.
i tried to create another JDefinedClass by invoking the _class method but the ClassOutline.implClass variable is final, so i cannot set it.
how to get a JDefinedClass which does not have any annotations?
is there another phase of code generation which i can trap into to really control the generation of JDefinedClass?
The code model is, indeed mostly "write only". But, speaking of annotations, you have probably missed the methods like com.sun.codemodel.JDefinedClass.removeAnnotation(JAnnotationUse) and com.sun.codemodel.JMethod.removeAnnotation(JAnnotationUse) (implemented from com.sun.codemodel.JAnnotatable.removeAnnotation(JAnnotationUse)).
So they're there. You can remove annotations with the normal CodeModel API.
As I can see, you can also remove fields and methods from classes. So what exactly are you missing?
JDefinedClass.annotations() It return an unmodifiable collection object and you cannot modify them.
So work around for this, you can restrict annotation addition/deletion at class and field level before building JCodeModel.
You need to create a custom Jackson2Annotator class which extends Jackson2Annotator and override their methods according to your requirement.
Following are few methods which are being used for specific type of annotation property:
propertyOrder(OTB JsonPropertyOrder)
propertyInclusion(OTB JsonInclude)
propertyField(can be used for custom defined annotation at field level)
More you can discover by looking Jackson2Annotator class what fit into your need.
I'm using Simperium and mogenerator in my project. I've added SPManagedObject entity to the model and set that as the parent class for other entities. For each entity, mogenerator automatically creates a _<entityname>.h and _<entityname>.m file that has several convenience accessors. I've modified the mogenerator build script so each of these subclasses SPManagedObject instead of NSManagedObject. The entity classes simply import the corresponding _<entityname>.h file.
My problem is I'm getting runtime errors when using any of the convenience accessors. For example, for an entity with attribute of type Integer32 called myInteger, the class file property is NSNumber. Mogenerator creates an accessor named myIntegerValue. The errors I'm getting are that myIntegerValue is an unrecognized selector for SPManagedObject. I can work around this obviously but it seems to be indicative of a bigger problem. Any ideas? Thanks!
This kind of problem is most often the result of failing to configure the class name in the Core Data model editor. You may have a class Foo and an entity Foo but they don't automatically go together (they're not required to have the same name). If you don't do this, you get instances of NSManagedObject instead of instances of your subclass. And of course, NSManagedObject doesn't have those methods.
Select the entity in the model editor and look in the model inspector on the right. Make sure that the class name is configured.
I wanted to know why do we need to specify the Annotation #XmlAccessorType when working with JAXB .
When i googled for this i found out this description from a website stating this
#XmlAccessorType sets default field and property serializability. By default, JAXB serializes public fields and properties. By setting #XmlAccessorType, the bean can choose to only allow annotated fields to be serialized.
Here the author mentions that with this annotation it gives control on serialization .
My question is , so #XmlAccessorType has nothing to do with the JAXB Binding and Unbinding from XML to java and java to XML , and it is all about Serialization only .
JAXB's #XmlAccessorType annotation is only used by JAXB (JSR-222) implementations for determining how to marshal a file to/from XML:
Normally the main decision to be made is between FIELD & PROPERTY/PUBLIC. FIELD is particularly useful when you have logic in your get/set methods that you do not want triggered during marshalling/unmarshalling. To see one way this choice affects the mapping metadata see:
http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html
NONE is a useful choice when you have many unmapped properties and you want to tell your JAXB implementation to only map the fields/properties you have annotated. This can be alot easier than adding a lot of #XmlTransient annotations into your model.
Fore More Information
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
I need to send/receive polymorphic objects from/to server while invoking my spring enabled restful service.
Please let me know if xmlseealso annotation can be useful even when there are more than 1 subclass in the class hierarchy. I'm askign this because, even though i pass 2 to 3 subclasses inside xmlseealso annotation, only the last subclass is considered when unmarshalling takes place.
like when the parent model is as below,
#XmlSeeAlso({RateFix.class, EarlyRedemption.class})
public class Instruction implements Serializable
jaxb2 always unmarshall the Instruction to EarlyRedemption model while it actually refers to RateFix.
I'm using jaxb 1.0 jars only where i can find the xmlseealso annotation
Please share me your view. Thanks.