我已将我的应用程序配置为返回 html、json 或 xml 视图。html 和 xml 视图很好,但 json 视图有问题。
返回的 json 总是以“{} &&”为前缀
例子
{} && {"asd":{"name":"Practical Spring LDAP","isbn":"978-1475265453","author":"Balaji Varanasi"}}
我渲染视图的弹簧配置是
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="true"/>
</bean>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.sam.beans.Book</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
样品控制器
@RequestMapping("/getBook")
public String showBook(Model model)
{
model.addAttribute("book", new Book("Practical Spring LDAP", "978-1475265453", "Balaji Varanasi"));
return "book";
}
需要帮助...
更新:为 Book 添加类定义
@XmlRootElement(name="book")
public class Book
{
private String name;
private String isbn;
private String author;
public Book()
{
}
public Book(String name, String isbn, String author)
{
this.name = name;
this.isbn = isbn;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}