当前位置: 代码网 > it编程>编程语言>Java > springboot注解及GET、POST接口写法

springboot注解及GET、POST接口写法

2024年05月15日 Java 我要评论
一、注解springboot提供了@contrller和@restcontroller。@controller:返回页面和数据@restcontroller:返回数据@restmapping注解:主要

一、注解

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错误

  • 404 :路劲不对
  • 405:方法不被允许

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

您可能感兴趣的文章:
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com