= jaxbcontext. newinstance (person. Class );// The following code shows how to convert an object to XMLExtends aller M = context. createmarshaller ();Address = new address ("China", "Beijing", "Beijing", "Shangdi West", "100080 ");Person P = new person (calendar. getinstance (), "jaxb2", address, gender. Male, "SW ");Filewriter fw = new filewriter ("person. xml ");M. Marshal (p, Fw );
// The following Code demonstrates converting the XML generated above to an objectFilereader Fr = new filereade
In the previous section.
We can see that netbeans saves a lot of time by automatically generating JPA entities. "Don't use wizard code you don't understand." --- Andrew hunt and Dave Thomas
Let's take a look at the generated entity code. You can understand it yourself.
Listing 1. examining a generated entity
package com.ensode.petclinicjavaee.entity;//imports omitted for brevity@Entity@Table(name = "owners", catalog = "petclinic", schema = "")@XmlRootEleme
corresponding Java objects; perform operations on these objects (modify, add, and delete object attributes), and save the content of these objects to this XML document.
package com.test;import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; public class User { private int id; private String name; @XmlAttribute (name = "id") public int getId() { return id; } public void setId(int id) { this.id = id;
About JAXB:1.JAXB is able to use Jackson's support implementation for JAXB annotations (jackson-module-jaxb-annotations), which facilitates the generation of XML and facilitates the generation of JSON, which can better flag Java classes that can be converted to JSON objects. JAXB allows Java people to map Java classes to XML representations, common annotations include: @XmlRootElement, @XmlElement, and so on.2.JAXB (Java Architecture for XML Binding)
();Book books = new book ();Customer.getinventoryreturn (). Add (books);To convert a Java object to an XML stringString xmlstr = converttoxml (customer);System.out.println (XMLSTR);The custom name space and later able inherit the NamespaceprefixmapperJaxbcontext context = Jaxbcontext.newinstance (Customer.getclass ());Marshaller m = Context.createmarshaller ();Namespaceprefixmapper mapper = new Preferredmapper ();M.setproperty ("Com.sun.xml.bind.namespacePrefixMapper", mapper);M.setproperty (Ma
b Y default. Annotations themselves use @controller and @responsebody annotations. Classes that use this annotation will be treated as a controller-with a @requestmapping method with a default @responsebody annotation. @ResponseBody – ALS of version 4.0 this annotation can also being added on the type level in which case is inherited and Does not need to being added on the method level. @ResponseBody can also be added to the class level, which is not required by the Inheritance method level.
Employeeservice.getbyid (1);}Add @xmlrootelement annotations to the entityThe results are as follows:We found that parsing into XML.Why parse it into XML instead of parsing it into JSON?As previously analyzed, message converters are determined by class and mediatype.We use Firebug to see:We found that the accept has XML, there is no JSON. So it's parsed into XML.Let's verify that the same address, HTTP header differs from accept. See if it's right.$.
in Java, there are many ways to convert an object into an XML file, and with dom4j, you can serialize to XML, and with some tool classes in the Simpleframework framework, you can simply sequence objects into XML files. Interested programs apes can search the web for concrete methods, and let's look at how the tools in the JDK (1.5) are implemented to convert an object into XML form. Package Javatoxml;import Java.io.stringwriter;import Javax.xml.bind.jaxbcontext;import Javax.xml.bind.jaxbexceptio
Sometimes we want to use reflection to get a comment on a class, a comment on a method, a comment on a property.The following is a simple example. It includes the three points mentioned above.Package Com.mine.practice.reflectfield;import Java.lang.annotation.annotation;import Java.lang.reflect.Field; Import Java.lang.reflect.method;import Javax.xml.bind.annotation.xmlaccesstype;import Javax.xml.bind.annotation.xmlaccessortype;import Javax.xml.bind.annotation.xmlattribute;import Javax.xml.bind.an
jaxb2rootelementhttpmessageconverter
xmlrootelement,xmltype (annotations)
...
mappingjackson2httpmessageconverter
Json
In addition to the first five, other converters will be tried by ClassLoader to load a class to determine whether the project contains a package, and then decide whether to join the converter collection.The extractor traverses the converter collectio
The object boy described earlier contains only simple attributes, and what to do if the boy contains other objects.
@XmlRootElement (name= "Root")
@XmlAccessorType (xmlaccesstype.property) public
class Boy {
private String name = "AA";
private address address;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
@XmlJavaTypeAdapter (addressadapter.class) public address
getaddress () {retu
JAXB (Java architecture for XML Binding) is an industry standard and is a technology that can generate Java classes from XML schemas.
You can allow Java objects and XML documents to be converted to each other.
Let's say there's a pojo.
Package com.xyz;Import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Boy{String name = "John";Public String GetName (){return name;}public void SetName (String name){THIS.name = name;}}
The annotation @x
JavaBean.
Introduction to JAXB Technology
JAXB (Java architecture for XML Binding) is an industry standard and is a technology that can generate Java classes from XML schemas. In this process, JAXB also provides a way to reverse-generate JavaBean XML instance documents and to write the contents of JavaBean to an XML instance document. On the other hand, JAXB provides a quick and easy way to bind XML schemas to Java representations, making it easy for Java developers to combine XML data with p
maps to Application/json
Note: Contentnegotiatingviewresolver is based on customer submitted mimetype (such as Text/html,application/xml) to the service side of a group of Viewresover mimetype comparison, Returns the Viewresover data if it is met.And/user/123.xml, Contentnegotiatingviewresolver will first convert the. xml to Application/xml according to the MediaTypes attribute, and then complete the comparison described earlier.
Using content negotiation to implement multiple view legends
Spring can easily integrate a variety of restful styles of web-service, but in fact SPRING-MVC has provided this support. You can generate XML, JSON, text, and other formats as needed. Generating XML relies on JAXB, mainly @xmlrootelement, @XmlAttribute, @XmlElement and other annotations, JDK1.6 began with JAXB. Generating JSON requires Jackson, who relies on Jackson-mapper-asl-xxx.jar, Jackson-core-asl-xxx.jar. MAVEN relies on the following, JACKSON-
;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.stringwriter;import Javax.xml.bind.jaxbcontext;import Javax.xml.bind.jaxbexception;import Javax.xml.bind.marshaller;import Org.dom4j.document;import Org.dom4j.documentexception;import Org.dom4j.documenthelper;import Org.dom4j.node;public class Xmlutil {private static String Noresult = "A use case for Xmlutil is as follows:/** * @Description: Chapter Li
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.