| 2017.10.19 | jaxws |
1.TimestampAdapter
import java.sql.Timestamp;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class TimestampAdapter extends XmlAdapter<Date, Timestamp> {
public Date marshal(Timestamp t) {
return new Date(t.getTime());
}
public Timestamp unmarshal(Date d) {
return new Timestamp(d.getTime());
}
}
2.DateAdapter
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class DateAdapter extends XmlAdapter<String, Date> {
static final String STANDARM_DATE_FORMAT = "yyyy-MM-dd";
static final String NO_SPLIT_DATE_FORMAT = "yyyyMMdd";
public Date unmarshal(String v) throws Exception {
if (v == null) {
return null;
}
DateFormat format = null;
if (v.indexOf("-") == -1) {
format = new SimpleDateFormat(NO_SPLIT_DATE_FORMAT);
} else {
format = new SimpleDateFormat(STANDARM_DATE_FORMAT);
}
return format.parse(v);
}
public String marshal(Date v) throws Exception {
DateFormat format = new SimpleDateFormat(STANDARM_DATE_FORMAT);
return format.format(v);
}
}
3.MapAdapter
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class MapAdapter extends XmlAdapter<MapConvertor, Map<Object, Object>> {
@Override
public MapConvertor marshal(Map<Object, Object> map) throws Exception {
MapConvertor convertor = new MapConvertor();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
MapConvertor.MapEntry e = new MapConvertor.MapEntry(entry);
convertor.addEntry(e);
}
return convertor;
}
@Override
public Map<Object, Object> unmarshal(MapConvertor map) throws Exception {
Map<Object, Object> result = new HashMap<Object, Object>();
for (MapConvertor.MapEntry e : map.getEntries()) {
result.put(e.getKey(), e.getValue());
}
return result;
}
}
4.MapConvertor
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlType(name = "MapConvertor")
@XmlAccessorType(XmlAccessType.FIELD)
public class MapConvertor {
private List<MapEntry> entries = new ArrayList<MapEntry>();
public void addEntry(MapEntry entry) {
entries.add(entry);
}
public static class MapEntry {
public MapEntry() {
super();
}
public MapEntry(Map.Entry<Object, Object> entry) {
super();
this.key = entry.getKey();
this.value = entry.getValue();
}
public MapEntry(String key, Object value) {
super();
this.key = key;
this.value = value;
}
private Object key;
private Object value;
public Object getKey() {
return key;
}
public void setKey(Object key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
public List<MapEntry> getEntries() {
return entries;
}
}
其中DateAdapter里面添加了yyyyMMdd格式兼容
MapAdapter用Object类型在传输JavaBean的时候是不显示具体的字段的,需要具体化为具体类型,如
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class MatDocAdapter extends XmlAdapter<MatDocConvertor, Map<Header, ArrayList<MatDoc>>> {
@Override
public MatDocConvertor marshal(Map<Header, ArrayList<MatDoc>> map) throws Exception {
MatDocConvertor convertor = new MatDocConvertor();
for (Map.Entry<Header, ArrayList<MatDoc>> entry : map.entrySet()) {
MatDocConvertor.MapEntry e = new MatDocConvertor.MapEntry(entry);
convertor.addEntry(e);
}
return convertor;
}
@Override
public Map<Header, ArrayList<MatDoc>> unmarshal(MatDocConvertor map) throws Exception {
Map<Header, ArrayList<MatDoc>> result = new HashMap<Header, ArrayList<MatDoc>>();
for (MatDocConvertor.MapEntry e : map.getEntries()) {
result.put(e.getKey(), e.getValue());
}
return result;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlType(name = "MapConvertor")
@XmlAccessorType(XmlAccessType.FIELD)
public class MatDocConvertor {
private List<MapEntry> entries = new ArrayList<MapEntry>();
public void addEntry(MapEntry entry) {
entries.add(entry);
}
public static class MapEntry {
public MapEntry() {
super();
}
public MapEntry(Map.Entry<Header, ArrayList<MatDoc>> entry) {
super();
this.key = entry.getKey();
this.value = entry.getValue();
}
public MapEntry(Header key, ArrayList<MatDoc> value) {
super();
this.key = key;
this.value = value;
}
private Header key;
private ArrayList<MatDoc> value;
public Header getKey() {
return key;
}
public void setKey(Header key) {
this.key = key;
}
public ArrayList<MatDoc> getValue() {
return value;
}
public void setValue(ArrayList<MatDoc> value) {
this.value = value;
}
}
public List<MapEntry> getEntries() {
return entries;
}
}
集合List类型会报错,需要使用ArrayList
ArrayList<Header> headerList的时候,自动生成的注解文件只有headerList一个节点参数,并没有列表参数包裹节点参数,需要手工在生成jaxws文件夹下的POJO文件加上
@XmlElementWrapper(name="items")
@XmlElement(name="item")
public List items;
其中(name=”items”)可以不写,但是XmlElement节点要写
@WebMethod
@WebResult(name = "returnResult")
public String push(@XmlJavaTypeAdapter(MapAdapter.class) @WebParam(name = "map", mode = WebParam.Mode.IN) Map<String, String> map)
throws UnknownHostException;
更新列表:
*
参考文章: