人性本善亦本恶,
喜怒哀乐显真情。
寒冬暖夏皆有道,
善恶终归一念间。善念慈悲天下广,
恶行自缚梦难安。
人心如镜自省照,
善恶分明照乾坤。
目录
②,定义HelloController类,添加方法hello,并添加注解。
[编辑 ③数组集合参数](#编辑 ③数组集合参数)
一,入门程序
先从一个小的案例开始,有这样一个需求,使用SpringBoot开发一个web应用,浏览器发起请求/hello 后,给浏览器返回字符串"Hello World"
①,创建springboot工程,并勾选web开发相关依赖
![](https://i-blog.csdnimg.cn/direct/28481110feec481c9a72db444cc8052e.png)
![](https://i-blog.csdnimg.cn/direct/d301f3281a074d26bac0b7bb7c6c8ff2.png)
![](https://i-blog.csdnimg.cn/direct/19da409f64d0408586bc04cde0e61814.png)
②,定义HelloController类,添加方法hello,并添加注解。
创建HelloController类:
java
package com.yuanzhen.demo3.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";
}
}
运行Demo3Application的main方法:
java
@SpringBootApplication
public class Demo3Application {
public static void main(String[] args) {
SpringApplication.run(Demo3Application.class, args);
}
}
运行日志如下:
![](https://i-blog.csdnimg.cn/direct/4309588617884f768e5416aec4b609a1.png)
③,运行测试
在浏览器窗口输入http://localhost:8080/hello
![](https://i-blog.csdnimg.cn/direct/c2eac1bd7c2d481cb10dc24e85e2d2c8.png)
同时日志输出:Hello World
运行成功。
二,请求响应
总体框架如下:
![](https://i-blog.csdnimg.cn/direct/a9daf3fcf73543788919e06c74b9416f.png)
![](https://i-blog.csdnimg.cn/direct/3290ed2bbc9b4812bf012162df8e2891.png)
①简单请求
还是按照上面步骤创建springboot工程:
![](https://i-blog.csdnimg.cn/direct/27f59f4331524bef8f6122b4fe48125f.png)
创建完成后,编写Controller类:
java
package com.yuanzhen.yzjavatest.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestController {
@RequestMapping("/yuanzhen")
public String yzTest(String name,Integer age){
System.out.println("name:"+name+",age:"+age);
return "请求成功";
}
}
然后运行:
java
@SpringBootApplication
public class YzJavaTestApplication {
public static void main(String[] args) {
SpringApplication.run(YzJavaTestApplication.class, args);
}
}
之后通过postman请求测试:
![](https://i-blog.csdnimg.cn/direct/28e0890e0b914d389069637f5c2b7716.png)
日志打印:
![](https://i-blog.csdnimg.cn/direct/98488d147fe64dd59f0d1ea573e879fd.png)
②实体参数请求
创建实体类User 和 Address
java
public class User {
private String name;
private Integer age;
private Address address;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setAddress(Address address) {
this.address = address;
}
public Address getAddress() {
return address;
}
public Integer getAge() {
return age;
}
public String getName() {
return name;
}
}
java
public class Address {
private String province;
private String city;
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
public void setProvince(String province) {
this.province = province;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public String getProvince() {
return province;
}
}
在controller中创建请求:
java
@RestController
public class RequestController {
@RequestMapping("/yuanzhen")
public String yzTest(String name,Integer age){
System.out.println("name:"+name+",age:"+age);
return "请求成功";
}
@RequestMapping("/yzTestBean")
public String yzTestBean(User user){
System.out.println("user:"+user);
return "请求成功";
}
}
运行程序
在postman中测试:
![](https://i-blog.csdnimg.cn/direct/0c30044c533940dfa3810b154333fd6d.png)
日志打印:
③数组集合参数
在controller中创建请求:
java
@RestController
public class RequestController {
@RequestMapping("/yzTestList")
public String yzTestList(@RequestParam List<String> name){
System.out.println("name:"+name);
return "请求成功";
}
}
在postman中请求:
![](https://i-blog.csdnimg.cn/direct/6959801f7d9442719c37fc8ecbd4cf07.png)
日志中输出:
![](https://i-blog.csdnimg.cn/direct/396431dc4be74b2f95e8d6952d369b00.png)
注意:
![](https://i-blog.csdnimg.cn/direct/14c3ee55decc47a08e87d9f432a599ea.png)
④日期参数
在controller中创建请求:
java
@RestController
public class RequestController {
@RequestMapping("/yzTestDate")
public String yzTestDate(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time){
System.out.println("time:"+time);
return "请求成功";
}
}
在postman中请求:
![](https://i-blog.csdnimg.cn/direct/e1deeb9e8f2349109ed8b810967147c7.png)
日志打印:
![](https://i-blog.csdnimg.cn/direct/9d19d843ac00451eb6c165f14ea08ed0.png)
⑤Json参数
注意:
![](https://i-blog.csdnimg.cn/direct/c5712fa21774470388cd6e0a44e6114a.png)
在controller中添加请求:
java
@RestController
public class RequestController {
@RequestMapping("/yuanzhen")
public String yzTest(String name,Integer age){
System.out.println("name:"+name+",age:"+age);
return "请求成功";
}
@RequestMapping("/yzTestBean")
public String yzTestBean(User user){
System.out.println("user:"+user);
return "请求成功";
}
@RequestMapping("/yzTestList")
public String yzTestList(@RequestParam List<String> name){
System.out.println("name:"+name);
return "请求成功";
}
@RequestMapping("/yzTestDate")
public String yzTestDate(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time){
System.out.println("time:"+time);
return "请求成功";
}
@RequestMapping("/yzTestJson")
public String yzTestJson(@RequestBody User user){
System.out.println("user:"+user);
return "请求成功";
}
}
在postman中使用:
![](https://i-blog.csdnimg.cn/direct/a7eb069876ca4187ba5e150114cef23c.png)
日志打印输出:
![](https://i-blog.csdnimg.cn/direct/40958d6c8be047cdaa2d85ec0598a664.png)
⑥路径参数
![](https://i-blog.csdnimg.cn/direct/8cb5323cb57d4226aa57ae6d62e13f59.png)
在controller中创建请求:
java
@RestController
public class RequestController {
@RequestMapping("/yzTestPath/{name}")
public String yzTestPath(@PathVariable String name){
System.out.println("name:"+name);
return "请求成功";
}
}
在postman中请求:
![](https://i-blog.csdnimg.cn/direct/8af9f88c60ee41c1ba9b2d570a766e89.png)
日志输出:
![](https://i-blog.csdnimg.cn/direct/713e71309cfd44e2a463c84f568af313.png)