使用SpringBoot将JSON有效负载转换为xml

时间:2017-04-20 19:27:19

标签: java json spring spring-boot jackson

我有一个使用SpringBoot编写的控制器操作,它处理/ greeting的POST请求并返回String响应。我的目的是将json有效负载转换为xml,并最终使用xml有效负载命中另一个webservice。我尝试了以下将json转换为xml。但是,xml转换永远不会发生。我收到了JSON回复。

GreetingController.java

array(3) { ["module"]=> string(6) "search" ["action"]=> string(12) "searchTweets" ["value"]=> string(0) "" }

Greeting.java

@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"
         }
      }
   }
}

4 个答案:

答案 0 :(得分:1)

根据documentation,正确的方法是:

JacksonXmlModule module = new JacksonXmlModule();
// to default to using "unwrapped" Lists:
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);

答案 1 :(得分:0)

尝试像这样修改你的课程:

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;
  }

}

答案 2 :(得分:0)

在Spring中默认为Json,因为包含了jackson,要覆盖此行为,首先需要设置响应的媒体类型

     @RequestMapping(produces=MediaType.APPLICATION_XML_VALUE)

并且您还需要使用

注释您的模型
       @JacksonXmlRootElement

答案 3 :(得分:0)

只需将此依赖项添加到pom中,并将“ Accept”标头作为“ application / xml”传递,它将动态地将JSON转换为xml。

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