- 支持APP&API模式的RESTful配置示例
… default-autowire="byName" >
<!—byName, byType, autodetect -->
<context:component-scan base-package="com.**.controller"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="ignoreAcceptHeader" value="true"/>
<property name="defaultContentType" value="text/html"/>
<property name="mediaTypes">
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<property name="favorParameter" value="false"/>
<property name="viewResolvers">
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"></property>
<property name="defaultViews">
<!-- for application/json -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<!-- for application/xml -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView" >
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
<!—
这是很重要的列装器,
可以方便地实现bean<=>xml,
需另外下载(http://xstream.codehaus.org/download.html),
可比JAXB
套件易用哦 -- >
Spring 3 Servlet + HttpMessageConverter + Annotation
– StringHttpMessageConverter: Text converter
– FormHttpMessageConverter : Form converter (application/x-www-form-urlencoded , ampped to MultiValueMap<String,String>)
– MarshallingHttpMessageConverter: XML converter(marshaller)
– MappingJacksonHttpMessageConverter: JSON converter(via Jackson’s ObjectMapper
– AtomFeedHttpMessageConverter: ATOM converter(via ROME’s Feed API)
– RssChannelHttpMessageConverter : RSS converter(via ROME’s Feed API)
<bean class="org.springframework.web.servlet.mvc.annotation .AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<ref bean="jsonConverter" />
<ref bean="marshallingConverter" />
<ref bean="atomConverter" />
<bean id="jsonConverter" class="org.springframework.http.converter.json .MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" /> <
<bean id="marshallingConverter" class="org.springframework.http.converter.xml .MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<value>com.company.project.bean.EntityA</value>
<value>com.company.project.bean.EntityB</value>
<bean id="atomConverter" class="org.springframework.http.converter.feed .AtomFeedHttpMessageConverter">
<property name="supportedMediaTypes" value="application/atom+xml" />
@RequestMapping(method=RequestMethod.GET, value="/emp/{id}", headers="Accept=application/json")
public @ResponseBody EntityA getEntityA(@PathVariable String id) { … return EntityA;}
Spring 3 Servlet + MVC + Annotation
– ContentNegotiatingViewResolver: Resource representation negotiation
– BeanNameViewResolver: Common view resolver based on bean name as spring-like.
– UrlBasedViewResolver: Spring MVC view same as former Spring version (JSP for text/html)
– InternalResourceViewResolver: Mostly like to UrlBasedViewResolver.
– MappingJacksonJsonView : JSON view
– MarshallingView : XML view (via XStreamMarshaller or Jaxb2Marshaller, etc)
<beans … xmlns:p= xmlns:context="http://www.springframework.org/schema/context” … default-autowire="byName"
<context:component-scan base-package="com.**.controller"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="ignoreAcceptHeader" value="true"/>
<property name="defaultContentType" value="text/html"/>
<property name="mediaTypes">
<entry key="json" value="application/json" />
<entry key=“html" value=“text/html" />
<entry key="xml" value="application/xml" />
<property name="favorParameter" value="false"/>
<property name="viewResolvers">
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages"/><property name="suffix" value=".jsp"></property>
<property name="defaultViews">
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView" >
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
<!– it needs the jar libaray: xstream.jar(http://xstream.codehaus.org/download.html) -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/>
public ModelAndView show(@PathVariable java.lang.Integer id) throws Exception {
UserInfo userInfo = manger.get(id);
return new ModelAndView("/userinfo/edit","userInfo",userInfo);
本文转自 dannyy1026 51CTO博客,原文链接:http://blog.51cto.com/dannyyuan/671654