- 支持APP&API模式的RESTful配置示例
XML示例:
… 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">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="favorParameter" value="false"/>
<property name="viewResolvers">
<list>
<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>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<!-- 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套件易用哦 -- >
</property>
</bean>
</list>
</property>
</bean>
2. 支持API模式的RESTful配置特例
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)
XML示例:
<bean class="org.springframework.web.servlet.mvc.annotation .AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
<ref bean="marshallingConverter" />
<ref bean="atomConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json .MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" /> <
/bean>
<bean id="marshallingConverter" class="org.springframework.http.converter.xml .MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.company.project.bean.EntityA</value>
<value>com.company.project.bean.EntityB</value>
</list>
</property>
</bean>
<bean id="atomConverter" class="org.springframework.http.converter.feed .AtomFeedHttpMessageConverter">
<property name="supportedMediaTypes" value="application/atom+xml" />
</bean>
控制器代碼示例:
@RequestMapping(method=RequestMethod.GET, value="/emp/{id}", headers="Accept=application/json")
public @ResponseBody EntityA getEntityA(@PathVariable String id) { … return EntityA;}
2. 支持APP模式的RESTful配置特例
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)
XML配置示例:
<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">
<map>
<entry key="json" value="application/json" />
<entry key=「html" value=「text/html" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="favorParameter" value="false"/>
<property name="viewResolvers">
<list>
<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>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<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) -->
</property>
</bean>
</list>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/>
控制器代碼示例:
@PATH(「/{id}」)
public ModelAndView show(@PathVariable java.lang.Integer id) throws Exception {
UserInfo userInfo = manger.get(id);
return new ModelAndView("/userinfo/edit","userInfo",userInfo);