天天看点

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

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

<bean class="org.springframework.web.servlet.mvc.annotation .AnnotationMethodHandlerAdapter">

<property name="messageConverters">

<list>

<ref bean="jsonConverter" />

<ref bean="marshallingConverter" />

<ref bean="atomConverter" />

</list>

<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 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;}

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.AnnotationMethodHandlerAdapter"/>

       <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

                        <property name="defaultContentType" value="text/html"/>

                        <property name="mediaTypes">

                                                <entry key=“html" value=“text/html" />

<property name="favorParameter" value="false"/>

<property name="viewResolvers">

                                        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

                                            <property name="prefix" value="/WEB-INF/pages"/><property name="suffix" value=".jsp"></property>

                                    <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>

    </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