簡體   English   中英

無法使用RestTemplate Jaxb解組響應正文

[英]Cannot unmarshall response body with resttemplate jaxb

我想從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 Accept = [application / xml,text / xml,application / * + xml]響應200 OK以“ application / xml”的形式讀取[swe.game.model.GameIdentifier]提取類型[swe.game.model.GameIdentifier]類型和內容的響應時出錯輸入[application / xml]; 嵌套的異常是org.springframework.http.converter.HttpMessageNotReadableException:無法解組為[class swe.game.model.GameIdentifier]:意外元素(uri:“”,local:“ uniqueGameIdentifier”)。 期望的元素是<{} GameIdentifier>; 嵌套的異常是javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“ uniqueGameIdentifier”)。 預期的元素是<{} GameIdentifier>

GameIdentifier中的注釋與提供的xml不匹配。 您的xml應該是這樣的:

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

或者您必須將@XmlRootElement(name =“ GameIdentifier”)更改為@XmlRootElement(name =“ uniqueGameIdentifier”); (內部元素的更改相同)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM