解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題
我在將項目用boot重構(gòu)時, 關(guān)于cxf的使用出了一些問題, 主要在實體類和json轉(zhuǎn)換這一方面。
在看了一些晚上的相關(guān)答案后, 了解到j(luò)axb默認支持xml格式, 而實現(xiàn)對象轉(zhuǎn)json是需要額外的轉(zhuǎn)換器的,然后在stackoverflow上找到一個解決方法是聲明一個bean,注入JsonProvider,但我發(fā)現(xiàn)這個可以解決服務(wù)端將對象轉(zhuǎn)為json的問題,
而客戶端還是會報一個異常:
No message body reader has been found for class ......, ContentType: application/json
后面在cxf的WebClient類的源碼中發(fā)現(xiàn):
create()方法有很多重載方法,其中有一個是可以指定provider來轉(zhuǎn)換格式,最后通過這個重載方法解決了客戶端json格式轉(zhuǎn)換問題。

在單獨使用cxf的基礎(chǔ)上做出改動,主要有兩方面
1. 服務(wù)端:在啟動類上聲明一個bean, 注入JacksonJaxbJsonProvider
2. 客戶端:在WebClient調(diào)用create()方法時,指定轉(zhuǎn)json的provider
下面是一個簡單的demo:
一、webservice服務(wù)端(生產(chǎn)者)1.maven依賴<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--cxf-jaxrs-starter--><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxrs</artifactId><version>3.2.0</version></dependency><!--jaxrs轉(zhuǎn)json工具--><dependency><groupId>com.fasterxml.jackson.jaxrs</groupId><artifactId>jackson-jaxrs-json-provider</artifactId><version>2.8.5</version></dependency>2.application.yml配置文件
配置cxf路徑和包掃描
server: port: 9001cxf: path: /services servlet.init: service-list-path: /info jaxrs: component-scan: true3.boot應(yīng)用啟動類配置
在啟動類中聲明一個bean,自動注入JacksonJaxbJsonProvider 對象,這樣cxf在將對象轉(zhuǎn)為json時會自動使用這個對象
@SpringBootApplicationpublic class CxfServerApplication { public static void main(String[] args) {SpringApplication.run(CxfServerApplication.class, args);} // 配置一個對象與json轉(zhuǎn)換的工具@Beanpublic JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {return new JacksonJaxbJsonProvider();}}4.客戶服務(wù)接口
關(guān)于cxf的路徑注解,請參照其他cxf資料
@Path('/customerService')public interface CustomerService { /** * 客戶服務(wù):根據(jù)id查詢客戶 */ @Path('/findById') @GET @Produces({'application/xml', 'application/json'}) Customer findById(@QueryParam('id')Integer id);}5.客戶服務(wù)實現(xiàn)類
一個簡單的實現(xiàn)類, 不需要加額外注解, 注入dao從數(shù)據(jù)庫查詢數(shù)據(jù)返回(dao層代碼未貼出, 可自行實現(xiàn))。
@Service@Transactionalpublic class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDao customerDao; @Override public Customer findById(Integer id) {// 調(diào)用dao, 從數(shù)據(jù)庫查詢客戶return customerDao.findById(id); }}二、webservice客戶端(消費者)1.maven依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--cxf-jaxrs-starter--><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxrs</artifactId><version>3.2.0</version></dependency><!--jaxrs轉(zhuǎn)json工具--><dependency><groupId>com.fasterxml.jackson.jaxrs</groupId><artifactId>jackson-jaxrs-json-provider</artifactId><version>2.8.5</version></dependency>2.配置轉(zhuǎn)json工具
由于WebClient的create()方法需要的是List<Provider>形式的參數(shù),所以創(chuàng)建一個繼承ArrayList類的JsonProvider,在構(gòu)造方法中添加JacksonJaxbJsonProvider對象元素
@Componentpublic class JsonProvider extends ArrayList<JacksonJaxbJsonProvider> {// 在構(gòu)造方法中, 添加JacksonJaxbJsonProvider public JsonProvider(){this.add(new JacksonJaxbJsonProvider()); }}3.使用WebClient調(diào)用webservice服務(wù)
在Controller內(nèi)注入上面創(chuàng)建的自定義的JsonProvider,并在WebClient調(diào)用create()方法時,作為方法參數(shù)注入,以此達到手動指定json轉(zhuǎn)換器的目的
@Controllerpublic class CustomerController { // 注入配置的轉(zhuǎn)json工具 @Autowired private List<JacksonJaxbJsonProvider> jsonProvider; @RequestMapping('/customer_findById') @ResponseBody public List<Customer> findById(Integer id) {//調(diào)用webservice獲取查詢數(shù)據(jù)Customer customer = WebClient.create('http://localhost:9001/services/customerService/findById?id='+id, jsonProvider).accept(MediaType.APPLICATION_JSON).get(Customer.class);return customer; }}三、其他注意1.需要用xml/json格式轉(zhuǎn)換后傳輸?shù)膶嶓w類要在類名上加一個注解
@XmlRootElement(name = 'xxx')2.上面demo使用的cxf-spring-boot-starter-jaxrs版本為3.2.0
在3.2.1以后的版本需要手動配置ViewResolver
否則會報錯:
@ConditionalOnProperty(spring.mvc.locale) did not find property ’locale’ (OnPropertyCondition)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:

網(wǎng)公網(wǎng)安備