SpringBoot - Controller
package com.example.demo.controller;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/books")
public class BookController {
// Get请求 通过路径变量传参
// http://127.0.0.1:8080/books/20
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id :"+id);
return "传入参数:"+id;
}
// Get请求 url 传参
// http://127.0.0.1:8080/books/get_parameters?name=小明
// 这个传进来的参数名称 要和 方法 形参名 一致
@RequestMapping(value = "/get_parameters",method = {RequestMethod.GET})
public String getParameters(String name){
System.out.println(name);
return "传入参数:"+name;
}
// 使用注解 映射绑定参数,参数名称 和形参名称不一致
@RequestMapping(value = "/get_parameters1",method = {RequestMethod.GET})
public String getParameters1(@RequestParam(name = "names",required = false,defaultValue = "默认值") String name1){
System.out.println(name1);
return "传入参数:"+name1;
}
// Post请求 json 对象传参
@RequestMapping(value = "/get_body",method = {RequestMethod.POST})
public JSONObject getBody(@RequestBody JSONObject obj){
System.out.println(obj);
return obj;
}
}