昨天尝试整合了下spring mvc 和 spring restful,虽谈不上历尽艰辛,但也是几经波折吧!
贴出主要的配置文件,完整的项目放在附件里自已下吧!
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.nothing" /> <!-- restful --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <!-- 声明了一个userXmlView bean 为了初始化XStreamMarshaller, 这个类会把我们接口中得到结果以XML文档形式展现出来 --> <bean id="userXmlView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.xstream.XStreamMarshaller"> <property name="autodetectAnnotations" value="true"/> </bean> </constructor-arg> </bean> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
这里注意一点,userXmlView一定要放在InternalResourceViewResolver的上面,不然返回会跳转到userXmlView.jsp中去的。当然找不到,结果你懂的!
直接给出本教程中客户端的调用
public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); String message = restTemplate.getForObject("http://localhost:8088/springmvc_restful/user/0.mv", String.class ); System.out.println(message); }
所需jar包其它的参考下面的教程吧!
参考教程:http://zhaoshijie.iteye.com/blog/1711170(里面包含服务端和客户端的教程)