Cxf - 轉換器
一、說明
對於一些比較複雜的組合型別資料,如如下介面中的getBookGroupByUser()方法:
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import com.xilen.cxf.entity.Book;
import com.xilen.cxf.entity.User;
@WebService
public interface UserInfoWs {
public String sayHello(String name);
public List<Book> getBookByUser(User u);
public Map<String, Book> getUserRecommBook();
public Map<User, List<Book>> getBookGroupByUser();
}
Cxf框架可能因為無法自動解析而在釋出時丟擲異常:
Exception in thread "main" javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException
at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:371)
at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:251)
at org.apache.cxf.jaxws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:155)
at javax.xml.ws.Endpoint.publish(Unknown Source)
at com.xilen.ServerMain.main(ServerMain.java:14)
Caused by: org.apache.cxf.service.factory.ServiceConstructionException
at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:341)
at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean...)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean...)
at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.buildServiceFromClass(JaxWsServiceFactoryBean.java:704)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean...)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:265)
at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:215)
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory...)
at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:159)
at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:211)
at org.apache.cxf.jaxws.EndpointImpl.getServer(EndpointImpl.java:456)
at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:334)
... 4 more
Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at java.util.List
at private java.util.Map com.xilen.cxf.ws.jaxws_asm.GetBookGroupByUserResponse._return
at com.xilen.cxf.ws.jaxws_asm.GetBookGroupByUserResponse
附:兩個實體類
public class Book {
private Integer id;
private String name;
private Date date;
/*getter and setter */
}
public class User {
private Integer id;
private String name;
private String email;
/*getter and setter */
}
二、開發
1、建立Java普通物件,對無法解析的資料型別重新封裝。如Map<User, List<Book>>型別的資料,可以做如下封裝
(1)先將Map的一個鍵值對組成一個新的物件Item
public class Item {
private User key;
private List<Book> value;
public Item() {
super();
}
/* getter and setter */
}
(2)將Item物件以集合型別做為最終轉換物件的屬性
public class DataSet {
private List<Item> item = new ArrayList<Item>();
public DataSet() {
super();
}
/* getter and setter */
}
2、開發Cxf轉換器。需要繼承javax.xml.bind.annotation.adapters.XmlAdapter<ValueType, BoundType>類。ValueType即自定義的資料型別,BoundType即受限制而需要被轉換的資料型別
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.xilen.cxf.entity.Book;
import com.xilen.cxf.entity.User;
public class MyXmlAdapter extends XmlAdapter<DataSet, Map<User, List<Book>>> {
@Override
public Map<User, List<Book>> unmarshal(DataSet v) throws Exception {
Map<User, List<Book>> maps = new HashMap<User, List<Book>>();
for (Item item : v.getItem()) {
maps.put(item.getKey(), item.getValue());
}
return maps;
}
@Override
public DataSet marshal(Map<User, List<Book>> v) throws Exception {
DataSet ds = new DataSet();
for (User user : v.keySet()) {
Item item = new Item(user, v.get(user));
ds.getItem().add(item);
}
return ds;
}
}
3、使用這個轉換器。使用時通過javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter這個註解修飾需要被轉換的介面方法上,並通過Value屬性指定轉換器
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.xilen.cxf.entity.Book;
import com.xilen.cxf.entity.User;
import com.xilen.cxf.ws.util.MyXmlAdapter;
@WebService
public interface UserInfoWs {
public String sayHello(String name);
public List<Book> getBookByUser(User u);
public Map<String, Book> getUserRecommBook();
public @XmlJavaTypeAdapter(MyXmlAdapter.class) Map<User, List<Book>> getBookGroupByUser();
}
三、附
1、工程結構圖
http://download.csdn.net/detail/u013379717/7210625
相關文章
- D/A轉換器
- Cxf - Spring整合Cxf暴露WebServicesSpringWeb
- Laravel Transform 轉換器LaravelORM
- D/A轉換器(DAC)
- 用於日期轉換的訊息轉換器
- heic格式轉換jpg工具——轉易俠heic轉換器
- 自定義值轉換器
- QWinFF(媒體轉換器)
- struts 型別轉換器型別
- PDF轉換器在電腦上怎樣使用?PDF轉換器功能有哪些?
- plist檔案格式轉換器
- [嵌入式]A/D轉換器
- 適用於Mac的WinX HD影片轉換器:影片轉換教程Mac
- Django(6)自定義路由轉換器Django路由
- Cisdem PDF Creator(PDF 生成轉換器)
- 使用瀏覽器位址列呼叫CXF Webservice的寫法瀏覽器Web
- CXF--入門
- cad版本轉換器如何操作低轉高?
- PDF轉換器手機版,PPT轉PDF方法
- 圖片格式轉換器有什麼,怎麼無損轉換heic格式
- 迅捷音訊轉換器怎麼使用?音訊
- Mybatis實踐(一)型別轉換器MyBatis型別
- 音視訊格式轉換器Permute 3
- Vidmore DVD Monster for MacDVD格式轉換器Mac
- djvu converter wizard(djvu格式轉換器)
- 萬能音訊轉換器Permute 3音訊
- 微控制器AD-DA轉換
- Windows Phone 資料繫結轉換器Windows
- JavaRetrofit2使用–自定義轉換器Java
- Native Query的自定義轉換器
- WPF 型別轉換器的實現型別
- 位元組陣列轉浮點數線上轉換器陣列
- CXF--客戶端客戶端
- CXF--整合SpringSpring
- Cxf - 基礎和示例
- PDF批次轉換器,批次轉word為pdf,批次轉ppt為pdf
- 定時器時間資料轉換子程式分析 (轉)定時器
- 在JAXP中透明的快取XSL轉換器 (轉)快取