解決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: 9001
cxf:
path: /services
servlet.init:
service-list-path: /info
jaxrs:
component-scan: true
3.boot應(yīng)用啟動類配置
在啟動類中聲明一個bean,自動注入JacksonJaxbJsonProvider 對象,這樣cxf在將對象轉(zhuǎn)為json時會自動使用這個對象
@SpringBootApplication
public class CxfServerApplication {
public static void main(String[] args) {
SpringApplication.run(CxfServerApplication.class, args);
}
// 配置一個對象與json轉(zhuǎn)換的工具
@Bean
public 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
@Transactional
public 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對象元素
@Component
public 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)換器的目的
@Controller
public 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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot與spring security的結(jié)合的示例
這篇文章主要介紹了SpringBoot與spring security的結(jié)合的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
SpringBoot結(jié)合JWT登錄權(quán)限控制的實現(xiàn)
本文主要介紹了SpringBoot結(jié)合JWT登錄權(quán)限控制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
Java利用策略模式實現(xiàn)條件判斷,告別if else
策略模式定義了一系列算法,并且將每個算法封裝起來,使得他們可以相互替換,而且算法的變化不會影響使用算法的客戶端。本文將通過案例講解如何利用Java的策略模式實現(xiàn)條件判斷,告別if----else條件硬編碼,需要的可以參考一下2022-02-02
ThreadLocal使用案例_動力節(jié)點Java學院整理
這篇文章主要介紹了ThreadLocal使用案例分析,需要的朋友可以參考下2017-08-08
SpringBoot2.0整合Shiro框架實現(xiàn)用戶權(quán)限管理的示例
這篇文章主要介紹了SpringBoot2.0整合Shiro框架實現(xiàn)用戶權(quán)限管理的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08

