spring boot 请求

对于http请求,无外乎都是get、post、put、delete。常用的也就那么几个。但刚学java的我还是记下他们的写法吧。虽然在工作中经常会用上,但也给初学的兄弟们更快上手吧。
get

java 复制代码
//@PathVariable 注解就是在地址中/参数,在post put del 中一样通用。下面就不做讲解了
//String name , int age 就是 ? 后面的参数了
@GetMapping("/testObjFun/{id}/{code}")
public String testObjFun(String name , int age , @PathVariable String id , @PathVariable String code){
    System.out.println(id);
    System.out.println(code);
    System.out.println(name);
    System.out.println(age);
    return "a";
}

post

其实,在post中,是有两种方式的,一种是表单提交,另一种是截荷提交。

表单提交就是我们正常 form提交,而截何提交,则是 formdata的提交。
截荷提交

java 复制代码
//截荷提交
//TestObject 是实体类,是需要另外定义的,如下所示
@PostMapping("/myPost")
public String myPost(@RequestBody TestObject testObject){
    System.out.println(testObject);
    String a1 = testObject.getA1();
    String a2 = testObject.getA2();
    System.out.println(a1);
    System.out.println(a2);

    return "aa";
}
java 复制代码
//实体类,就像我们定义数据表类一样的意思
    package com.bnc.s12.common;

    import org.springframework.beans.factory.annotation.Configurable;

    @Configurable
    public class TestObject {
        private String a1;
        public String a2;
        private String a3;

        public String getA1() {
            return a1;
        }

        public String getA2() {
            return a2;
        }

        public String getA3() {
            return a3;
        }

        public void setA1(String a1) {
            this.a1 = a1;
        }

        public void setA2(String a2) {
            this.a2 = a2;
        }

        public void setA3(String a3) {
            this.a3 = a3;
        }
    }

在前端的代码大概是如下这样的

javascript 复制代码
//这儿我只是写个例子,字段是需要跟实体类的字段要一致的
let data = new FormData();
data.addend("name" , "xiaobing");
data.append("age" , 15)

表单提交

java 复制代码
@PostMapping("/myPost")
public String myPost(@RequestParam String name , @RequestParam String id , @RequestParam String code){
    System.out.println(id);
    System.out.println(code);
    System.out.println(name);
    return "aa";
}

put 和 delete 都是一个意思,这儿我就不写了。获取前端的数据,无非就是把数据拿过来。也就是这么几种

相关推荐
2501_926227941 小时前
UDP网络编程:【Java】无连接通信到Socket实战(二)
java·网络·udp
Sunny_yiyi1 小时前
Java根据模版导出PDF文件
java·开发语言·pdf
麦兜*1 小时前
MongoDB 与 GraphQL 结合:现代 API 开发新范式
java·数据库·spring boot·mongodb·spring·maven·graphql
绝无仅有1 小时前
前端开发环境搭建:从安装 Node 到成功运行代码
后端·面试·github
shan&cen1 小时前
Day02 集合 | 30. 串联所有单词的子串、146. LRU 缓存、811. 子域名访问计数
java·数据结构·算法·缓存
yshhuang1 小时前
在Windows上搭建开发环境
前端·后端
绝无仅有1 小时前
某个互联网大厂的Elasticsearch基础面试题与答案
后端·面试·github
无责任此方_修行中2 小时前
AWS IoT Core 成本优化实战:从 PoC 到生产的省钱之旅
后端·架构·aws
ITMan彪叔2 小时前
Java MQTT 主流开发方案对比
java·后端