English 中文(简体)
Spring mvc休息, json和xml输出含有包装名称
原标题:Spring mvc rest, json and xml output contain the package name

We need to support XML and JSON output from a objects, I have a List which i display in json and xml format, the problem i am facing is it also shows the underlying class, below i am showing all part of codes starting with output
domain objects configuration & controller

**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;

www.un.org/spanish/ecosoc 这些背景与本《一样。

 <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>

www.un.org/spanish/ecosoc 控制器部分为:

    @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

i 撰写习俗转换器。

问题回答

你提供的守则似乎使用了XStream说明,而你的春季配置则被配置成使用劫机。

Have you tried the annotations supported by jackson? https://github.com/FasterXML/jackson-annotations/wiki/JacksonAnnotations





相关问题
.net xmlserializer error

The error is when the class gets serialized, I don t get a run time error or anything (unless I try to deserialize). When the XmlSerializer serializes my class, some times it adds some text at then ...

Deserialize XML with ampersand using XmlSerializer()

The following code breaks when the XML has data like "Lord & Hogan". Any suggestions? Thanks, Ken private T GetResponse<T>(String apiObject, String query) { //Deserialize ...

Easier way to serialize C# class as XML text

While trying to answer another question, I was serializing a C# object to an XML string. It was surprisingly hard; this was the shortest code snippet I could come up with: var yourList = new List<...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

deserialize the XML Document --- Need Help

I am using the below code snippet now to deserialize the XML document ... [WebMethod] public XmlDocument OrderDocument(XmlDocument xmlDoc) { XmlSerializer serializer = new XmlSerializer(typeof(...

in C++/CLI

When I m trying serialize a class containing this property: [NonSerialized] property System::Collections::ObjectModel::ReadOnlyCollection<String^>^ IgnoredWords I get a compilation error ...

Serialize property even empty

How can I tell to the XmlSerializer to serialize a string property that is empty? [XmlElement("description")] public string Description { get; set; ...

xmlserializer validation

I m using XmlSerializer to deserialize Xml achives. But I found the class xsd.exe generated only offers capability to read the xml, but no validation. For example, if one node is missing in a document,...

热门标签