微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐