Spring MVC Rest响应JSON和XML [英] spring mvc rest response json and xml

查看:109
本文介绍了Spring MVC Rest响应JSON和XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以xml结构或json结构的字符串形式从数据库返回结果. 我有一个解决方案,但我不知道这是否是解决此问题的最佳方法. 我这里有两种方法:

I have the requirement to return the result from the database either as a string in xml-structure or as json-structure. I've got a solution, but I don't know, if this one is the best way to solve this. I have two methods here:

@RequestMapping(value = "/content/json/{ids}", method = RequestMethod.GET)
public ResponseEntity<String> getContentByIdsAsJSON(@PathVariable("ids") String ids)
{
  String content = null;
  StringBuilder builder = new StringBuilder();
  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.add("Content-Type", "text/html; charset=utf-8");
  // responseHeaders.add("Content-Type", "application/json; charset=utf-8");

  List<String> list = this.contentService.findContentByListingIdAsJSON(ids);
  if (list.isEmpty())
  {
     content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>no data  found</error>";
     return new ResponseEntity<String>(content, responseHeaders, HttpStatus.CREATED);
  }
  for (String json : list)
  {
     builder.append(json + "\n");
  }
  content = builder.toString();
  return new ResponseEntity<String>(content, responseHeaders, HttpStatus.CREATED);
}

@RequestMapping(value = "/content/{ids}", method = RequestMethod.GET)
public ResponseEntity<String> getContentByIdsAsXML(@PathVariable("ids") String ids)
{
  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.add("Content-Type", "application/xml; charset=utf-8");

  String content = this.contentService.findContentByListingIdAsXML(ids);
  if (content == null)
  {
     content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>no data found</error>";
     return new ResponseEntity<String>(content, responseHeaders, HttpStatus.CREATED);
  }
  return new ResponseEntity<String>(content, responseHeaders, HttpStatus.CREATED);
}

对于第一种方法,我需要一个更好的解决方案,我已经在这里问过: spring mvc rest mongo dbobject响应

for the first method I need a better solution, which I already asked here: spring mvc rest mongo dbobject response

接下来的事情是,我在配置中插入了一个json转换器:

The next thing is, that I inserted in the configuration a json converter:

<bean id="jsonHttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
   <property name="supportedMediaTypes" value="application/json"/>
</bean>

当我将第一种方法的content-type更改为"application/json"时,它可以工作,但是xml响应不再起作用,因为json转换器希望将xml字符串转换为json-structure我想.

when I change the content-type at the first method to "application/json", it works, but then the xml response doesn't work anymore, because the json converter wants to convert the xml string to json-structure I think.

我该怎么办,那个春天确定了一个方法应返回json类型而另一个方法应将普通xml作为字符串返回的区别? 我用接受标志尝试了它:

what can I do, that spring identifies the difference that the one method should return a json type and the other one a normal xml as string? I tried it with the accept flag:

@RequestMapping(value = "/content/json/{ids}", method = RequestMethod.GET, headers = "Accept=application/json")

但这是行不通的.我收到以下错误:

but this doesn't work. I get the following error:

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError

我希望有人能帮助我.

推荐答案

如果使用的是Spring 3.1,则可以利用@RequestMapping批注中新的produces元素来确保您生成XML或JSON,如下所示:您希望,即使在同一应用内也是如此.

If you are using Spring 3.1, you can take advantage of the new produces element on the @RequestMapping annotation to ensure that you produce XML or JSON as you wish, even within the same app.

我在这里写了一篇关于这个的帖子:

I wrote a post about this here:

http://springinpractice.com/2012/02/22/supporting-xml-and-json-web-service-endpoints-in-spring-3-1-using-responsebody/

这篇关于Spring MVC Rest响应JSON和XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆