0
votes

When I'm sending XML payload to REST controller is not mapped as expected. My implementation has been done by using Spring Boot. I have generated the POJOs based on the XSD file using JaxB. XML elements are mapped only if they follow the same naming convention followed in POJO.

<Declaration xmlns:p="My_Common_Types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MY_Schema.xsd">
    <ID>ID</ID>
    <Regime>REG</Regime>
</Declaration>

Generated Code Using JAXB is as follows,


    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "id",
        "regime"
    })
    @XmlRootElement(name = "Declaration")
    public class Declaration {

        @XmlElement(name = "ID")
        protected String id;

        @XmlElement(name = "Regime")
        protected String regime;

        public String getID() {
            return id;
        }

        public void setID(String value) {
            this.id = value;
        }

        public String getRegime() {
            return regime;
        }

        public void setRegime(String value) {
            this.regime = value;
        }
    }

Rest Controller is as follows,


    @RestController
    public class XMLConsumerController {

        @PostMapping("/xmlPayload")
        public void decodeXML(@RequestBody Declaration xmlPayLoad) {

        }
    }

pom.xml dependencies are as follows,

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

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

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

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

</dependencies>

Really appreciate if someone can help me to solve this.

1

1 Answers

1
votes

Remove the jackson-dataformat-xml dependency and will work.

Update:

Used your pasted xml, via Postman. The body is sent as text/xml. My dependencies as below:

<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-web</artifactId>
    </dependency>
</dependencies>

And it works fine

Update 2:

From the message in the error response, it looks that it expects namespace for the Declaration, even I do not see it in the class you posted. Probably there is a namespace specified in package-info.java.

Ok try with this body for the request:

<p:Declaration xmlns:p="My_Common_Types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MY_Schema.xsd">
    <ID>ID</ID>
    <Regime>REG</Regime>
</p:Declaration>

It may still fail, but definitely will not complain about Declaration. If it does about the ID or Regime then add the 'p' prefix in the payload and repeat.