Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot REST with XML Support

I made a simple REST webservice with Spring Boot 1.2.5 and it works fine for JSON but I can't make this work to return XML.

This is my controller:

@RestController
..
@RequestMapping(method = RequestMethod.GET,  produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@ResponseStatus(HttpStatus.OK)
public List<Activity> getAllActivities() {
    return activityRepository.findAllActivities();
}

When I call it with Accept: application/json everything works, but when I try with application/xml I get some HTML with 406 Error and message:

The resource identified by this request is only capable of generating responses 
with characteristics not acceptable according to the request "accept" headers.

My model objects:

@XmlRootElement
public class Activity {

    private Long id;
    private String description;
    private int duration;
    private User user; 
    //getters & setters...
}

@XmlRootElement
public class User {

    private String name;
    private String id;
    //getters&setters...
}

My pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

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

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

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

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Do I need some additional jars in my pom.xml to make this work? I tried adding jaxb-api or jax-impl but it didn't help.

like image 885
jgr Avatar asked Sep 18 '15 14:09

jgr


People also ask

Can REST be used with XML?

REST stands for Representational State Transfer. It is a software architecture style that relies on a stateless communications protocol, most commonly, HTTP. REST structures data in XML, YAML, or any other format that is machine-readable, but usually JSON is most widely used.

Does spring boot support XML?

Spring Boot REST XML example. The following application is a Spring Boot RESTful application which returns data in XML format from an H2 database using Spring Data JPA. This is the project structure. This is the Maven build file.

CAN REST API have XML response?

Data types that REST API can return are as follows: JSON (JavaScript Object Notation) XML. HTML.

How do I send XML data to a RESTful web service?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.

What is Spring Boot RESTful web service?

In this Spring Boot tutorial, I will show you a Restful Web service example in that Spring REST Controller can receive/consume XML Request Body and return XML Response instead of JSON. We also use Spring Data JPA to interact with database (MySQL/PostgreSQL).

How to make a restful Spring Boot project consume and produce XML?

But we can easily make our RESTful Spring Boot Web service endpoints consume and produce an XML representation of a resource as well. To make our Spring Boot project consume and produce an XML representation of a resource, we will need to add to our POM.xml one additional dependency.

How to return data in XML in Spring Boot?

When we create a Spring Boot project with ‘Starter Web’ dependency, we only get support for returning data in JSON format, with the help of the Jackson library. To embed support for returning data in XML format we need third-party dependencies. There are many libraries that support XML return format, for Example – Jackson, JAXB, etc.

What is the use of application/xml dependency in Spring Boot?

By adding the above dependency to our project, we are making our project be able to consume and produce XML. In the case when our Spring Boot project supports XML, providing the Content-Type HTTP Header with a value of application/xml is optional.


1 Answers

To make this work in Spring Boot without using Jersey we need to add this dependency:

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

The output will be a bit ugly but it works:

<ArrayList 
    xmlns="">
    <item>
        <id/>
        <description>Swimming</description>
        <duration>55</duration>
        <user/>
    </item>
    <item>
        <id/>
        <description>Cycling</description>
        <duration>120</duration>
        <user/>
    </item>
</ArrayList>

Here is nice tutorial: http://www.javacodegeeks.com/2015/04/jax-rs-2-x-vs-spring-mvc-returning-an-xml-representation-of-a-list-of-objects.html

like image 199
jgr Avatar answered Oct 03 '22 16:10

jgr