Springmvc fastjson Springmvc基于fastjson实现导包及配置文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Springmvc fastjson Springmvc基于fastjson实现导包及配置文件

Y_wee   2021-03-15 我要评论
想了解Springmvc基于fastjson实现导包及配置文件的相关内容吗,Y_wee在本文为您仔细讲解Springmvc fastjson的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Springmvc,fastjson,导包,配置文件,下面大家一起来学习吧。

1、导包

 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.58</version>
  </dependency>

2、配置文件

 <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
      <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes">
          <list>
            <value>text/html;charset=UTF-8</value>
            <value>application/json</value>
            <value>application/xml;charset=UTF-8</value>
          </list>
        </property>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>

3、jsp代码

<script>
  $(function () {
    $.ajax({
      url:"${pageContext.servletContext.contextPath}/user/json",
      data:JSON.stringify({username:"tom"}),//JSON.stringify()必须有,否则只会当做表单的格式提交
      dataType:"json",
      type:"post",
      contentType:"application/json;charset=utf-8",//如果想以json格式把数据提交到后台的话,这个必须有,否则只会当做表单提交
      success:function (data) {
        alert(data.username);
      }
    });
  });
</script>

4、控制器代码

@RequestMapping("/json")
  public @ResponseBody User jsonToObject(@RequestBody User user){
    System.out.println("--------");
    System.out.println(user);
    return user;
  }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们