且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring 3支持RESTful API/APP配置示例

更新时间:2022-10-02 19:23:58

  1. 支持APP&API模式的RESTful配置示例

XML示例:
<beans xmlns:context=http://www.springframework.org/schema/context
… 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配置示例:
<beans …    xmlns:p=http://www.springframework.org/schema/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">
                                    <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);

本文转自 dannyy1026 51CTO博客,原文链接:http://blog.51cto.com/dannyyuan/671654