首页 > 代码库 > 玛莎--JAXB的marshell
玛莎--JAXB的marshell
玛莎,安玛莎这里个方法名字好feshion,有没有玛莎拉蒂的既视感。
什么是玛 莎marshalling?就是序列化 , translating data structures or object state into a format
实现很简单,pojo对象加注解@XmlRootElement,否则会报异常
[com.sun.istack.SAXException2: 由于类型 "com.happy.entities.ItemEvaluation" 缺少 @XmlRootElement 注释, 无法将该类型编集为元素]
看官网介绍:https://en.wikipedia.org/wiki/Serialization
In computer science, in the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment.[1] When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of object-oriented objectsdoes not include any of their associated methods with which they were previously inextricably linked.
This process of serializing an object is also called marshalling an object.[2] The opposite operation, extracting a data structure from a series of bytes, is deserialization (which is also called unmarshalling).
https://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding
Java Architecture for XML Binding (JAXB) is a software framework that allows Java developers to map Java classes to XMLrepresentations. JAXB provides two main features:
the ability to marshal Java objects into XML and the inverse,
i.e. to unmarshalXML back into Java objects.
In other words, JAXB allows storing and retrieving data in memory in any XML format, without the need to implement a specific set of XML loading and saving routines for the program‘s class structure. It is similar to xsd.exe
andXmlSerializer
in the .NET Framework.
JAXB is particularly useful when the specification is complex and changing. In such a case, regularly changing the XML Schemadefinitions to keep them synchronised with the Java definitions can be time consuming and error-prone.
JAXB is a part of the Java SE platform and one of the APIs in the Java EE platform, and is part of the Java Web Services Development Pack (JWSDP). It is also one of the foundations for WSIT.
JAXB 1.0 was developed under the Java Community Process as JSR 31.[1] In 2006 JAXB 2.0 was released under JSR 222 and Maintenance Release 2 released in December 2009.[2]
JAXB(Java Architecture for XML Binding)
JAXB是Java Architecture for XML Binding的缩写。提供了一个快捷的方式将
Java对象与XML进行转换。
在JAX-WS(Java的WebService规范之一)中,JDK1.6自带的版本JAX-WS2.1,其底层支持就是JAXB。
JavaJava provides automatic serialization which requires that the object be marked by implementing the java.io.Serializable
interface. Implementing the interface marks the class as "okay to serialize", and Java then handles serialization internally. There are no serialization methods defined on the Serializable
interface, but a serializable class can optionally define methods with certain special names and signatures that if defined, will be called as part of the serialization/deserialization process. The language also allows the developer to override the serialization process more thoroughly by implementing another interface, the Externalizable
interface, which includes two special methods that are used to save and restore the object‘s state. There are three primary reasons why objects are not serializable by default and must implement the Serializable
interface to access Java‘s serialization mechanism.
Firstly, not all objects capture useful semantics in a serialized state.
For example, a Thread
object is tied to the state of the current JVM. There is no context in which a deserialized Thread
object would maintain useful semantics.
Secondly, the serialized state of an object forms part of its classes‘ compatibility contract.
Maintaining compatibility between versions of serializable classes requires additional effort and consideration. Therefore, making a class serializable needs to be a deliberate design decision and not a default condition.
Lastly, serialization allows access to non-transient private members of a class that are not otherwise accessible.
Classes containing sensitive information (for example, a password) should not be serializable nor externalizable. The standard encoding method uses a simple translation of the fields into a byte stream. Primitives as well as non-transient, non-static referenced objects are encoded into the stream. Each object that is referenced by the serialized object via a field that is not marked astransient
must also be serialized; and if any object in the complete graph of non-transient object references is not serializable, then serialization will fail. The developer can influence this behavior by marking objects as transient, or by redefining the serialization for an object so that some portion of the reference graph is truncated and not serialized.
It is possible to serialize Java objects through JDBC and store them into a database.[citation needed] While Swing components do implement the Serializable interface, they are not portable between different versions of the Java Virtual Machine. As such, a Swing component, or any component which inherits it, may be serialized to an array of bytes, but it is not guaranteed that this storage will be readable on another machine.
玛莎--JAXB的marshell