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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值