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.
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.
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.
Data types that REST API can return are as follows: JSON (JavaScript Object Notation) XML. HTML.
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.
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).
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With