数组请求参数
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"; } }
总结