Spring MVC请求及返回JSON数据

  • 返回JSON对象,我们就需要用到@ResponseBody注解,如果接收JSON数据封装成JavaBean的话,我们就需要用到@RequestBody注解。随后在配置文件上创建对应的bean即可。

  • 一、通过json数据传输
$("#submit_bt").click(function(){
    var data = {
        title: $("#title").val(),
        dm_label: $("#dm_label").val(),
        url: $("#url").val(),
        img_url: $("#img_url").html(),
        state: 0
    }
    $.ajax({
        type:'post',
        url:'${ctx}/admin/dynamic/message/add',
        dataType : 'json',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        success:function(data){
            if (data.mark=="0") {
                window.location.href="${ctx}/admin/dynamic/message/list?state=0&page_size=10&father_id=24";
            } else {
                alert(data.tip);
            }
        },
        error: function(textStatus) {
            alert(textStatus);
        }
    });
});

@RequestMapping(value = "/admin/dynamic/message/add", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
@Authority(AuthorityType.NoAuthority)
public RetInfo insert(@Valid @RequestBody TsJzDynamicMessage tsJzDynamicMessage, BindingResult result, HttpServletRequest request) {
    RetInfo retInfo = new RetInfo();
    if (result.hasErrors()) {
        List<FieldError> err = result.getFieldErrors();
        FieldError fe = err.get(0);
        retInfo.setMark("1");
        retInfo.setTip(fe.getDefaultMessage());
    } else {
        TsJzAdmin tsJzAdmin = AdminSession.getAdmin(request);
        retInfo = dynamicMessageService.insertDynamicMessage(tsJzDynamicMessage, tsJzAdmin);
    }
    return retInfo;
}

二、通过url数据传输

$("#submit_bt").click(function(){
    var data = "func_name=" + $("#func_name").val() + "&link_url=" + $("#link_url").val()
        +"&is_valid=" + $("#is_valid option:selected").val() +"&parent_id=" + $("#parent_id").val();
    $.ajax({
        type:'post',
        url:'${ctx}/admin/funcion/add',
        dataType : 'json',
        data: data,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success:function(data){
            if (data.mark=="0") {
                window.location.href="${ctx}/admin/funcion/list?page_size=10&father_id=${father_id}";
            } else {
                alert(data.tip);
            }
        },
        error: function(textStatus) {
            alert(textStatus);
        }
    });
});
@RequestMapping(value = "/admin/funcion/add")
@ResponseBody
@Authority(AuthorityType.NoAuthority)
public RetInfo addFunction(HttpServletRequest request, @Valid TsJzFuncion function, BindingResult result) {
    RetInfo retInfo = new RetInfo();
    if (result.hasErrors()) {
        List<FieldError> err = result.getFieldErrors();
        FieldError fe = err.get(0);
        retInfo.setMark("1");
        retInfo.setTip(fe.getDefaultMessage());
    } else {
        HttpSession session = request.getSession(false);
        TsJzAdmin tsJzAdmin = (TsJzAdmin) session.getAttribute(Constant.USERLOGINSESSION);
        retInfo = funcionService.addFunction(function, tsJzAdmin);
    }
    return retInfo;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值