博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 3支持RESTful API/APP配置示例
阅读量:5922 次
发布时间:2019-06-19

本文共 5960 字,大约阅读时间需要 19 分钟。

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

XML
示例:
<beans 
xmlns: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=     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

转载地址:http://lvsvx.baihongyu.com/

你可能感兴趣的文章
fail2ban 帮助postfix 过滤恶意IP
查看>>
Simple Proxy Server (Java)
查看>>
Kafka消费的几种方式--low-level SimpleConsumer
查看>>
解决mysql数据库不能支持中文的问题
查看>>
VMware14虚拟机秘钥
查看>>
JVM -verbose参数详解
查看>>
CentOS LInux启动关闭和服务管理
查看>>
java与xml
查看>>
Redis Sentinel机制与用法(二)
查看>>
ls命令实际使用
查看>>
磁盘及磁盘阵列系统选择
查看>>
Javascript异步数据的同步处理方法
查看>>
9. Palindrome Number(回文数)(leetcode)
查看>>
MySQL之自定义函数实例讲解
查看>>
用.htaccess获取文件夹和文件名
查看>>
自我提升mysql
查看>>
步步为营之——建造者模式(Builder)
查看>>
快速排序——Java
查看>>
unity游戏与我
查看>>
187. Repeated DNA Sequences
查看>>