Spring boot REST API to produce xml reading from file
In this article, we will be seeing how to produce XML response in REST end-point developed using Spring Boot.
Spring main class, which will read the XML content and produces the content as REST end-point.
package com.simple.xml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
}
}
@RestController
class XmlStringConverter {
@GetMapping(path= "/content", produces = { "application/xml", "text/xml" })
public String articleInfo() throws IOException {
// provide the path where the xml file content is present
File xmlFile = new File("/path/of/xml/file/resources/my-articles.xml");
String xml2String ="";
try (BufferedReader bufReader = new BufferedReader(new FileReader(xmlFile))){
StringBuilder sb = new StringBuilder();
String line = bufReader.readLine();
while( line != null){ sb.append(line).append("\n");
line = bufReader.readLine(); }
xml2String= sb.toString();
}
return xml2String;
}
}
Below is the content (file name my-articles.xml), this will read and displayed in the end-point.
Note: If the xml file is within the class path, simply use the file name when reading in java.
<note>
<to>X</to>
<from>Y</from>
<heading>Reminder</heading>
<body>Message to X from Y!</body>
</note>
- Dependencies included in the pom.xml, to read xml, jackson-dataformat-xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rest-xml</groupId>
<artifactId>simple-xml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- xml dependency-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<!-- xml dependency end-->
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
After executing the spring boot application, https://localhost:8080/content
the xml content will be rendered as below.