1、springboot入门程序撰写并启动。
主要代码:
package com.caz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootWebQuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootWebQuickstartApplication.class, args);
}
}
运行结果
2、使用postman练习参数的获取。
2.1简单参数
package com.cazrequest.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 sompleParam(String name,Integer age){
System.out.println(name+":"+age);
return "OK";
}
}
get
post
2.2实体参数
简单实体参数
package com.cazrequest.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 +
'}';
}
}
复杂实体参数
package com.cazrequest.pojo;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String name) {
this.province= province;
}
public String getCity() {
return city;
}
public void setAge(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city=" + city +
'}';
}
}
2.3数组集合参数
数组
集合
2.4日期参数
2.5json参数
2.6路径参数
多个参数
3.案例
3.1
product.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新增产品界面</title>
</head>
<body style="font-size: 30px">
<form action="addProduct">
产品名称 :<input type="text" name="name" value=""><br />
产品价格: <input type="text" name="price" value=""><br />
<input type="submit" value="增加商品">
</form>
</body>
</html>
Product
package com.cazrequest.pojo;
public class Product {
private String name;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
ProductController
package com.cazrequest.controller;
import com.cazrequest.pojo.Product;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class ProductController {
@RequestMapping("/addProduct1")
public String simpleParam1(HttpServletRequest request) {
String name = request.getParameter("name");
String ageStr = request.getParameter("price");
int price = Integer.parseInt(ageStr);
System.out.println("addProduct1:"+name + " : " + price);
return "OK";
}
@RequestMapping("/addProduct")
public String simpleParam(String name , Integer price){
System.out.println("您输入的信息是:"+name+" : "+price);
return "OK";
}
@RequestMapping("/addProduct2")
public String simpleParam2(Product product){
System.out.println(product);
return "OK";
}
}
3.2
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单信息获取</title>
</head>
<body>
<form action="formCatch" method="post">
<ul style="list-style: none; line-height: 30px">
<li>
输入用户姓名:
<input type="text" name="name" />
<br />
</li>
<li>
选择性别:
<input name="sex" type="radio" value="boy" />
男
<input name="sex" type="radio" value="girl" />
女
</li>
<li>
选择密码提示问题:
<select name="question">
<option value="母亲生日">
母亲生日
</option>
<option value="宠物名称">
宠物名称
</option>
<option value="电脑配置">
电脑配置
</option>
</select>
</li>
<li>
请输入问题答案:
<input type="text" name="key" />
</li>
<li>
请选择个人爱好:
<div style="width: 400px">
<input name="like" type="checkbox" value="dance" />
唱歌跳舞
<input name="like" type="checkbox" value="web" />
上网冲浪
<input name="like" type="checkbox" value="hill" />
户外登山
<br />
<input name="like" type="checkbox" value="sports" />
体育运动
<input name="like" type="checkbox" value="reading" />
读书看报
<input name="like" type="checkbox" value="movie" />
欣赏电影
</div>
</li>
<li>
<input type="submit" value="提交" />
</li>
</ul>
</form>
</body>
</html>
Form
package com.cazrequest.pojo;
public class Form {
private String name;
private String sex;
private String question;
private String key;
private String[] like;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String[] getLike() {
return like;
}
public void setLike(String[] like) {
this.like = like;
}
}
FormController
package com.cazrequest.controller;
import com.cazrequest.pojo.Form;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FormController {
@RequestMapping("/formCatch")
public String simpleParam(Form form){
System.out.println("您输入的信息是:"+form);
return "OK";
}
}
运行结果