好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解决springboot整合cxf-jaxrs中json转换的问题

前言

我在将项目用boot重构时, 关于cxf的使用出了一些问题, 主要在实体类和json转换这一方面。

在看了一些晚上的相关答案后, 了解到jaxb默认支持xml格式, 而实现对象转json是需要额外的转换器的,然后在stackoverflow上找到一个解决方法是声明一个bean,注入JsonProvider,但我发现这个可以解决服务端将对象转为json的问题,

而客户端还是会报一个异常:

No message body reader has been found for class ......, ContentType: application/json

后面在cxf的WebClient类的源码中发现:

create()方法有很多重载方法,其中有一个是可以指定provider来转换格式,最后通过这个重载方法解决了客户端json格式转换问题。

最后的解决方案:

在单独使用cxf的基础上做出改动,主要有两方面

1. 服务端:在启动类上声明一个bean, 注入JacksonJaxbJsonProvider

2. 客户端:在WebClient调用create()方法时,指定转json的provider

下面是一个简单的demo:

一、webservice服务端(生产者)

1.maven依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

< 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转json工具-->

< dependency >

     < groupId >com.fasterxml.jackson.jaxrs</ groupId >

     < artifactId >jackson-jaxrs-json-provider</ artifactId >

     < version >2.8.5</ version >

</ dependency >

2.application.yml配置文件

配置cxf路径和包扫描

?

1

2

3

4

5

6

7

8

server:

   port: 9001

cxf:

   path: /services

   servlet.init:

     service-list-path: /info

   jaxrs:

     component-scan: true

3.boot应用启动类配置

在启动类中声明一个bean,自动注入JacksonJaxbJsonProvider 对象,这样cxf在将对象转为json时会自动使用这个对象

?

1

2

3

4

5

6

7

8

9

10

11

12

@SpringBootApplication

public class CxfServerApplication {

     public static void main(String[] args) {

         SpringApplication.run(CxfServerApplication. class , args);

     }

 

     // 配置一个对象与json转换的工具

     @Bean

     public JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {

         return new JacksonJaxbJsonProvider();

     }

}

4.客户服务接口

关于cxf的路径注解,请参照其他cxf资料

?

1

2

3

4

5

6

7

8

9

10

@Path ( "/customerService" )

public interface CustomerService {

     /**

      * 客户服务:根据id查询客户

      */

     @Path ( "/findById" )

     @GET

     @Produces ({ "application/xml" , "application/json" })

     Customer findById( @QueryParam ( "id" )Integer id);

}

5.客户服务实现类

一个简单的实现类, 不需要加额外注解, 注入dao从数据库查询数据返回(dao层代码未贴出, 可自行实现)。

?

1

2

3

4

5

6

7

8

9

10

11

@Service

@Transactional

public class CustomerServiceImpl implements CustomerService {

     @Autowired

     private CustomerDao customerDao;

     @Override

     public Customer findById(Integer id) {

         // 调用dao, 从数据库查询客户

         return customerDao.findById(id);

     }

}

二、webservice客户端(消费者)

1.maven依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

< 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转json工具-->

< dependency >

     < groupId >com.fasterxml.jackson.jaxrs</ groupId >

     < artifactId >jackson-jaxrs-json-provider</ artifactId >

     < version >2.8.5</ version >

</ dependency >

2.配置转json工具

由于WebClient的create()方法需要的是List<Provider>形式的参数,所以创建一个继承ArrayList类的JsonProvider,在构造方法中添加JacksonJaxbJsonProvider对象元素

?

1

2

3

4

5

6

7

@Component

public class JsonProvider extends ArrayList<JacksonJaxbJsonProvider> {   

     // 在构造方法中, 添加JacksonJaxbJsonProvider

     public JsonProvider(){

         this .add( new JacksonJaxbJsonProvider());

     }

}

3.使用WebClient调用webservice服务

在Controller内注入上面创建的自定义的JsonProvider,并在WebClient调用create()方法时,作为方法参数注入,以此达到手动指定json转换器的目的

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@Controller

public class CustomerController {

     // 注入配置的转json工具

     @Autowired

     private List<JacksonJaxbJsonProvider> jsonProvider;

     @RequestMapping ( "/customer_findById" )

     @ResponseBody

     public List<Customer> findById(Integer id) {

         //调用webservice获取查询数据

         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格式转换后传输的实体类要在类名上加一个注解

?

1

@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)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/wk52525/article/details/79113978

查看更多关于解决springboot整合cxf-jaxrs中json转换的问题的详细内容...

  阅读:34次