而wsgen工具用到了JAX-B
这个Person类被注解为一个@XmlType,Skier类被注解为一个@XmlRootElement,这两个注解都是用来说明Java到XML的绑定
package eg.jaxb;
import javax.xml.bind.annotation.XmlType;
@XmlType
public class Person {
private String name;
private int age;
private String gender;
public Person() {
}
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setName(String name) {
this.name = name;
}
}
package eg.jaxb;
import java.util.Collection;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Skier {
private Person person;
private String national_team;
private Collection major_archivements;
public Skier() {
}
public Skier(Person person, String national_team,
Collection major_archivements) {
this.person = person;
this.national_team = national_team;
this.major_archivements = major_archivements;
}
public Collection getMajor_archivements() {
return major_archivements;
}
public String getNational_team() {
return national_team;
}
public Person getPerson() {
return person;
}
public void setMajor_archivements(Collection major_archivements) {
this.major_archivements = major_archivements;
}
public void setNational_team(String national_team) {
this.national_team = national_team;
}
public void setPerson(Person person) {
this.person = person;
}
}
Marshal演示了Marshaling(编码)/Unmarshaling(解码)的过程
package eg.jaxb;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
public class Marshal {
private static final String file_name = "bd.xml";// 源程序是file_name = "bd.mar"
public static void main(String[] args) {
new Marshal().run_example();
}
private void run_example() {
try {
JAXBContext ctx = JAXBContext.newInstance(Skier.class);
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// // Marshal a Skier object: 1st to stdout,2nd to file
Skier skier = createSkier();
m.marshal(skier, System.out);
FileOutputStream out = new FileOutputStream(file_name);
m.marshal(skier, out);
out.close();
// Unmarshal as proof of concept
Unmarshaller u = ctx.createUnmarshaller();
Skier bd_clone = (Skier) u.unmarshal(new File(file_name));
System.out.println("--------------");
m.marshal(bd_clone, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
private Skier createSkier() {
Person bd = new Person("Bjoern Daehlie", 41, "Male");
List list = new ArrayList();
list.add("12 Olympic Medals");
list.add("9 World Championships");
list.add("Winningest Winter Olympian");
list.add("Greatest Nordic Skier");
return new Skier(bd, "Norway", list);
}
}
以上代码来自《Java消息服务》
下面说明一下Marshaling(编码)与wsgen的关系了
这是一个用wsgen工具生成的Java类,生成GetTimeAsElapsedResponse 所用类在附件
package ch02.ts.jaxws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "getTimeAsElapsedResponse", namespace = "http://ts.ch02/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getTimeAsElapsedResponse", namespace = "http://ts.ch02/")
public class GetTimeAsElapsedResponse {
@XmlElement(name = "return", namespace = "")
private long _return;
/**
*
* @return
* returns long
*/
public long get_return() {
return this._return;
}
/**
*
* @param _return
* the value for the _return property
*/
public void set_return(long _return) {
this._return = _return;
}
}
这个类中@XmlType注解的name属性设置为 "
getTimeAsElapsedResponse",这就是客户端调用getTimeAsElapsed服务操作时由Web服务端返回给客户端的SOAP消息名称。这就是返回的SOAP:
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml;charset="utf-8"
5e
8d
1359292481466
0
这是上面SOAP消息的Request:
POST /ts HTTP/1.1
Content-type: text/xml;charset="utf-8"
Soapaction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
User-Agent: JAX-WS RI 2.1.6 in JDK 6
Host: 127.0.0.1:4567
Connection: keep-alive
Content-Length: 167
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import ch02.ts.jaxws.GetTimeAsElapsedResponse;
public class MarshalGTER {
public static void main(String[] args) {
new MarshalGTER().run_example();
}
private void run_example() {
try {
JAXBContext ctx = JAXBContext
.newInstance(GetTimeAsElapsedResponse.class);
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
GetTimeAsElapsedResponse tr = new GetTimeAsElapsedResponse();
tr.set_return(new java.util.Date().getTime());
m.marshal(tr, System.out);
} catch (PropertyException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
输出结果:
1359312314440
可以看到所有导入的命名空间URI是一样的。使用JAX-B,Java类型实例可以编码为XML文档实例,反之这些XML文档也可以解码为Java类型或其他语言类型。
JAX-B对与JAVA和XSD类型间的转换提供底层支持。