CXF WebService object property value is null or empty when generating XML missing the field node workaround

Source: Internet
Author: User
Tags bind

Recently doing an Android project, need to use the webservice, in the use of CXF development WebService found a problem, such as I now have a building object, where the Name property value may be null or empty, Found when an XML file is generated by the building object

When the Name property value is null: The generated XML will be missing <name></name> this node, and the Android client will report null pointer error when parsing XML;

When the Name property value is "" "Empty: the name node in the generated XML is like this <name/> this form is not what I want, because Android parsing the name will get the form of name=anytype{}, which is obviously wrong. The format we need should be <name></name> in this form.

On the internet to look at a lot of information that the default XML processing is like this, we need to rewrite the XML processing, and later on the Internet to see this article solves my problem, please refer to

http://www.boyunjian.com/do/article/snapshot.do?uid=2819102595941137602

Http://www.cnblogs.com/fragranting/archive/2012/03/25/xml--jaxb.html

The main point is to use the JAX-WS data Map to provide a mapping between the XML schema and Java, where I use the Javax.xml.bind.annotation class,

1. We first need to write a class that inherits XMLAdapter abstract classes:

As follows: Here I put a value of NULL or "" empty data Into "" (space output) so that we can generate the XML we need

Package com.hbmop.app.util;

Import Javax.xml.bind.annotation.adapters.XmlAdapter;

public class DataAdapter extends Xmladapter<object, object>{
	//java→xml processing, OB is the attribute value passed in when Java generates XML, OB can be an array, Other complex types, such as collections,
	//Can be processed in this method, return returns the desired result type
	@Override public
	Object Marshal (object ob) throws Exception {
		
		if (ob = = NULL | | ob.equals (")") {
			ob= "";
		}
		return ob;
	}

	Xml→java when handling
	@Override public
	Object Unmarshal (Object arg0) throws Exception {
		//TODO auto-generated Method stub
		return arg0;
	}

}
2. Using annotations based on the J Avax.xml.bind.annotation class

@XmlElement options include: Required: Whether the element node must exist. For example, minOccurs is not equal to 1. Nillable: Whether the field contains nillable= "true" property, if Nillable () is true, then the JavaBean attribute is mapped to an XML schema Nillable element declaration

</pre><p><pre name= "code" class= "Java" > @XmlAccessorType (Xmlaccesstype.field)  
@XmlType (name = "Building") public
class Building {
	@XmlJavaTypeAdapter (value = dataadapter.class)
	@XmlElement ( required=true,nillable=true)
   private String name;
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}}

With these two steps, we can implement the XML format we want.

The main annotation

Xmlaccessororder controls the ordering of fields and properties in the class.
xmlaccessortype Controls whether fields or Javabean properties are serialized by default.
XmlAttribute Maps the JavaBean property to an XML property.
XmlElement Maps the JavaBean property to an XML element that is derived from the property name.
Xmlelementwrapper generates a wrapper element that wraps an XML representation.
XmlList is used to map properties to list simple types.
The Xmlmimetype Association controls the MIME type of the XML representation of the property.
Xmlrootelement Maps a class or enumeration type to an XML element.
xmltransient prevents the JavaBean property from being mapped to an XML representation.
XmlType Maps a class or enumeration type to an XML schema type.
Xmlvalue supports mapping classes to XML Schema complex types with simplecontent or XML Schema simple types.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.