• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Y-wee
博客园    首页    新随笔    联系   管理     

springmvc使用fastjson

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;
    }
记得快乐
posted @ 2020-10-20 15:26  Y-wee  阅读(434)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3