SSI ļʱ



java
SSI ļʱ
首页 > 软件编程 > java > springboot  get post接口写法

springboot注解及GET、POST接口写法

作者:牛右刀薛面

springboot提供了@Contrller和@RestController注解,@Controller返回页面和数据而@RestController返回数据,本文重点介绍springboot注解及GET、POST接口写法,感兴趣的朋友一起看看吧
ƼѶơʱŻݻƷ12G99Ԫ/ꡢ24G768Ԫ/3꣬>>>9i0i.cn/qcloud

Ƽ㻹ԭ۹Ʒڰ0.8ʱ48GҵƷ2998Ԫ/3꣬>>>9i0i.cn/aliyun

一、注解

springboot提供了@Contrller和@RestController。

@Controller:返回页面和数据

@RestController:返回数据

@RestMapping注解:主要做路径映射url

value:请求URL的路径。

method:HTTP请求方法。

@RestMapping(value="user", method= RequestMethod.GET)

1.1 GET

无参数

@RequestMapping (value="/hello", method= RequestMethod.GET)
    public String hello(String name){
        return "123"+name;
    }

参数传递

@RequestMapping (value="/hello", method= RequestMethod.GET)
    public String hello(String name){
        return "123"+name;
    }

参数映射

@RequestParam注解代表参数映射,将传入进来的nickname映射到name

@RequestMapping (value="/hello2", method= RequestMethod.GET)
    public String hello2(@RequestParam(value ="nickname",required = false) String name){
        return "123"+name;
    }

1.2 POST

无参数

@RequestMapping(value = "/post1", method = RequestMethod.POST)
    public String post1(){
        return "hello post";
    }

带参数

@RequestMapping(value = "/post2", method = RequestMethod.POST)
    public String post2(String username, String password){
        return username+"-"+password;
    }

Bean封装

@RequestMapping(value = "/post3",method = RequestMethod.POST)
    public String post3(User user){
        System.out.println(user);
        return "post";
    }

json

要在参数前面加一个注解@RequestBody,传入进来的参数名和类的私有变量要保持一致

@RequestMapping(value = "/post34",method = RequestMethod.POST)
    public String post4(@RequestBody User user){
        System.out.println(user);
        return "post";
    }

1.3错误

到此这篇关于springboot注解及GET、POST接口写法的文章就介绍到这了,更多相关springboot get post接口写法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文
SSI ļʱ
SSI ļʱ


http://www.vxiaotou.com