请求响应-请求

项目运行和重构

每次修改代码后,都要重构。

软件:idea、apipost

简单参数请求

get方式

@RequestMapping("/xxx")
public String xxx(String xxx,Integer xxx) {
    ...;
    return ...;
}
package com.start.springbootstart.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//请求处理类
@RestController
public class HelloController {
    @RequestMapping("/Sp1")
    public String Sp1(String name,Integer age) {
        System.out.println(name+":"+age);
        return "ok";
    }

}

post方式

映射关系

当传递参数与所需参数,名字不一样的时候,会返回null。

 可以通过如下字符进行改变。

@RequestParam(name="....")

但是当加入这个,默认request=true,代表该参数必须传递,否则会出错。

所以我们可以添加如下

(@RequestParam(name="...",required = false)

实体参数请求

把各个参数进行封装。

生成get()和set()方法,在{}中间右键:generate-get() set()

还有记得生成toString方法,在{}中间右键:generate-toString

如果没有生成toString方法,会显示地址,如下

com.start.springbootstart.Controller.Pojo.User@13d595fc

代码

package com.start.springbootstart.Pojo;

public class User1 {
    private String name;
    private Integer age;
    private Address address;

    @Override
    public String toString() {
        return "User1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", 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;
    }


}


package com.start.springbootstart.Pojo;

public class Address {
    private String city;
    private String street;

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", street='" + street + '\'' +
                '}';
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }


}

package com.start.springbootstart.Controller;

import com.start.springbootstart.Pojo.User1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;

//请求处理类
@RestController
public class HelloController {

    @RequestMapping("/Sp1")
    public String Sp1(@RequestParam(name="name",required = false) String username, Integer age) {
        System.out.println(username+":"+age);
        return "ok";
    }

    @RequestMapping("/Sp2")
    public String Sp2(User1 user1) {
        System.out.println(user1);
        return "ok";
    }


}

数组集合参数

记得要导入包

import java.util.Arrays;
 
   @RequestMapping("/Sp3")
    public String Sp3(String[] hh) {
        System.out.println(Arrays.toString(hh));
        return "ok";
    }

集合参数请求

记得要导入包。

请求参数名要与形参中集合变量名一致,并且通过@RequestParam进行绑定参数关系,封装数据。

import java.util.List;


//    集合参数请求
    @RequestMapping("/Sp4")
    public String Sp4(@RequestParam List<String> hh) {
        System.out.println(hh);
        return "ok4";
    }

日期时间参数请求

日期每个位必要时记得补0,才不会报错。

@DateTimeFormat(pattern ="yyyy-MM-dd HH:mm:ss")//指定传递日期的模板
//    日期时间参数
    @RequestMapping("/Sp5")
    public String Sp5(@DateTimeFormat(pattern ="yyyy-MM-dd HH:mm:ss") LocalDateTime dd) {
        System.out.println(dd);
        return "ok5";
    }

JSON参数

json的键名要和形参对象属性名相同,需要使用@RequestBody标识。

    @RequestMapping("/Sp6")
    public String Sp6(@RequestBody User1 user1) {
        System.out.println(user1);
        return "ok6";
    }

路径参数

通过请求URL直接传递参数,使用{}来标识该路径参数,需要使用@PathVariable获取路径参数。

//  路径参数
    @RequestMapping("/Sp7/{id}")
    public String Sp7(@PathVariable Integer id) {
        System.out.println(id);
        return "ok7";
    }

进行多个参数传递,可推广。

//  多个参数传递
    @RequestMapping("/Sp8/{id}/{name}")
    public String Sp8(@PathVariable Integer id, @PathVariable String name) {
        System.out.println(id+":"+name);
        return "ok8";
    }

总代码

package com.start.springbootstart.Controller;

import com.start.springbootstart.Pojo.User1;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

//请求处理类
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        System.out.println("hello controller~");
        return "hello controller~";
    }

    @RequestMapping("/Sp1")
    public String Sp1(@RequestParam(name="name",required = false) String username, Integer age) {
        System.out.println(username+":"+age);
        return "ok";
    }
//    实体参数请求
    @RequestMapping("/Sp2")
    public String Sp2(User1 user1) {
        System.out.println(user1);
        return "ok";
    }
//  数组参数请求
    @RequestMapping("/Sp3")
    public String Sp3(String[] hh) {
        System.out.println(Arrays.toString(hh));
        return "ok";
    }
//    集合参数请求
    @RequestMapping("/Sp4")
    public String Sp4(@RequestParam List<String> hh) {
        System.out.println(hh);
        return "ok4";
    }
//    日期时间参数
    @RequestMapping("/Sp5")
    public String Sp5(@DateTimeFormat(pattern ="yyyy-MM-dd HH:mm:ss") LocalDateTime dd) {
        System.out.println(dd);
        return "ok5";
    }
//    json参数
    @RequestMapping("/Sp6")
    public String Sp6(@RequestBody User1 user1) {
        System.out.println(user1);
        return "ok6";
    }
//  路径参数
    @RequestMapping("/Sp7/{id}")
    public String Sp7(@PathVariable Integer id) {
        System.out.println(id);
        return "ok7";
    }
//  多个参数传递
    @RequestMapping("/Sp8/{id}/{name}")
    public String Sp8(@PathVariable Integer id, @PathVariable String name) {
        System.out.println(id+":"+name);
        return "ok8";
    }


}

如果你觉得本文对你有用的话,请随意打赏~

  • 16
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值