本文介绍了spring mvc rest mongo dbobject响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个spring mvc rest调用,响应应该是mongo db(Basic)DBObject的结果.据我所知,DBObject是一个JSON对象.是否可以返回该对象,或者我应该返回它们的常规字符串内容?

这是我到目前为止的解决方案:

@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");

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

有人对此要求有更好的解决方案吗?

thx非常提前.西蒙

解决方案

我在您的代码中看到了一件奇怪的事情.您必须返回json或xml吗?如果必须返回json,这在您的情况下很简单,@ ResponseBody发挥了神奇作用

@RequestMapping(value = "/content/json/{ids}", method = RequestMethod.GET)
@ResponseBody
public MyGreatContentObject getContentByIdsAsJSON(@PathVariable("ids") String ids) {
   return this.contentService.findContentByListingId(ids);
}

无论如何,我认为您仍然必须多学习一些基本概念

i want to create a spring mvc rest call and the response should be the results from the mongo db (Basic)DBObject. the DBObject is, as far as i know, a JSON object. is it possible to return this objects or should i return the normal string content of them?

this is the solution i have so far:

@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");

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

does anyone have a better solution for that requirement?

thx very much in advance.simon

解决方案

I'm see a strange thing in you code. Do you must return json or xml? If you must return json it's simple in your situation, @ResponseBody do the magic

@RequestMapping(value = "/content/json/{ids}", method = RequestMethod.GET)
@ResponseBody
public MyGreatContentObject getContentByIdsAsJSON(@PathVariable("ids") String ids) {
   return this.contentService.findContentByListingId(ids);
}

in any way, i'm think you still must learn base concepts a little more

这篇关于spring mvc rest mongo dbobject响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:25