一、springboot入门程序撰写并启动
操作过程:创建一个新项目,选择Spring Initializr,Language选择Java,Type选择Mavenue,将域名选择为自己的域名,再选择jdk的版本等,最后选择web依赖,完成项目创建。在包名下创建一个类。开始编写处理请求类。
具体代码如下:
package edu.wust.springbootwebquickstart.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//请求处理类
@RestController
public class HelloController{
@RequestMapping("/hello")
public String Hello(){
System.out.println("Hello World!");
return "Hello World!"; //这个返回值是返回给浏览器的数据
}
}
再在SpringBootWebQuickstartApplication中点击运行,启动springboot工程。
在浏览器的地址栏中输入localhost:8080/hello 出现一下页面:
SPringleboot入门程序启动成功!
二、使用postman练习参数的获取
1、请求响应——请求-postman工具的入门程序
具体成功页面如下:
2、简单参数&实体参数:
package edu.wust.controller;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//测试请求参数接收
@Controller
public class RequestController {
//原始方式
// @RequestMapping("/simpleParam")
@ResponseBody
//public String simpleParam(HttpServletRequest request){
//获取请求参数
// String name=request.getParameter("name");
// String ageStr=request.getParameter("age");
// int age=Integer.parseInt(ageStr);
//System.out.println(name+":"+age);
//return "OK";
//}
//springboot方式
@RequestMapping("/simpleParam")
public String simpleParam(String name,Integer age){
System.out.println(name+":"+age);
return "OK";
}
}
原始方法在postman里面的运行界面:
spingboot方式在postman中的运行界面:
使用springboot方法,然后方法形参名称与请求参数名称不一致的情况 可以使用@RequestParam完成映射,(此时@RequestParam修饰的参数必须传递)具体如下:
代码:
package edu.wust.controller;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
//测试请求参数接收
@Controller
public class RequestController {
//原始方式
//@RequestMapping("/simpleParam")
//@ResponseBody
//public String simpleParam(HttpServletRequest request){
//获取请求参数
//String name=request.getParameter("name");
//String ageStr=request.getParameter("age");
//int age=Integer.parseInt(ageStr);
//System.out.println(name+":"+age);
//return "OK";
//}
//springboot方式
//@ResponseBody
// @RequestMapping("/simpleParam")
// public String simpleParam(String name,Integer age){
// System.out.println(name+":"+age);
// return "OK";
// }
//springboot方式然后方法形参名称与请求参数名称不一致的情况
//可以使用@RequestParam完成映射
@ResponseBody
@RequestMapping("/simpleParam")
public String simpleParam(@RequestParam(name = "name") String username, Integer age){
System.out.println(username+":"+age);
return "OK";
}
}
运行页面:
postman的页面:
简单实体参数的传递:(规则:请求参数名与形参属性名相同,即可直接通过POJO接收)
代码如下:
User类代码:
package edu.wust.pojo;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
RequestController类:
package edu.wust.controller;
import edu.wust.pojo.User;
import edu.wust.pojo.User;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class RequestController {
//2.实体方法
@ResponseBody
@RequestMapping("/simplePojo")
public String simplePojo(User user){
System.out.println(user);
return "OK!";
}
}
运行页面:
复杂实体参数的传递:
代码如下:
Address类代码:
package edu.wust.pojo;
public class Address {
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 "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}
User类代码:
package edu.wust.pojo;
import java.util.Objects;
public class User {
private String name;
private Integer age;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}
RequestController类:
package edu.wust.controller;
import edu.wust.pojo.User;
import edu.wust.pojo.User;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
//测试请求参数接收
@Controller
public class RequestController {
//原始方式
2.实体方法--复杂实体参数
@ResponseBody
@RequestMapping("/complexPojo")
public String complexPojo(User user){
System.out.println(user);
return "OK!";
}
}
运行页面:
3、数组集合参数的传递
数组参数传递:
具体代码如下:
package edu.wust.controller;
import edu.wust.pojo.User;
import edu.wust.pojo.User;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.lang.reflect.Array;
import java.util.Arrays;
//测试请求参数接收
@Controller
public class RequestController {
//3.数组集合参数
@ResponseBody
@RequestMapping("/arrayParam")
public String arrayParam(String[]hobby){
System.out.println(Arrays.toString(hobby));
return "OK!";
}
}
运行页面如下:
集合参数传递:
具体代码:
package edu.wust.controller;
import edu.wust.pojo.User;
import edu.wust.pojo.User;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
//测试请求参数接收
@Controller
public class RequestController {
//4.数组集合参数--集合参数(请求参数名与形参集合名称相同且请求参数为多个,@RequestParam绑定参数关系)
@ResponseBody
@RequestMapping("/listParam")
public String listParam(@RequestParam List<String> hobby){
System.out.println(hobby);
return "OK!";
}
}
运行页面:
3、日期时间参数传递:
具体代码:
package edu.wust.controller;
import edu.wust.pojo.User;
import edu.wust.pojo.User;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
//测试请求参数接收
@Controller
public class RequestController {
//5。日期时间参数:使用@DateTimeFormat注解完成日期参数格式转换,用用(pattern = "yyyy-MM-dd HH:mm:ss")指定前端传输过来的格式
@ResponseBody
@RequestMapping("/dateParam")
public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
System.out.println(updateTime);
return "OK!";
}
}
运行页面:
4、Json参数传递:
具体代码:
package edu.wust.controller;
import edu.wust.pojo.User;
import edu.wust.pojo.User;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
//测试请求参数接收
@Controller
public class RequestController {
//6.Json参数传递:Json数据键名与形参对象属性名相同,定义pojo类型形参即可接收参数,需要使用@RequestBody标识
@ResponseBody
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "OK!";
}
}
运行页面:
5、路径参数传递:
具体代码:
package edu.wust.controller;
import edu.wust.pojo.User;
import edu.wust.pojo.User;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
//测试请求参数接收
@Controller
public class RequestController {
//7、路径参数传递:通过请求URL直接传递参数,使用{...}来标识该路径参数,需要使用@pathVariable获取路径参数
@ResponseBody
@RequestMapping("/path/{id}")
public String pathParam(@PathVariable Integer id){
System.out.println(id);
return "OK!";
}
}
运行页面:
6、路径参数传递(多个)
具体代码:
package edu.wust.controller;
import edu.wust.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
//测试请求参数接收
@Controller
public class RequestController {
//8、路径参数传递(多个参数)
@ResponseBody
@RequestMapping("/path/{id}/{name}")
public String pathParam2(@PathVariable Integer id,@PathVariable String name){
System.out.println(id+":"+name);
return "OK!";
}
}
运行页面:
7、请求响应-响应
普通的响应
具体代码:
package edu.wust.controller;
import edu.wust.pojo.*;
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("/hello")
public String hello(){
System.out.println("Hello World ~");
return "Hello World ~";
}
@RequestMapping("/getAddr")
public Address getAddr(){
Address addr = new Address();
addr.setProvince("广东");
addr.setCity("深圳");
return addr; //返回值是一个对象,但responsebody会将对象转为json,然后再响应回去
}
@RequestMapping("/listAddr")
public List<Address> listAddr(){
List<Address> list = new ArrayList<>();
Address addr = new Address();
addr.setProvince("广东");
addr.setCity("深圳");
Address addr2 = new Address();
addr2.setProvince("陕西");
addr2.setCity("西安");
list.add(addr);
list.add(addr2);
return list; //返回值是一个集合,responsebody直接以json的格式响应回去
}
}
运行页面:
设置统一的响应结果:
具体代码:
package edu.wust.controller;
import edu.wust.pojo.*;
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("/hello")
public Result hello(){
System.out.println("Hello World ~");
//return new Result(1,"success","Hello World ~");
return Result.success("Hello World ~");
}
@RequestMapping("/getAddr")
public Result getAddr(){
Address addr = new Address();
addr.setProvince("广东");
addr.setCity("深圳");
return Result.success(addr);
}
@RequestMapping("/listAddr")
public Result listAddr(){
List<Address> list = new ArrayList<>();
Address addr = new Address();
addr.setProvince("广东");
addr.setCity("深圳");
Address addr2 = new Address();
addr2.setProvince("陕西");
addr2.setCity("西安");
list.add(addr);
list.add(addr2);
return Result.success(list);
}
}
运行页面:
8、请求响应-响应-案例
具体图片:
9、product请求响应
运行页面:
10、注册请求响应
运行页面: