springMvc + Json整合配置 RESTful

来源:互联网 发布:淘宝怎么修改会员名 编辑:程序博客网 时间:2024/05/23 13:32

这边文章主要记录下Mac springmvc json配置.

用到的版本:

  1. os 10.10.5 
  2. jdk 1.7
  3. spring 3.2.4
  4. maven 3.3

控制器:

@Controller@RequestMapping("/app")public class HelloContrller {    @RequestMapping(value="/info/{proId}",method=RequestMethod.GET)    public String getProductInfo(@PathVariable String proId, HttpServletRequest request,HttpServletResponse response) throws Exception {        request.setAttribute("name", proId);        return "index";    }    @RequestMapping(value = "/json/{id}", method = RequestMethod.GET)    @ResponseBody    public Object saveModel(@PathVariable int id) {        List list = new ArrayList();        list.add("name");        list.add("jack");        return list;    }}


1、主要配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">    <display-name></display-name>    <context-param>        <param-name>contextConfigLocation</param-name>        <!-- 单进程部署 -->        <param-value>classpath:applicationContext.xml</param-value>        <!-- 分布式部署 -->        <!-- <param-value>classpath:spring/spring-main-cluster.xml</param-value> -->    </context-param>    <!-- 自定义filter过滤器, 暂时无实现filter类,注释 -->    <!--<filter>        <filter-name>AccessFilter</filter-name>        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>        <init-param>            <param-name>targetBeanName</param-name>            <param-value>AccessFilter</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>AccessFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>-->    <filter>        <filter-name>setcharacter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>utf-8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>setcharacter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- end -->    <listener>        <description>spring监听器</description>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <description>spring mvc servlet</description>        <servlet-name>springMvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <description>spring mvc 配置文件</description>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring_mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springMvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>


2、spring_mvc.xml配置

<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:util="http://www.springframework.org/schema/util"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:p="http://www.springframework.org/schema/p"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd       http://www.springframework.org/schema/util       http://www.springframework.org/schema/util/spring-util.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <!-- 自动扫描(自动注入),扫描这个包以及它的子包的所有使用@Service注解标注的类 -->    <context:component-scan base-package="org.app.*"/>    <context:annotation-config/>    <!-- springMvc注解-->    <mvc:annotation-driven />    <!-- mvc资源管理-->    <mvc:resources location="/css/" mapping="/css/**" />    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />-->    <!-- ContentNegotiatingViewResolver视图解析器,利用他就可以配置多种返回值 -->    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">        <property name="order" value="1" />        <property name="favorParameter" value="false" />        <!-- 这里是否忽略掉accept header,默认就是false -->        <property name="ignoreAcceptHeader" value="true"/>        <!-- 如果所有的mediaType都没匹配上,就会使用defaultContentType -->        <property name="defaultContentType" value="text/html"/>        <property name="mediaTypes">            <map>    <span style="font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; font-size: 13px; line-height: 24.05px;">返回一个JSON格式或者XML格式的数据</span>                <entry key="json" value="application/json"/>                <entry key="xml" value="application/xml"/>            </map>        </property>        <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="/"/>                    <property name="suffix" value=".jsp"></property>                </bean>            </list>        </property>        <!-- 默认使用MappingJacksonJsonView生成jsonview-->        <property name="defaultViews">            <list>                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">                    <property name="extractValueFromSingleKeyModel" value="true"/>                </bean>                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">                    <property name="marshaller">                        <bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>                    </property>                </bean>            </list>        </property>    </bean></beans></span>


0 0
原创粉丝点击