spring mvc restful service string/json/xml

Spring MVC可以提供restful service,通过@ResponseBody注解可以返回各种数据类型,比如string/json/xml

配置:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	   <property name="messageConverters">
	       <list>
	           <ref bean="jsonConverter" />
	       </list>
	   </property>
	</bean>
	 
	<bean id="jsonConverter"
	      class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
	   <property name="supportedMediaTypes" value="application/json" />
	</bean>	

关于xml类型的配置,网上有通过配置 MarshallingHttpMessageConverter,但是如果对model对象不加jaxb注解的话,仍然不work,但是,加了注解以后,即使不配置 MarshallingHttpMessageConverter,仍然可以解析xml,因此,本文不采用 MarshallingHttpMessageConverter,直接在数据对象上加jaxb注解。

package com.bill.springrest.model;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "user")
public class User implements Serializable{
    private int userId;
    private String userName = "";
    private String password = "";
    
    @XmlElement
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    
    @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;
    }
    
    @Override
    public String toString() {
        return "User [userId=" + userId + ", userName=" + userName
                + ", password=" + password + "]";
    }
}

对于List数据,jaxb没有提供直接的支持,需要进行数据封装

package com.bill.springrest.model;

import java.util.List;

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

@XmlRootElement(name = "userList")
public class UserList{
    private int count;
    private List<User> userList;
    
    public UserList() {}
    
    public UserList(List<User> userList) {
        this.userList = userList;
        this.count = userList.size();
    }
    
    public int getCount() {
        return count;
    }
    
    public void setCount(int count) {
        this.count = count;
    }
    
    @XmlElement(name="user")
    public List<User> getUserList() {
        return userList;
    }
    
    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
    
}

对于rest接口的测试,可以用firefox的restClient插件 https://addons.mozilla.org/zh-CN/firefox/addon/restclient/

在controller中可以通过headers限定可以处理的请求,比如headers="Accept=application/json,application/xml"

controller会根据请求中的header来判断返回的数据格式,比如,请求中是Accept: application/json,那么将返回json对象

例如,发送GET http://127.0.0.1:8080/springrest/rest/user/1.do           Accept: application/xml

返回

<user>

< password />
< userId > 1 </ userId >
< userName > Bill </ userName >
</ user >


源码:http://download.csdn.net/detail/sundongsdu/5840955


参考文献:

http://my.oschina.net/615stu/blog/79312

http://www.ibm.com/developerworks/cn/web/wa-spring3webserv/

http://www.mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值