美文网首页
springboot get,post请求001

springboot get,post请求001

作者: 前进的码农 | 来源:发表于2019-11-27 11:04 被阅读0次

请求方式注解

@RequestMapping post,get都支持不指定情况下
@GetMapping
只支持post方式请求
等同于@RequestMapping(value = "",method = RequestMethod.GET)
@PostMapping只支持post方式请求
等同于@RequestMapping(value = "",method = RequestMethod.POST)

获取请求参数的几种方式

1、不带键值直接从路径中获取

postman请求格式如下:
192.168.3.157:8080/user/get_user_info/10

   @RequestMapping("/get_user_info/{user_id}")
    public Object test(@PathVariable("user_id")String userId){
        map.put("name","name");
        map.put("userId",userId);
        return map;
    }

2、通过键值获取

192.168.3.157:8080/user/get_user_info?user_id=10

  @RequestMapping("get_user_info")
    public Object test1(@RequestParam("user_id")String userId ){
        map.put("name","name");
        map.put("userId",userId);
        return map;
    }

3、获取请求头信息

通过注解@RequestHeader获取

 @PostMapping("get_user_info")
 public Object test1(@RequestHeader("token")String token){
        System.out.println("token--->>"+token);
        return token;
    }

4、@RequestParam注解说明

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    @AliasFor("name")
    String value() default "";//name等同于value

    @AliasFor("value")
    String name() default "";

    boolean required() default true;//是否为必传参数
        //默认值
    String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}

使用

@RequestParam(value = "user_id",defaultValue = "123",required = false)String userId
@RequestParam(name = "user_id",defaultValue = "123",required = false)String userId
上面两种方式是相等的
@RequestParam("user_id")

从body中获取数据@RequestBody

@RequestMapping(value = "/get_user_info/{user_id}",method = RequestMethod.GET)
    public Object test(@PathVariable("user_id")String userId, @RequestBody String str){
        System.out.println("jsonpObject--->>"+str);
        map.put("name","name");
        map.put("userId",userId);
        return map;
    }
 String这里的String类型可以写任意类型,json,map具体的实体类都可以前提是格式要对

相关文章

  • springboot get,post请求001

    请求方式注解 获取请求参数的几种方式 1、不带键值直接从路径中获取 postman请求格式如下:192.168.3...

  • iOS请求方法和网络安全

    GET和POST请求 GET和POST请求简介 GET请求模拟登陆 POST请求模拟登陆 GET和POST的对比 ...

  • iOS请求方法和网络安全

    GET和POST请求GET和POST请求简介GET请求模拟登陆POST请求模拟登陆GET和POST的对比保存用户信...

  • java发送http请求

    restTemplate get请求 post请求 apache.http.client get请求 post请求...

  • 解决springboot中只支持get请求,无法支持post请求

    解决springboot中只支持get请求,无法支持post请求 报错信息如下: 405 相关类如下: 解决办法:...

  • Get和Post的区别

    Get请求和Post请求区别如下: Post请求比Get请求更安全,get请求直接将参数放置在URL中,post请...

  • Okhttp3

    简介 配置 请求思路 get请求思路 post请求思路 get,post 同步和异步请求 异步请求(get) 同步...

  • gf框架 ghttp使用

    案例中包含以下内容 get请求 get请求携带参数 post请求携带参数 post请求发送xml数据 post请求...

  • HttpUtil工具

    HttpUtil工具,http get post请求,https get post请求,ajax response...

  • ajax 请求的时候 get 和 post 方式的区别?

    get和post的区别 get请求不安全,post安全 get请求数据有限制,post无限制 get请求参数会在u...

网友评论

      本文标题:springboot get,post请求001

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