Rest模板getForObject()仅映射camel case字段

时间:2018-06-04 07:58:32

标签: spring spring-boot resttemplate

使用休息Web服务返回以下xml响应。

Person>
<ttId>1408</ttId>
<FirstName>RAJ</FirstName>
<NationalityValue>INDIAN</NationalityValue>
<Sex>Male</Sex>
</Person>

这是dto

import java.io.Serializable;
import java.util.Date;


public class PersonInfoDto implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Long ttId;    
    private String NationalityValue;
    private String Sex;
    private String FirstName;

    //getters and setters

}

使用Spring Boot,当我尝试使用代码时,它只返回一个值(在驼峰的情况下)。我是否需要添加任何命名策略才能使其正常运行?

String uri = apiPath;       
RestTemplate restTemplate = new RestTemplate();
PersonInfoDto personInfoDto = restTemplate.getForObject(uri, PersonInfoDto.class);

//此处对象只包含一个值

ttId = 1408

其余值返回null。

这需要依赖吗? 这是pom文件

<?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>msa</groupId>
    <artifactId>MQA</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <name>MQA</name>
    <description>MQA project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <orika-core.version>1.4.6</orika-core.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- Oracle JDBC driver -->
        <!-- https://mvnrepository.com/artifact/com.oracle/ojdbc14 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- Orika -->
        <dependency>
            <groupId>ma.glasnost.orika</groupId>
            <artifactId>orika-core</artifactId>
            <version>${orika-core.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2 个答案:

答案 0 :(得分:0)

您应该使用@XmlElement。试试这个

public class PersonInfoDto implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Long ttId;    
    @XmlElement(name = "NationalityValue")
    private String nationalityValue;
    @XmlElement(name = "Sex")
    private String sex;
    @XmlElement(name = "FirstName")
    private String firstName;

    //getters and setters
}

另一种选择是编写自定义Http消息转换器。看看这个http://www.baeldung.com/spring-httpmessageconverter-rest

答案 1 :(得分:0)

从pom.xml中删除了以下条目。它奏效了。

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>