JAXB WebService数据映射

作用是 java对象 <---> xml文件   之间的转换

JAXB Annotation

@XmlRootElement   // xml 文件的根元素

@XmlElement

@XmlAccessorType  // 表明类内,什么样的成员 是 可以被xml 转化 传输的  可以是 FIELD PROPERTY ...

@XmlTransient

@XmlJavaTypeAdaptor      http://speed847.iteye.com/blog/454231

 

 

1.1webService接口(传入一个对象参数)

@WebService
public interface ISampleService {

 public String sayUserName(
   @WebParam(name="user")
   UserDTO user);
}

 

1.2 webService实现类

@WebService(endpointInterface="com.server.ISampleService")
public class SampleServiceImpl implements ISampleService {

 public String sayUserName(UserDTO user) {
  
  return user.getName();
 }

}

 

1.3

/*
 * 使用jaxb 的annotation标注java-xml映射,尽量使用默认约定
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="User")
public class UserDTO {
 private Integer id;
 private String name;

}

 

 

2.1 webService接口(返回一个list)

@WebService
public interface IListServer {

 public @WebResult(partName="lt") StringList tList();
}

 

 

2.2 webService实现类

@WebService(endpointInterface="com.server.list.IListServer",targetNamespace = "http://list.server.com/", serviceName = "ListServerService", portName = "ListServerPort")
public class ListServer implements IListServer{

 
 public StringList tList(){
  List<Object> lt=new ArrayList<Object>();
  lt.add(1);
  lt.add(233);
  lt.add("I love you!");
  lt.add("你好!");
  
  StringList sl=new StringList();
  sl.setStrList(lt);
  return sl;
 }
}

 

2.3 把list封装成一个对象

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"strList"},name="stringlist")
public class StringList {

 @XmlElement(nillable=true)
 private List<Object> strList;

 public List<Object> getStrList() {
  return strList;
 }

 public void setStrList(List<Object> strList) {
  this.strList = strList;
 }
}

 

 

JAXB uses annotations to specify which Java properties in a class should be serialized using MTOM or WSIAP. For MTOM, the @XmlMimeType annotation lets you specify how a binary (e.g., java.awt.Image) Java property gets bound to a schema element decorated with the xmime:content-Type attribute. The xmime:contentType attribute [XMIME] is used to indicate the content type of an XML element with type xs:base64Binary or xs:hexBinary. For WSIAP, the @XmlAttachmentRef annotation plays the same role.

<script></script>

你可能感兴趣的:(java,xml,webservice,Blog)