后端开发之使用postman工具接收高级数据详解及代码演示

数组请求参数

package com.bigdata.springboottext;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pojo.User;

import java.util.Arrays;

@RestController
public class RequestController {
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] hobby){
        //便于我们查看数组中的每一个元素
        System.out.println(Arrays.toString(hobby));
        return "OK";
    }
}

集合请求参数

package com.bigdata.springboottext;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.User;

import java.util.Arrays;
import java.util.List;

@RestController
public class RequestController {
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> hobby){
        //便于我们查看数组中的每一个元素
        System.out.println(hobby);
        return "OK";
    }
}

小结

日期参数

后端服务端接收数据的时候

所以 要指定接收的数据 格式是怎么样的

package com.bigdata.springboottext;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.User;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

@RestController
public class RequestController {
    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern ="yyyy-MM-dd  HH:mm:ss") LocalDateTime updateTime){
        System.out.println(updateTime);
        return "OK";
    }
}

注意

书写要格式化 要求空格严格遵循规则 在传入参数时多输入空格会报错

(本人曾经找了半小时的错误 后台一个ideal 一个postman 一个浏览器)

Json参数(重点)

json在前后端交互时使用频率非常高

前端程序如果传递比较复杂的数据就可以使用json

后端返回复杂数据也可以通过json

此时前端就要解析

一层一层的获取数据

postman发送请求时如何传输json格式的数据?

服务端又如何接收json数据?

package com.bigdata.springboottext;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
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.RestController;
import pojo.User;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

@RestController
public class RequestController {
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user){
        System.out.println(user);
        return "OK";
    }
}

路径参数

我们可以用路径参数绑定多个形参

package com.bigdata.springboottext;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import pojo.User;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

@RestController
public class RequestController {
    @RequestMapping("/path/{id}/{name}/{age}")
    //通过注解获取路径参数并且绑定给形参
    public String jsonParam(@PathVariable Integer id,@PathVariable String name,@PathVariable String age){
        System.out.println(id+" "+name+" "+age);
        return "OK";
    }
}

总结

  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值