We often have needs, such as invoking other people's interfaces, others will return to your JSON or XML format data, and then we have to convert to the corresponding object for our own use;
Sometimes it is someone else's tune our interface, let us provide XML format data or JSON format data, JSON words here do not say, very good processing, even SPRINGMVC itself is easy to support.
The following describes only XML and object conversions
First we need to introduce the jar package: Xstream-1.4.8.jar
Directly below the code, the code is written in the SPRINGMVC environment
@Controller Public class navigationappdatacontroller extends basecontroller{ @SuppressWarnings("Unused")Private Static FinalLogger log = Loggerfactory.getlogger (Navigationappdatacontroller.class);@Autowired PrivateStonavigationappmanager Stonavigationappmanager;@RequestMapping(Value ="/appsite/getnavigationxmldata.htm") Public void Getnavigationxmldata(HttpServletRequest request,httpservletresponse response) {list<navigationappcommand> Findstorenavigationapptoxml = Stonavigationappmanager.findstorenavigationappdat A (); Xstreamconfig Xstreamconfig =NewXstreamconfig ();//What does this mean? I'll describe it in the following text, which is about renaming nodes in XMLXstreamconfig.getaliasmap (). Put ("Topcategories", List.class); Xstreamconfig.getaliasmap (). Put ("category", Navigationappcommand.class); String XML = ToXML (Findstorenavigationapptoxml, xstreamconfig);//Set the encoding format of the returned dataResponse.setcharacterencoding (Charsettype.utf8);//Set content type, decide what type of browser will read the data we return to showResponse.setcontenttype ("Application/xml");//Writes data to response and returns it to the requesting sideResponse.getwriter (). write (XML); }/** * Converts a Bean object into an XML-formatted string based on the xstreamconfig format returns * * @param Bean * Object * @param xstreamconfig * Configuration * @return * * Public StaticStringToXML(Object bean,xstreamconfig Xstreamconfig) {XStream XStream = Constructxstream (xstreamconfig);returnXstream.toxml (Bean); }/** * Convert XML string to corresponding object * * @param XML * @return */ Public Static<T> TFromXml(String XML) {returnFromXml (XML,NULL); } Public Static<T> TFromXml(String xml,xstreamconfig Xstreamconfig) {XStream XStream = Constructxstream (xstreamconfig);return(T) xstream.fromxml (XML); } Public StaticXStreamConstructxstream(Xstreamconfig xstreamconfig) {XStream XStream =NewXStream ();if(Validator.isnotnullorempty (Xstreamconfig)) {//*******************alias********************************************map<string, class<?>> aliasmap = Xstreamconfig.getaliasmap ();if(Validator.isnotnullorempty (Aliasmap)) { for(Map.entry<string, Class<?>> entry:aliasMap.entrySet ()) {String key = Entry.getkey (); class<?> value = Entry.getvalue ();//class renameXstream.alias (key, value); } }//*******************implicitcollectionmap********************************************map<string, class<?>> implicitcollectionmap = Xstreamconfig.getimplicitcollectionmap ();if(Validator.isnotnullorempty (Implicitcollectionmap)) { for(Map.entry<string, Class<?>> entry:implicitCollectionMap.entrySet ()) {String key = Entry.getkey (); class<?> value = Entry.getvalue (); Xstream.addimplicitcollection (value, key); } } } }}
/** * XStream related configuration. * */ Public Final class xstreamconfig{ /** Aliases. * / Privatemap<string, class<?>> aliasmap =NewHashmap<string, class<?>> ();/** Implicit collection hidden, hidden, such as the following list, the second parameter in the generic Class is ownertype. * / Privatemap<string, class<?>> implicitcollectionmap =NewHashmap<string, class<?>> ();/** * get aliases. * * @return the aliasmap * * PublicMap<string, class<?>>Getaliasmap(){returnAliasmap; }/** * Set aliases. * * @param Aliasmap * The Aliasmap to set */ Public void Setaliasmap(map<string, class<?>> aliasmap) { This. Aliasmap = Aliasmap; }/** * Get implicit collection hidden, hidden, such as a list below, the second parameter in the generic Class is ownertype. * * @return the implicitcollectionmap * * PublicMap<string, class<?>>Getimplicitcollectionmap(){returnImplicitcollectionmap; }/** * Set implicit collection hidden, hidden, such as the following list, the second parameter in the generic Class is ownertype. * * @param Implicitcollectionmap * The Implicitcollectionmap to set */ Public void Setimplicitcollectionmap(map<string, class<?>> implicitcollectionmap) { This. Implicitcollectionmap = Implicitcollectionmap; }}
The implementation method is very simple. Here is the meaning of several parameter settings for response.
-
Response.setcharacterencoding (Charsettype.utf8);
Sets the encoding format of the returned data if this is not set, the data returned to the client's requester may be garbled and the encoding format must be unified
-
Response.setcontenttype ("Application/xml");
Set the content type, decide what type of browser will read our returned data to show, if not set, then the browser display is a large block of XML format of the string, without any effect, and after setting it can be like I above, the node can click Expand and Close, and the data and node color contrast is obvious, Easy to watch.
Using the XStream implementation in Java to convert an object to an XML format string or an XML-formatted string to an object = =