关于java:springboot二RestApi

29次阅读

共计 2990 个字符,预计需要花费 8 分钟才能阅读完成。

第一类:申请门路参数

@PathVariable
获取门路参数。即 url/{id} 这种模式。

@RequestParam
获取查问参数。即 url?name= 这种模式

第二类:Body 参数

第三类:申请头参数以及 Cookie

@RequestHeader
@CookieValue

代码实现

新建包 controller

新建类 Student

package com.example.demo.controller;

public class Student {
    private String name = "";
    private int age = 1;

    private int id = 1;

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public int getAge() {return age;}

    public void setAge(int age) {this.age = age;}

    public int getId() {return id;}

    public void setId(int id) {this.id = id;}

    public String toString() {return "name:" + name + ";age=" + age + ";id:" + id;}
}

新建 StudentController

package com.example.demo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class StudentController {@RequestMapping(value = "/student")

    public Object student(@RequestParam(name = "id") Integer id, Integer age, String name, HttpServletRequest request) {
//      //RequestParam 模式不传会报错,Integer id 模式不传则为空
        System.out.println(request.getHeader("Connection"));
        System.out.println(request.getCookies());
        for (Cookie cookie : request.getCookies()) {if ("myCookie".equals(cookie.getName())) {System.out.println(cookie.getValue());
            }
        }

        System.out.println(name);
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        return student;
    }

    @GetMapping(value = "/student/detail/{id}/{age}")
    public Object student1(@PathVariable("id") Integer id,
                           @PathVariable("age") Integer age) {Map<String,Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("age",age);

        System.out.println(retMap);
        return retMap;
    }

    //    @RequestMapping(value = "/student/detail/{id}/{status}")
    @DeleteMapping(value = "/student/detail/{id}/{status}")
    public Object student2(@PathVariable("id") Integer id,
                           @PathVariable("status") Integer status) {Map<String,Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("status",status);
        return retMap;
    }

    @DeleteMapping(value = "/student/{id}/detail/{city}")
    public Object student3(@PathVariable("id") Integer id,
                           @PathVariable("city") Integer city) {Map<String,Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("city",city);
        return retMap;
    }

    @PostMapping(value = "/student/{id}")
    public void addStudent(@RequestBody Student student,@PathVariable("id") Integer id) {System.out.println("----------start------");
        System.out.println(student.getName());
        System.out.println(student);
        System.out.println(id);
        System.out.println("----------end------");
    }

    @PostMapping(value = "/student2")
    public Object addStudent2(@RequestBody Student student) {System.out.println("----------start------");
        System.out.println(student);
        System.out.println("----------end------");
        return student;
    }

    @PostMapping(value = "/student3")
    public Object addStudent3(Student student) {
//        不写 @RequestBody 相当于获取类的初始值
        System.out.println("----------start------");
        System.out.println(student);
        System.out.println("----------end------");
        return student;
    }

    @PutMapping(value = "/student/{id}")
    public String updateStudent(@PathVariable("id") Integer id,@RequestBody Student student) {System.out.println(student);
        return "update Student ID:" +id;
    }
}

正文完
 0