【问题标题】:spring boot + cxf rest json/xml providersspring boot + cxf rest json/xml 提供程序
【发布时间】:2017-01-09 02:14:59
【问题描述】:

我用 spring boot 和 cxf rest 服务实现了一个简单的项目,我试图弄清楚 xml/json 提供程序发生了什么。

  • 没有供应商 CxfConfig : 当我 @Produce("application/xml") 它返回给我一个有效的 xml 当我 @Produce("application/json") 我得到 No message body writer has found for class com.cxfexample.dto.User, ContentType: application/json
  • CxfConfig 的供应商:
    @Configuration
    public class CxfConfig {
    @Autowired
    private Bus bus;

    @Autowired
    MyServiceIntf peService;

    @Bean
    public Server rsServer() {
        JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
        endpoint.setBus(bus);
        endpoint.setAddress("/");
        endpoint.setServiceBeans(Arrays.<Object>asList(peService));
        endpoint.setProviders(Arrays.asList(jacksonJaxbJsonProvider(), jaxbElementProvider()));
        return endpoint.create();
    }

    @Bean
    public JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {
        return new JacksonJaxbJsonProvider();
    }

    @Bean
    public JAXBElementProvider jaxbElementProvider() {
        return new JAXBElementProvider();
    }
}

当我 @Produce("application/xml") or @Produce("application/json") 它工作正常但是当我实现 both 时我得到仅对 @Produce({"application/xml","application/json"}) 的第一个参数的数据格式有效

问题 1 那么有一个用于 xml 而不是 Json 的默认映射器?

问题 2 错误消息 No message body writer has found for class 是指提供者的缺席或对我的 Pojo 用户的注释错误?当我删除@XmlRootElement 我也收到了应用程序/xml 生成的这条消息。

问题 3 当我使用提供程序时,为什么我只为 @Produce({"application/xml","application/json"}) 的第一个参数获取有效数据,所以当我请求时这个服务,例如邮递员,我的内容类型只是应用程序/xml,而不是两者

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cxfexamle</groupId>
    <artifactId>cxfexample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>CXFExample</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- CXF RS -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>3.1.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.8.5</version>
        </dependency>
    </dependencies>

</project>
@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}


@Path("/Rest")
public interface MyServiceIntf {

    @GET
    @Path("/getUser")
    @Produces({"application/json","application/xml"})
    User getUser();
}


@Service
public class MyServiceImpl implements MyServiceIntf {
    @Autowired
    User user;

    @Override
    public User getUser() {
        user.setUserName("David");
        user.setPassword("123");
        return user;
    }
}


@XmlRootElement
@Component
public class User {
    private String userName;
    private String password;

    @XmlElement    
    public String getUserName() {
        return userName;
    }
    @XmlElement
    public String getPassword() {
        return password;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    } 
    public void setPassword(String password) {
        this.password = password;
    }
}

【问题讨论】:

    标签: spring spring-boot cxf


    【解决方案1】:

    问题 1 那么有一个用于 xml 而不是 Json 的默认映射器?

    是的,JAXB 自 1.6 起随 JVM 一起提供。它是一个 Java 到 XML 的映射器。映射到 JSON 需要额外的库,例如 Jackson 或 Jettison

    问题 2 错误消息 No message body writer has found for class 是指提供者的缺席或对我的 Pojo 用户的注释错误?

    这意味着 CXF 无法序列化对象。您需要添加序列化提供程序或配置 JAXB。

    问题 3 当我使用提供程序时,为什么我只在 @Produce({"application/xml","application/json"}) 的第一个参数中获得有效数据

    您需要将客户端的Accept 标头设置为所需的内容类型。如果您没有指定任何人,CXF 将根据可接受的媒体类型或@Produces 中的第一个类型生成响应

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 2012-11-10
      • 2015-09-01
      • 2019-03-01
      • 2023-01-12
      • 2018-08-24
      • 1970-01-01
      相关资源
      最近更新 更多