Java---SpringBoot详解一


人性本善亦本恶,  
喜怒哀乐显真情。  
寒冬暖夏皆有道,  
善恶终归一念间。  

善念慈悲天下广,  
恶行自缚梦难安。  
人心如镜自省照,  
善恶分明照乾坤。

目录

一,入门程序

①,创建springboot工程,并勾选web开发相关依赖

②,定义HelloController类,添加方法hello,并添加注解。

③,运行测试

二,请求响应

 ①简单请求

 ②实体参数请求

​编辑 ③数组集合参数

④日期参数

 ⑤Json参数

 ⑥路径参数


一,入门程序

先从一个小的案例开始,有这样一个需求,使用SpringBoot开发一个web应用,浏览器发起请求/hello 后,给浏览器返回字符串“Hello World”

①,创建springboot工程,并勾选web开发相关依赖

 

②,定义HelloController类,添加方法hello,并添加注解。

创建HelloController类:

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方法:

@SpringBootApplication
public class Demo3Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo3Application.class, args);
    }

}

 运行日志如下:

③,运行测试

在浏览器窗口输入http://localhost:8080/hello

 

同时日志输出:Hello World 

运行成功。

二,请求响应

总体框架如下:

 ①简单请求

还是按照上面步骤创建springboot工程:

 创建完成后,编写Controller类:

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  "请求成功";
    }
}

然后运行:

@SpringBootApplication
public class YzJavaTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(YzJavaTestApplication.class, args);
    }

}

之后通过postman请求测试:

 日志打印:

 ②实体参数请求

创建实体类User 和 Address

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;
    }


}
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中创建请求:

@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中测试:

日志打印:

 ③数组集合参数

在controller中创建请求:

@RestController
public class RequestController {

  
    @RequestMapping("/yzTestList")
    public String yzTestList(@RequestParam List<String> name){
        System.out.println("name:"+name);
        return  "请求成功";
    }
}

在postman中请求:

日志中输出:

注意:

④日期参数

 在controller中创建请求:

@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中请求:

日志打印:

 ⑤Json参数

注意:

 在controller中添加请求:

@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中使用:

日志打印输出:

 ⑥路径参数

 在controller中创建请求:


@RestController
public class RequestController {


    @RequestMapping("/yzTestPath/{name}")
    public String yzTestPath(@PathVariable String name){
        System.out.println("name:"+name);
        return  "请求成功";
    }
}

在postman中请求:

日志输出:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁震

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值