当前位置:编程学习 > JAVA >>

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

支持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)
–        MappingJa

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,