栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

get、post请求后端接受参数形式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

get、post请求后端接受参数形式

get 请求

传统拼接方式
地址① http://localhost:8989/SSSP/emps?pageNo=2
restful风格:
地址② http://localhost:8989/SSSP/emp/7
如果想获取地址①中的 pageNo的值 ‘2’ ,则使用 @RequestParam ,
如果想获取地址②中的 emp/7 中的 ‘7 ’ 则使用 @PathVariable

 @GetMapping("/param")
 //get请求简单接受,请求中没有str不hi报错,str为null
    public String getMethod(  String str){  
        System.out.println("str:"+str);
        return str;
    }

 @GetMapping("/param")
 //如果加了@RequestParam,请求中必须带str 参数名,不带会报错
    public String getMethod(@RequestParam  String str){
        System.out.println("str:"+str);
        return str;
    }


@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Product get(@PathVariable(value = "id", required = true) Long id)

@RequestMapping(value="/emp/{id}",method=RequestMethod.GET)
public String edit(@PathVariable("id")Integer id,Map 

@RequestParam 用来接收 地址栏 json格式数据(字符串、数组)
@PathVariable:占位符,用于restful风格

post请求
@PostMapping("/post3")
        public String postMethod3( @RequestBody  String str){
        System.out.println("str:"+str);
        return str;
    }
====输出====
str:{
    "str":"123"
} 
============
@RequestMapping(value = "/add", method = RequestMethod.POST)
public AjaxResult save(@RequestBody Product product) {}

@RequestBody: 转成json K-V形式接受参数,适合接收对象

content-type

Content-Type(MediaType),即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息.参考

response.Header里常见Content-Type一般有以下四种:

  • application/x-www-form-urlencoded :标准格式
  • multipart/form-data:post表单中有type=file控件时content-type会使用此编码方式.
  • application/json :最常用
  • text/xml

https://my.oschina.net/u/3086656/blog/5128991

https://www.cnblogs.com/huanghuizhou/p/9887757.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832212.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号