Prev Next

Spring / Spring boot convert XML HTTP Request To JSON using Jackson

In this tutorial, we will discuss how to convert XML HTTP Request Payload to JSON response. The controller endpoint receives an XML Request which the controller method converts to JSON and returns it to the client. Please note that the XML structure is predefined and it works only with a particular Java model class.

Input XML:

<RequestModel>
    <id>1</id>
    <name>javapedia.net</name>
    <skillSet>
        <skillSet>
            <skillName>Java</skillName>
            <category>server programming</category>
        </skillSet>
        <skillSet>
            <skillName>Angular</skillName>
            <category>frontend programming</category>
        </skillSet>
        <skillSet>
            <skillName/>
            <category>database</category>
        </skillSet>
    </skillSet>
</RequestModel>

Output Json:

{
  "id": 1,
  "name": "javapedia.net",
  "skillSet": [
    {
      "skillName": "Java",
      "category": "server programming"
    },
    {
      "skillName": "Angular",
      "category": "frontend programming"
    },
    {
      "name": "mongodb",
      "category": "database"
    }
  ]
}

We add the Jackson-xml dependency in the classpath and also set consumes attribute property to "application/xml" and the conversion happens implicitly by Spring Boot Http Mession Converter.

pom.xml snippet:

<dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

Controller Method:

@PostMapping(path = "/mapXMLtoJson", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody RequestModel mapXMLtoJson(@RequestBody final RequestModel request) {
        return request;
    }

The controller method specifies that this method consumes XML and produces JSON.

Testing the endpoint using postman tool:

The complete project is available in Github.

«
»
College Assignment Help That Helps You Get More Free Time and Better Grade

Comments & Discussions