Java RESTful服务不返回list-JSON

时间:2014-08-14 16:40:26

标签: java rest service

我的RESTful服务返回奇怪的JSON,其中包含书籍列表:

[[1,"Book1","Author1"],[2,"Book2","Author2"],[3,"Book3","Author3"]]

我想,应该是这样的:

[     {         "id": 1,    "title": "book",       "author": "author"  },     {        "id": 2,         "title": "book2",         "author": "author2"     } ]

控制器:

package com.bookService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/books")
public class BookController implements IBookController {

    @Autowired
    IBookQueries bookQueries;

    public void setBookQueries(IBookQueries bookQueries) {
        this.bookQueries = bookQueries;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @Produces(MediaType.APPLICATION_JSON)

    public @ResponseBody
    List<Book> getAllBooks() {
         return bookQueries.getAllBooks();
    }   
 }

如何获得名称 - 值对json?

1 个答案:

答案 0 :(得分:-1)

json被大括号包裹,而springmvc将使用jackson将对象渲染为json字符串

所以返回类型应该是 Object

public @ResponseBody
Object getAllBooks() {
     return bookQueries.getAllBooks();
}