如何在springMVC中格式化xml而不是原始格式(Restful webservice)

时间:2015-01-04 09:47:31

标签: java xml web-services spring-mvc

目前,我想使用以下格式返回返回xml结果:

我试过用这样的东西

       @XmlRootElement(name = "item")

public class Book implements Serializable {

 @XmlAttribute


public int getBookId() {


return bookId;
    }

...

.... 和

@XmlRootElement(name = "OneBoxResults")
public class JavaClazz {

private List<Book> OneBoxResults;

public List<Book> getOneBoxResults() {
    return OneBoxResults;
}
@XmlElements(@XmlElement(name = "book", type = Book.class))
public void setOneBoxResults(List<Book> oneBoxResults) {
    OneBoxResults = oneBoxResults;
}
...

但是,我收到的返回结果只有Json格式如下:

{&#34; oneBoxResults&#34;:[{&#34; BOOKID&#34;:1,&#34; BOOKNAME&#34;:&#34;搜索 部署&#34;,&#34;更新&#34;:&#34; 2014-01-07&#34;,&#34;说明&#34;:&#34;成功部署 通常涉及以下内容 元素:&#34;&#34;路径&#34;:空},{&#34; BOOKID&#34;:2&#34; BOOKNAME&#34;:&#34; GSA 信息&#34;,&#34;更新&#34;:&#34; 2015-01-07&#34;,&#34;说明&#34;:&#34;配置OneBox 模块,因此它将搜索查询发送给提供者(自定义 应用&#34;&#34;路径&#34;:空}]}

我还尝试在控制器中创建新格式,如下所示:

    @RequestMapping(value = "/rest.oneboxSample",produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET)
public @ResponseBody String oneboxSample(){
    String tmpOpenField = "<Field name=\"";
    String tmpCloseField = "</Field>";
    StringBuilder builder = new StringBuilder();
    builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    builder.append("<OneBoxResults>").append("<resultCode>");
    builder.append("Listbook").append("<resultCode>");
    for(int i = 0; i < bookDao.getBooks().size(); i++){
        Book tmpBook = bookDao.getBooks().get(i);
        builder.append("<MODULE_RESULT>");
        builder.append(tmpOpenField).append("bookId\">").append(tmpBook.getBookId()).append(tmpCloseField);
        builder.append(tmpOpenField).append("bookName\">").append(tmpBook.getBookName()).append(tmpCloseField);
        builder.append(tmpOpenField).append("update\">").append(tmpBook.getUpdate()).append(tmpCloseField);
        builder.append(tmpOpenField).append("description\">").append(tmpBook.getDescription()).append(tmpCloseField);
        builder.append(tmpOpenField).append("path\">").append(tmpBook.getPath()).append(tmpCloseField);
        builder.append("</MODULE_RESULT>");
    }
    builder.append("</OneBoxResults>");
    return builder.toString();
}

但结果并不好。它返回了一个字符串而不是我们需要的xml格式。

现在,我们的系统需要接收xml格式而不是原始xml格式。 请告诉我如何做到这一点。

以下是我写的源代码

https://www.dropbox.com/s/4tyg0kp7gkzodod/onebox-service.zip?dl=0

谢谢,

0 个答案:

没有答案