美文网首页
前端传参JSON格式, SpringBoot 处理JSON格式请

前端传参JSON格式, SpringBoot 处理JSON格式请

作者: cyhai | 来源:发表于2020-07-07 18:01 被阅读0次

    现在不管是web端还是客户端,请求使用的都基本是用json格式,像iOS中,使用字典键值带参

    NSError *error;
    NSData *body = [NSJSONSerialization dataWithJSONObject:@{@"name":@"哈哈哈哈哈",@"age":@27} options:0 error:&error];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:body];
    

    客户端这样的做法,是为了更好的处理参数。
    再例如小程序端的参数带值也是如此

        request(url,params,methodType) {
            let _this = this;
            return new Promise((resolve, reject) => {
                var reqTask = wx.request({
                    url: url,
                    data: params,
                    header: { "content-type": "application/json"},
                    method: methodType,
                    dataType: 'json',
                    responseType: 'text',
    
                    success: (result) => {
                        resolve(result);
                     
                    },
                    fail: () => { },
                    complete: () => { }
                });
    
            });
    

    JavaScript中 XMLHttpRequest的网络请求

     var request = new XMLHttpRequest();
        //构造POST请求正文数据
        request.open('POST', URL, true);
        request.setRequestHeader("content-type","application/json");
        // 构造请求  请求类型 URL 是否异步 true异步 false同步
        // 发送请求
        request.send(JSON.stringify({"name":"哈哈哈哈","age":28}));
    

    当然 JavaScript中可以使用FormData结合application/x-www-form-urlencoded

    说完前端请求,就到SpringBoot的处理了json数据

    @RequestMapping(value ="/test/posthello",method = {RequestMethod.POST},produces = "application/json;charset=UTF-8")
    @ResponseBody
    

    好吧,其实就是这么简单
    这里提供两个我写好的接口,仅供学习(请勿偷袭服务器!!!

    http://109.206.246.129:8080/demoTest/test/hello
    请求类型:GET
    参数:page(int),size(int)
    
    http://109.206.246.129:8080/demoTest/test/posthello
    请求类型:POST
    参数:name(string),age(int)
    

    相关文章

      网友评论

          本文标题:前端传参JSON格式, SpringBoot 处理JSON格式请

          本文链接:https://www.haomeiwen.com/subject/vjrzqktx.html