无法使用RestTemplate Jaxb解组响应正文

时间:2018-11-25 18:07:17

标签: java spring xsd resttemplate

我想从xsd生成Java类,然后使用resttemplate将其解组。我知道如何生成它。但是在收到响应后,我重新模板抛出一个错误。

这是我的xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="GameIdentifier">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="UniqueGameID" type="xs:string" maxOccurs="1" minOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这是我pom.xml的样子

<parent>
    <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.2</version>
            <type>maven-plugin</type>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.12</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>

                    <outputDirectory>src/main/java</outputDirectory>
                    <clearOutputDir>false</clearOutputDir>
                    <packageName>swe.game.model</packageName>
                    <schemaDirectory>src/main/resources/xsd/</schemaDirectory>
                    <schemaFiles>gameId.xsd</schemaFiles>
                    <schemaFiles>gameState.xsd</schemaFiles>
                    <schemaFiles>halfMap.xsd</schemaFiles>
                    <schemaFiles>playerRegistration.xsd</schemaFiles>
                    <schemaFiles>responseEnvelope.xsd</schemaFiles>
                    <schemaFiles>responseEnvelopeUniqueId.xsd</schemaFiles>
                </configuration>
            </plugin>

        </plugins>
    </build>

这是我生成的类的外观:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "uniqueGameID"
})
@XmlRootElement(name = "GameIdentifier")
public class GameIdentifier {

    @XmlElement(name = "UniqueGameID", required = true)
    protected String uniqueGameID;

    /**
     * Gets the value of the uniqueGameID property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUniqueGameID() {
        return uniqueGameID;
    }

    /**
     * Sets the value of the uniqueGameID property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUniqueGameID(String value) {
        this.uniqueGameID = value;
    }
}

这就是我所说的:

私有静态最终Logger日志= LoggerFactory.getLogger(NetworkManager.class);

public void gameId(){     GameIdentifier gameIdentifierMessage = null;

try {
  URL url = new URL(BASE + PORT + "game/new");
  log.info("" + url.toURI());
  RestTemplate restTemplate = new RestTemplate();

  gameIdentifierMessage = restTemplate.getForObject(url.toURI(), GameIdentifier.class);

  log.info(gameIdentifierMessage.toString());
} catch (Exception e) {
  log.error(e.getLocalizedMessage());
}

这是我的日志

  

http:// **************** / game / new HTTP GET   http://swe.wst.univie.ac.at:18235/game/new接受= [application / xml,   text / xml,application / * + xml]响应200 OK读取到   [swe.game.model.GameIdentifier]作为“ application / xml”时出错   提取类型[swe.game.model.GameIdentifier类]的响应,然后   内容类型[application / xml];嵌套异常为   org.springframework.http.converter.HttpMessageNotReadableException:   无法解组到[class swe.game.model.GameIdentifier]:   意外元素(uri:“”,本地:“ uniqueGameIdentifier”)。预期   元素是<{} GameIdentifier>;嵌套异常为   javax.xml.bind.UnmarshalException:意外元素(uri:“”,   本地:“ uniqueGameIdentifier”)。预期元素是   <{} GameIdentifier>

1 个答案:

答案 0 :(得分:0)

GameIdentifier中的注释与提供的xml不匹配。您的xml应该是这样的:

<GameIdentifier>
    <UniqueGameID>zL07H</UniqueGameID>
</GameIdentifier>

或者您必须将@XmlRootElement(name =“ GameIdentifier”)更改为@XmlRootElement(name =“ uniqueGameIdentifier”); (内部元素的更改相同)