Table of contents
Postman
Postman is a powerful Chrome plug-in for debugging web pages and sending web page http requests. It is often used for interface testing.
Use of Postman
Create user first
Then log in through the user you just created
Create workspace
After the creation is completed, it will automatically enter the springbootweb workspace.
Click +Add new request
ask
The pom.xml file is as follows
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.he1220</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
simple parameters
Receive request parameters in raw form
The directory structure is as follows
The contents of the RequestController.java file are as follows
package com.he1220.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class RequestController {
@RequestMapping("/simpleParam")
public String simpleParam(HttpServletRequest request){
String name = request.getParameter("name");
String age = request.getParameter("age");
System.out.println(name);
System.out.println(age);
return "ok";
}
}
The test results are as follows
Receive request parameters in SpringBoot way
@RestController
public class RequestController {
@RequestMapping("/simpleParam")
public String simpleParam(String name,Integer age){
System.out.println(name);
System.out.println(age);
return "ok";
}
}
If the method parameter name does not match the request parameter name, the mapping can be completed through the @RequestParam annotation.
Specify the request parameter name through the name attribute
The Required attribute in @RequestParam defaults to true. The request parameter must be passed. If not passed, an error (400) will be reported. Can be set to false
@RestController
public class RequestController {
@RequestMapping("/simpleParam")
public String simpleParam(@RequestParam(name="name")String username,Integer age){
System.out.println(username);
System.out.println(age);
return "ok";
}
}
Entity parameters
It is necessary to ensure that the request parameter name is consistent with the formal parameter object attribute name.
The directory structure is as follows
RequestController.java
@RestController
public class RequestController {
@RequestMapping("/Pojo")
public String Pojo(User user){
System.out.println(user);
return "ok~";
}
}
User.java
package com.he1220.demo.pojo;
public class User{
private String name;
private String age;
private Add add;
public User(String name, String age) {
this.name = name;
this.age = age;
}
public Add getAdd() {
return add;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAdd(Add add) {
this.add = add;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", add=" + add +
'}';
}
}
Add.java
package com.he1220.demo.pojo;
public class Add {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Add{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}
The test results are as follows
Array parameters
RequestController.java
@RestController
public class RequestController {
@RequestMapping("/arrParam")
public String arrParam(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "ok~";
}
}
The test results are as follows
Collection parameters
It is necessary to ensure that the parameter name of the request is the same as the collection variable name, and the parameter relationship is bound through the @RequestParam annotation.
RequestController.java
@RestController
public class RequestController {
@RequestMapping("/listParam")
public String listParam(@RequestParam List<String> hobby){
System.out.println(hobby);
return "ok~";
}
}
date parameter
Use @DateTimeFormat annotation to complete date parameter format conversion
@RestController
public class RequestController {
@RequestMapping("/dateParam")
public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
System.out.println(updateTime);
return "ok~";
}
Json parameters
Encapsulate the Json object into the entity class through the @RequestBody annotation
@RestController
public class RequestController {
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "ok~";
}
}
path parameters
Pass parameters directly through the request URL. Use {...} to identify the parameter path. You need to use @PathVariable to obtain the parameter path.
@RestController
public class RequestController {
@RequestMapping("/path/{id}/{name}")
public String pathParam(@PathVariable Integer id,@PathVariable String name){
System.out.println(id + name);
return "ok~";
}
}
response
@ResponseBody
Put it on the Controller method/class
Respond directly to the method return value. If the return value type is an entity object/collection, it will be converted into a JSON format response.
@RestController = @Controller + @ResponseBody
Unified response results
The directory structure is as follows
pojo.Result.java
package com.he1220.demo.pojo;
/**
* 统一响应结果封装类
*/
public class Result {
private Integer code ;//1 成功 , 0 失败
private String msg; //提示信息
private Object data; //数据 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 +
'}';
}
}
The content of RequestController.class is as follows
@RestController
public class RequestController {
@RequestMapping("/getAddr")
public Result getAddr(String province, String city){
System.out.println(province);
System.out.println(city);
Add addr = new Add();
addr.setProvince(province);
addr.setCity(city);
return Result.success(addr);
}
}
The test results are as follows