简体   繁体   English

如何使用Spring MVC从POST请求返回XML响应?

[英]How to return a XML response from a POST rquest with Spring MVC?

I am making a POST request which sends a JSON. 我正在发送一个JSON的POST请求。 The Controller picks up the JSON, processes the JSON and I want the controller to return some data in XML format. 控制器获取JSON,处理JSON,我希望控制器以XML格式返回一些数据。

How can I do that with a POST request? 如何通过POST请求做到这一点?

@RequestMapping( value = Controller.RESOURCE_PATH + ".xml", headers = "Accept=application/json", produces = "*/*" )
public String exportXml( @RequestBody String requestJson ) throws IOException
{
    JSONObject json = JSONObject.fromObject( requestJson );
    Option option = new Option();
    option.processJson( json );

    return "";
}

There are many ways to achieve this. 有很多方法可以实现这一目标。 One is to use MarshallingView and XStreamMarshaller 一种是使用MarshallingView和XStreamMarshaller

Firstly add following jars to your classpath (maven dependencies): 首先,将以下jar添加到您的类路径(Maven依赖项)中:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-oxm</artifactId>
  <version>${org.springframework-version}</version>
</dependency>
<dependency>
  <groupId>com.thoughtworks.xstream</groupId>
  <artifactId>xstream</artifactId>
  <version>1.4.4</version>
</dependency>

Then configure a Marshaller on your spring xml configuration 然后在您的spring xml配置上配置Marshaller

<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>

Assuming you have following bean you want to Marshal (ie: display as XML) 假设您有以下想要调度的bean(即显示为XML)

public class MyMessage {
  private String message;

  // getters & setters
}

In your controller class inject org.springframework.oxm.Marshaller and have your handler method return a MarshallingView like this: 在控制器类中注入org.springframework.oxm.Marshaller,并让您的处理程序方法返回一个MarshallingView,如下所示:

@Controller
public class MyController {

  @Autowired private Marshaller marshaller;

  @RequestMapping("/helloxml")
  public MarshallingView helloxml(Model model) {
    MyMessage msg = new MyMessage();
    msg.setMessage("hello world");
    model.addAttribute("msg", msg);

    MarshallingView marshallingView = new MarshallingView(marshaller);
    marshallingView.setModelKey("msg"); // set what model attribute to display as xml

    return marshallingView;
  }
}

The above setup will give you xml like this when /helloxml is requested 上面的设置将在请求/helloxml时为您提供这样的xml

<com.gerrydevstory.xmlview.MyMessage>
  <message>hello world</message>
</com.gerrydevstory.xmlview.MyMessage>

Of course this isn't a very good setup if you deal with many XML marshalling. 当然,如果您要处理许多XML编组,那么这不是一个很好的设置。 You should leverage view resolvers configuration in this case. 在这种情况下,您应该利用视图解析器配置。

Also the name of XML element can be aliased too to shorten it. XML元素的名称也可以别名来缩短它。 Check out the XStream documentation 查看XStream文档

Finally, keep in mind XStream is just one of many marshaller supported by Spring, also consider JAXB, Castor, Jibx etc. 最后,请记住,XStream只是Spring支持的众多编组器之一,还考虑了JAXB,Castor,Jibx等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM