秀外慧中的springMVC(六)---輸入和輸出使用xml格式方式

可以在方法上使用@ResponseBody返回單個vo並轉換爲xml返回數據,當然如果想傳入xml轉換實體使用@RequestBody和   json的傳入傳出是一樣的.

springmvc配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:security="http://www.springframework.org/schema/security"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd  
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd  
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!--
SpringMVC視圖選擇器 
 -->
	<bean id="viewResolver"	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>  
		<property name="prefix" value="/WEB-INF/webPage/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	
<!--
返回xml
 -->	
	<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg>
            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                <property name="classesToBeBound">
                    <list>
                        <value>com.wechat.pdcquery.vo.User</value>
                    </list>
                </property>
            </bean>
        </constructor-arg>
    </bean>

<!--
返回json(需要額外的包支持) 
 -->	  
 <!-- 
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
	    <property name="supportedMediaTypes">  
	        <list>  
	            <value>text/html;charset=UTF-8</value>
	            <value>application/json;charset=UTF-8</value>
	              
	        </list>  
	    </property>  
	</bean> 
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
	    <property name="messageConverters">  
	        <list>  
	            <ref bean="mappingJacksonHttpMessageConverter"/>  
	        </list>  
	    </property>  
	</bean>  
	 -->
</beans>  

實體需要註解以便轉換爲xml

package com.wechat.pdcquery.vo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="user")
public class User {
    
    private int id;
    private String name;
    private String address;
    
    public int getId() {
        return id;
    }
    @XmlElement
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    @XmlElement
    public void setAddress(String address) {
        this.address = address;
    }    
}
controller

    @RequestMapping(value="/users", method = RequestMethod.GET)
    public @ResponseBody User getUsers() {    
        //模擬從數據庫中獲得數據....        
            User user = new User();
            user.setId(1);
            user.setName("Name: aaa");
            user.setAddress("Address: bbb");
            return user;            
     }

轉換list

實體bean

package com.wechat.pdcquery.vo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="user")
public class User {
    
    private int id;
    private String name;
    private String address;
    
    public int getId() {
        return id;
    }
    @XmlElement
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    @XmlElement
    public void setAddress(String address) {
        this.address = address;
    }    
}

package com.wechat.pdcquery.vo;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;

@SuppressWarnings("unchecked")
@XmlRootElement
public class ListBean {
    private String name;
    private List list;

	@XmlElements({ @XmlElement(name = "user", type = User.class) })
    public List getList() {
        return list;
    }
 
    public void setList(List list) {
        this.list = list;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}
controller
@RequestMapping(value = "/querynodata", method = { RequestMethod.GET,
			RequestMethod.POST })
	public @ResponseBody
	ListBean querynodata(HttpServletRequest request, ModelMap map) {

		List<User> list = new ArrayList<>();
		User user = new User();
		user.setId(1);
		user.setName("Name: aaa");
		user.setAddress("Address: bbb");
		list.add(user);
		list.add(user);
		// moView.addObject(list);
		ListBean listBean = new ListBean();
		listBean.setList(list);
		listBean.setName("ffff");
		return listBean;
		
	}

map同理只要xml註解規範就會轉換,如果不能轉換則會調用下一個轉換器進行轉換

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章