请求
代码
package com.itheima.controller;
import com.itheima.pojo.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@RestController
public class RequestController {
@RequestMapping("/simpleParam")
public String simpleParam(HttpServletRequest request){
String name = request.getParameter("name");
String age = request.getParameter("age");
int ageInt = Integer.parseInt(age);
return "name:"+name+" age:"+ageInt;
}
@RequestMapping("/simpleParam2")
public String simpleParam2(String name,int age){
return "name:"+name+" age:"+age;
}
@RequestMapping("/simpleParam3")
public String simpleParam3(@RequestParam(name = "username",required = false) String name, int age){
return "name:"+name+" age:"+age;
}
@RequestMapping("/simplePojo")
public String simplePojo(User user){
System.out.println(user);
return "OK";
}
@RequestMapping("/complexPojo")
public String complexPojo(User user){
System.out.println(user);
return "OK";
}
@RequestMapping("/arrayParam")
public String arrayParam(String[] name){
for (String s : name) {
System.out.println(s);
}
System.out.println(Arrays.toString(name));
return "OK";
}
@RequestMapping("/listParam")
public String listParam(@RequestParam List<String> name){
System.out.println(name);
return "OK";
}
@RequestMapping("/dateParam")
public LocalDateTime dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime updateTime){
return updateTime;
}
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
System.out.println(user);
System.out.println(user.getAddress().getProvince());
return "OK";
}
@GetMapping("/pathParam/{id}")
public String pathParam(@PathVariable("id") int id){
return "OK";
}
@RequestMapping("/path/{id}/{name}")
public String pathParam2(@PathVariable Integer id, @PathVariable String name){
System.out.println(id+ " : " +name);
return "OK";
}
}
json导入数据
{
"info": {
"_postman_id": "a3044d4e-20b2-44d6-919a-e12be04adc5a",
"name": "黑马Web学习",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "33580123"
},
"item": [
{
"name": "请求响应学习",
"item": [
{
"name": "简单参数",
"item": [
{
"name": "SpringBoot方式",
"item": [
{
"name": "简单请求=>同名形参",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/simpleParam2?name=Tom&age=10",
"host": [
"{{local}}"
],
"path": [
"simpleParam2"
],
"query": [
{
"key": "name",
"value": "Tom"
},
{
"key": "age",
"value": "10"
}
]
}
},
"response": []
},
{
"name": "@RequestParam注解映射",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/simpleParam3?username=Tom&age=10",
"host": [
"{{local}}"
],
"path": [
"simpleParam3"
],
"query": [
{
"key": "username",
"value": "Tom"
},
{
"key": "age",
"value": "10"
}
]
}
},
"response": []
}
]
},
{
"name": "原始方式=>HttpServletRequest(请求对象)",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/simpleParam?name=Tom&age=10",
"host": [
"{{local}}"
],
"path": [
"simpleParam"
],
"query": [
{
"key": "name",
"value": "Tom"
},
{
"key": "age",
"value": "10"
}
]
}
},
"response": []
}
]
},
{
"name": "实体参数",
"item": [
{
"name": "简单实体参数",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/simplePojo?name=Tom&age=10",
"host": [
"{{local}}"
],
"path": [
"simplePojo"
],
"query": [
{
"key": "name",
"value": "Tom"
},
{
"key": "age",
"value": "10"
}
]
}
},
"response": []
},
{
"name": "复杂实体参数",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/complexPojo?name=Tom&age=10&address.province=河北&address.city=石家庄",
"host": [
"{{local}}"
],
"path": [
"complexPojo"
],
"query": [
{
"key": "name",
"value": "Tom"
},
{
"key": "age",
"value": "10"
},
{
"key": "address.province",
"value": "河北"
},
{
"key": "address.city",
"value": "石家庄"
}
]
}
},
"response": []
}
]
},
{
"name": "数组集合参数",
"item": [
{
"name": "数组参数",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/arrayParam?name=12&name=36489",
"host": [
"{{local}}"
],
"path": [
"arrayParam"
],
"query": [
{
"key": "name",
"value": "12"
},
{
"key": "name",
"value": "36489"
}
]
}
},
"response": []
},
{
"name": "集合参数",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/listParam?name=12&name=36489",
"host": [
"{{local}}"
],
"path": [
"listParam"
],
"query": [
{
"key": "name",
"value": "12"
},
{
"key": "name",
"value": "36489"
}
]
}
},
"response": []
}
]
},
{
"name": "日期时间类型",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/dateParam?updateTime=2024-11-12",
"host": [
"{{local}}"
],
"path": [
"dateParam"
],
"query": [
{
"key": "updateTime",
"value": "2024-11-12"
}
]
}
},
"response": []
},
{
"name": "Json参数",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"name\":\"张三\",\r\n \"age\":29,\r\n \"address\":{\r\n \"province\":\"河北省\",\r\n \"city\":\"石家庄市\"\r\n }\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{local}}/jsonParam",
"host": [
"{{local}}"
],
"path": [
"jsonParam"
]
}
},
"response": []
},
{
"name": "路径参数",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{local}}/path/10185/物流",
"host": [
"{{local}}"
],
"path": [
"path",
"10185",
"物流"
]
}
},
"response": []
}
]
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [
""
]
}
}
],
"variable": [
{
"key": "local",
"value": "http://localhost:8080/",
"type": "string"
}
]
}
响应

package com.itheima.controller;
import com.itheima.pojo.entity.Address;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class ResponseController {
@RequestMapping(value ="hello")
public String hello(){
return "hello";
}
@RequestMapping(value ="getAddr")
public Address getAddr(){
Address address = new Address();
address.setProvince("广东省");
address.setCity("深圳市");
return address;
}
@RequestMapping(value ="listAddr")
public List<Address> listAddr(){
List<Address> list = new ArrayList<>();
Address address = new Address();
address.setProvince("广东省");
address.setCity("深圳市");
Address address2 = new Address();
address2.setProvince("广东省");
address2.setCity("广州市");
list.add(address);
list.add(address2);
return list;
}
}
统一响应
package com.itheima.pojo.entity;
public class Result {
private Integer code ;
private String msg;
private Object data;
public Result() {
}
public Result(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static Result success(Object data){
return new Result(1, "success", data);
}
public static Result success(){
return new Result(1, "success", null);
}
public static Result error(String msg){
return new Result(0, msg, null);
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", msg='" + msg + ''' +
", data=" + data +
'}';
}
}
package com.itheima.controller;
import com.itheima.pojo.entity.Address;
import com.itheima.pojo.entity.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class ResponseController {
@RequestMapping(value ="hello")
public Result hello(){
return Result.success("hello");
}
@RequestMapping(value ="getAddr")
public Result getAddr(){
Address address = new Address();
address.setProvince("广东省");
address.setCity("深圳市");
return Result.success(address);
}
@RequestMapping(value ="listAddr")
public Result listAddr(){
List<Address> list = new ArrayList<>();
Address address = new Address();
address.setProvince("广东省");
address.setCity("深圳市");
Address address2 = new Address();
address2.setProvince("广东省");
address2.setCity("广州市");
list.add(address);
list.add(address2);
return Result.success(list);
}
}