@RequestMapping(value="/show3", method = RequestMethod.GET)
public ModelAndView getShow3(People people){
System.out.println("show3");
ModelAndView modelAndView = new ModelAndView();
this.people = people;
modelAndView.setViewName("json");
modelAndView.addObject(people);
modelAndView.addObject("json","11");
return modelAndView;
}
这是请求的代码springmvcxmljson
public ModelAndView getShow3(People people){
System.out.println("show3");
ModelAndView modelAndView = new ModelAndView();
this.people = people;
modelAndView.setViewName("json");
modelAndView.addObject(people);
modelAndView.addObject("json","11");
return modelAndView;
}
这是请求的代码springmvcxmljson
这是xml返回的结果这是json返回的结果
<property name="order" value="1" />
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
<constructor-arg>
<map>
<entry key="json" value="application/json;charset=utf-8"/>
<entry key="xml" value="application/xml;charset=utf-8"/>
</map>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" >
</bean>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<array>
<value>com.test.controller.People</value>
</array>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property></bean>
这是配置文件关于返回类型的
@Component
@XmlRootElement(name="people")
public class People {
private int id;
private String username;
private String password;
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@XmlElement
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
7楼我有说的,
参考下这篇文章
考虑使用xstream来转化xml
额我试了一种办法 就是你说的加属性,我在People类里加了一个User 类型的属性,
传值成功了网址输入太长了,还有有没有不通过设置属性传的办法啊
感觉是13楼说的问题,应该往哪里添节点