Reputation: 69
I have a controller action written using SpringBoot that handles POST requests for /greeting and returns a String response. My intention is to convert the json payload to xml and eventually hit another webservice with an xml payload. I tried the following to convert json to xml. However the xml conversion never happens. I get a JSON response.
@RequestMapping(value = "/greeting", method = RequestMethod.POST, consumes = "application/json")
public String greeting(@RequestBody final Greeting greeting) throws JsonProcessingException
{
final ObjectMapper xmlMapper = new ObjectMapper();
final String xml = xmlMapper.writeValueAsString(greeting);
return xml;
}
package com.fmr.communication.delivery.stream.model;
import com.fasterxml.jackson.databind.JsonNode;
public class Greeting
{
private long id;
private String content;
private JsonNode parent;
public String getContent()
{
return content;
}
public long getId()
{
return id;
}
public JsonNode getParent()
{
return parent;
}
public void setContent(final String content)
{
this.content = content;
}
public void setId(final long id)
{
this.id = id;
}
public void setParent(final JsonNode parent)
{
this.parent = parent;
}
}
{
"id":2,
"content":"Hello, User!",
"parent":{
"child":{
"header":{
"attrOne":"value1",
"attrTwo":"value2"
},
"footer":{
"attrOne":"value1",
"attrTwo":"value2"
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<content>Hello, User!</content>
<id>2</id>
<parent>
<child>
<footer>
<attrOne>value1</attrOne>
<attrTwo>value2</attrTwo>
</footer>
<header>
<attrOne>value1</attrOne>
<attrTwo>value2</attrTwo>
</header>
</child>
</parent>
</root>
Upvotes: 3
Views: 18253
Reputation: 1506
Just add this dependency in your pom, and pass 'Accept' header as 'application/xml' it will dynamically transform JSON to xml.
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
Upvotes: 2
Reputation: 6574
in Spring the default is Json since jackson is included, to override this behavior you need first to set the mediatype of your response
@RequestMapping(produces=MediaType.APPLICATION_XML_VALUE)
and also you will need to annotate your model with
@JacksonXmlRootElement
Upvotes: 0
Reputation: 10017
According to documentation, the correct way to do it is:
JacksonXmlModule module = new JacksonXmlModule();
// to default to using "unwrapped" Lists:
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);
Upvotes: 1
Reputation: 9796
Try to modify your class like this:
package com.fmr.communication.delivery.stream.model;
import com.fasterxml.jackson.databind.JsonNode;
@JacksonXmlRootElement(localName = "A")
public class Greeting
{
private long id;
private String content;
private JsonNode parent;
@JsonAnyGetter
public String getContent()
{
return content;
}
@JsonAnyGetter
public long getId()
{
return id;
}
@JsonAnyGetter
public JsonNode getParent()
{
return parent;
}
@JsonAnySetter
public void setContent(final String content)
{
this.content = content;
}
@JsonAnySetter
public void setId(final long id)
{
this.id = id;
}
@JsonAnySetter
public void setParent(final JsonNode parent)
{
this.parent = parent;
}
}
Upvotes: 0