0

我们需要支持对象的 XML 和 JSON 输出,我有一个以 json 和 xml 格式显示的列表,我面临的问题是它还显示了底层类,下面我显示了以输出
域开头的所有代码部分对象配置和控制器

**XML Output**

    "<**com.bookstore.Books**> Unwanted
    <books>
    <com.bookstore.Book>
    <name>book0</name>
    </com.bookstore.Book>
    <com.bookstore.Book>
    <name>book1</name>
    </com.bookstore.Book>
    </books>
    </com.bookstore.Books>" 


    **JSON Output** Unwanted part is in bold

    **"org.springframework.validation.BindingResult.books**" 
    :[{"isbn":"03601","authors":[{"autho

    I have defind book class as
    @XStreamAlias("book")
    public class Book {....}
    and Books
    @XStreamAlias("books")
    public class Books {...

List<Book> books;

上下文设置是这样的

 <beans:bean id="xmlView"
      class="org.springframework.web.servlet.view.xml.MarshallingView">
      <beans:constructor-arg>
         <beans:bean class="org.springframework.oxm.xstream.XStreamMarshaller">
            <beans:property name="autodetectAnnotations" value="true"/>
         </beans:bean>
      </beans:constructor-arg>
   </beans:bean>
   <beans:bean id="jsonView"
      class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
   </beans:bean> 
   <beans:bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
   </beans:bean>

控制器部分是:

    @RequestMapping(value = "/books/xml")
        public ModelAndView getAllBooksXML() {
            List<Book> books = bookService.getAllBooks();
            ModelAndView mav = 
                new ModelAndView("xmlView", BindingResult.MODEL_KEY_PREFIX + "books", books);
            return mav;
        }

    @RequestMapping(value = "/books/json")
        public ModelAndView getAllBooksJson() {
            List<Book> books = bookService.getAllBooks();
            ModelAndView mav = 
                new ModelAndView("jsonView", BindingResult.MODEL_KEY_PREFIX + "books", books);
            return mav;
        }


Please let me know otherwie i will have t

我写了一个自定义转换器。

4

1 回答 1

0

您提供的代码似乎使用 XStream 注释,而您的 spring 配置配置为使用 jackson。

你试过jackson支持的注解吗? https://github.com/FasterXML/jackson-annotations/wiki/JacksonAnnotations

于 2012-05-01T12:15:35.010 回答