Use Xfire and Xstream to jointly build a java development solution that integrates its own business system webservice services

1. Xfire deployment and construction
  Using Xfire to build a set of java webservice service interface is very easy. Now xfire has stopped development and was merged into CXF project by apache. We download the Xfire project to use the jar package and throw it into the buildpath of the project. The core jars include the following:
 
 
We create a new folder META-INF in the src directory, and then create a word folder xfire of it, and create the file services.xml in it. The structure after that is as follows:
 

 
Let's take a look directly at services.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
     <service>
          <!-- webserviceq name, required when calling Specify this -->
          <name>readerService</name>
          <!-- This is usually the website of your own company, which is of little significance -->
          <namespace>http://test/HelloService</namespace>
          <!-- Interface Class -->
          <serviceClass>com.xfire.servlet.IReaderService</serviceClass>
          <!-- Implementation class -->
          <implementationClass>com.xfire.servlet.ReaderService</implementationClass>
     </service>
</beans>
 
Many people think that this is enough. No, not yet. If you specify this, how can others access it. How to forward the corresponding request to xfire and let it process it. Here you need to modify web.xml again.
Modified as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     id="WebApp_ID" version="3.0">
     <servlet>
          <servlet-name>XFireServlet</servlet-name>
          <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
     </servlet>

     <servlet-mapping>
          <servlet-name>XFireServlet</servlet-name>
          <url-pattern>/services/*</url-pattern>
     </servlet-mapping>
</web-app>
 
In fact, it is to add a servlet and the corresponding mapping. Next, we type directly on the browser:
 
Second, use Xstream to convert java objects and XML formats
  In the projects I have experienced, webservice is generally used for data interaction with third-party manufacturers, so it is very important to adopt XML-based interface operation specification documents when formulating system docking. Whether we publish the webservice service interface or call other people's webservice interface as a client, when integrating with our own original system, we inevitably need to convert XML and POJO objects into each other to facilitate development. At this time, we can use the XML parser to parse the XML and recreate the java object, and vice versa. But this code is not economical, we need to traverse and retrieve the XML to convert it into a qualified java object. Therefore, Xstream has done such repetitive work for us, and we can easily convert the two into each other just by using it correctly.
 
  Xstream is an OXMapping technology, which is a framework for processing XML file serialization. When serializing JavaBeans or deserializing XML files, other auxiliary classes and mapping files are not required, so that XML serialization is no longer complicated. Sow. Xstream can also serialize or deserialize JavaBeans into Json, which is very convenient to use.
 
  No additional configuration is required in the configuration file, just import the jar package into the project. The jar package I use is:
 
 Xstream provides many functional interfaces to customize the conversion needs. We focus on the functions of the following interfaces:
First of all, our code examples use such javabeans to explain:
package test.xstream.test;
public class Author {
    private String name;
    public Author(String name) {
            this.name = name;
    }
    public String getName() {
            return name;
    }
}
 
1. xStream.alias(String, Class), function: replace the full name of the class in serialization with an alias. If the interface is not called explicitly, the XML tag will use the full class path of the javabean.
 
The xml serialized without using the alias alias:
<test.xstream.test.Author>
  <name>name</name>
</test.xstream.test.Author>
 
The xml serialized when using the alias alias:
<Author>
  <name>name</name>
</Author>
How to use: xstream.alias(" Author ", Author .class)
 
2. xstream.aliasField (String, Class, String), role: use alias instead of attribute name.
The xml serialized without using the alias aliasField:
<Author>
  <name>name</name>
</Author>
 
The xml serialized when using the alias aliasField:
<Author>
  <author>name</author>
</Author>
How to use: xstream.aliasFiel ("author", Author.class , "name"), in fact, the attribute " name " in the class specifies the alias "author" for input or output.
 
3. xstream.registerLocalConverter (Class definedIn, String fieldName, Converter converter), role: custom converter, which can transfer the attributes of the specified class (this attribute is generally of type List) to a specific converter for processing.
Code description:
xstream.registerLocalConverter( AppBsChangeResult.class, "entityIDs", new ListConverter("accID"))
 
public class AppBsChangeResult extends AppBsBaseDriverResult {    
    private static Logger logger = Logger.getLogger(AppBsChangeResult.class);
private List<String> entityIDs = null;
public List<String> getEntityIDs() {
return entityIDs;
}
public void setEntityIDs(List<String> entityIDs) {
this.entityIDs = entityIDs;
}
}
 
public class ListConverter implements Converter {    
    public ListConverter(String tagName){
        this.tagName = tagName;
    }    
    private String tagName = null;    
    public String getTagName() {
        return tagName;
    }
    public void setTagName(String tagName) {
        this.tagName = tagName;
    }
    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext arg2) {
        List<String> list = (List<String>) value;
        for (String subval : list) {
           // In order to convert to XML, ensure that there are all <expands></expands> tags, and only output "" without writing tags when the value is empty
            if("".equals(subval) && list.size()==1){
                writer.setValue(subval);
            }else{
                writer.startNode(tagName);
                writer.setValue(subval);
                writer.endNode();
            }
        } 
    }
 
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        List<String> list = new ArrayList<String>();
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String key = reader.getNodeName();
            String value = null;
            if (reader.hasMoreChildren()) {
                value = (String)unmarshal(reader, context);
            } else {
                value = reader.getValue();
            }
                // insert into list
            list.add(value);
            reader.moveUp();
        }
        return list;
    }
 
    //Override the method of the parent class to indicate which java type can be processed by the converter
    public boolean canConvert(Class type) {
        return type.equals(ArrayList.class); 
    }
}
 
After executing xstream.registerLocalConverter ( AppBsChangeResult.class, "entityIDs", new ListConverter("accID")), the values ​​of the N accID tags in the XML can be converted into the attribute "entityIDs" in the AppBsChangeResult class, because the attribute "entityIDs" is A List<String>, so you can add the values ​​of N accIDs into this List. Note that the abstract interface marshal of the converter is generally used in the process of converting javabean objects to XML (serialization), while the abstract interface unmarshal is used in the process of converting XML to javabean objects (deserialization).
 
4. xstream.addDefaultImplementation (Class, Class), role: use abstract classes, or conversion of parent and child classes
If an abstract class or a parent class is used as a class attribute, when the object is converted into xml, the class attribute will be added to the node corresponding to the abstract attribute, and the value is the package path of the subclass.
How to remove the class attribute:
xStream.addDefaultImplementation(Sun.class,Parent.class)
 
If an abstract class or a parent class is used as a class attribute, when converting an xml string into an object, if the corresponding node has a class attribute, there is no problem in the conversion. If the class attribute in the xml string is removed, when converting an object, the An exception will be thrown: java.lang.InstantiationError
The solution is also:
xStream.addDefaultImplementation(Sun.class,Parent.class)
 
5. xstream.omitField (Class, String), function: Define the value of a certain attribute without xml serialization, that is, the specified field will not be processed. This field is hidden when serializing and null when deserializing
 
6、xstream.useAttributeFor(Class, String),xstream.useAttributeFor(String, Class)和xstream.useAttributeFor(Class)
  1. xstream.useAttributeFor(String.class); All fields of String type are defined as attribute tag display
  2. xstream.useAttributeFor("name", String.class); All String type field names become name and are defined as attribute tag display
  3. xstream.useAttributeFor(Person.class, "name"); Set the field node as an attribute and use the above method alone
 <test.Person>
  <name>张三</name>
  <age>19</age>
  <string>Li Si</string>
  <string>王五</string>
  <string>赵六</string>
</test.Person>
Will output in the following XML format:
<test.Person name="张三">
  <age>19</age>
  <string>Li Si</string>
  <string>王五</string>
  <string>赵六</string>
</test.Person>
7. xstream.addImplicitCollection ( Class, String), role: omit the collection root node
public class Person {
private String name;
private int age;
private List friends;
 
public Person(String name, int age, String... friends) {
this.name = name;
this.age = age;
this.friends = Arrays.asList(friends);
}
}
 
public class XstreamTest {
public static void main(String[] args) {
Person bean = new Person("Zhang San", 19, "Li Si", "Wang Wu", "Zhao Liu");
XStream xStream = new XStream(new DomDriver());
xStream.addImplicitCollection(Person.class, "friends");
//Serialization
String xml = xStream.toXML(bean);
System.out.println(xml);
// deserialize
bean=(Person)xStream.fromXML(xml);
System.out.println(bean);
}
}
The XML comparison before and after xStream.addImplicitCollection(Person.class, "friends") is as follows.
before use:
<test.Person>
  <name>张三</name>
  <age>19</age>
  <friends class="java.util.Arrays$ArrayList">
    <a class="string-array">
      <string>Li Si</string>
      <string>王五</string>
      <string>赵六</string>
    </a>
  </friends>
</test.Person>
After use:
<test.Person>
  <name>张三</name>
  <age>19</age>
  <a class="string-array">
    <string>Li Si</string>
    <string>王五</string>
    <string>赵六</string>
  </a>
</test.Person>
 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327057882&siteId=291194637