spingboot如何接受前端请求

首先我们是否用的是rest风格开发的的都是适用的.

普通参数

get 请求

发送方

注:由于是get请求不用body(json)接收.

spingboot如何接受前端请求_第1张图片

接受方

spingboot如何接受前端请求_第2张图片

post请求

发送端

注意:在请求体(body)里面用x-www-from-urlencoded(不仅可以发请求,还可以发文件)

spingboot如何接受前端请求_第3张图片

接受体没有

spingboot如何接受前端请求_第4张图片

5种不同参数类型的传递

    • 普通参数[简单数据]:参数不同名

注意看,我们上面网址传递的参数名字与接受参数的名字是相同的,如果不相同需要另外的代码来j映射.

发送端:

spingboot如何接受前端请求_第5张图片

接收代码:

spingboot如何接受前端请求_第6张图片
    • 实体类参数

接受方:

可以直接封装进去

spingboot如何接受前端请求_第7张图片

3. 属性名里面引用别的属性名

spingboot如何接受前端请求_第8张图片

接受方

spingboot如何接受前端请求_第9张图片

4. 数组参数

发送到参数名字与数组名相同.否则不行.

spingboot如何接受前端请求_第10张图片

spingboot如何接受前端请求_第11张图片

5. 集合类型

错误案例

发送方:

spingboot如何接受前端请求_第12张图片

接受方

spingboot如何接受前端请求_第13张图片

报错:

五月11,2e211g:13:39上午org.apache.catalina.core.StandardWrapperValve invoke
Servlet.service()for servlet [dispatcher]in context with path [threw exception [Request processing failed;nested exceptic
java.lang.NoSuchMethodException Create breakpoint java.util.List.()

死兵不得再尝试造一个list类型的对象并把接收到的信息放进去。然后我们只是想让web中的数据作为集合中的数据出现.

解决方法

加入@requestparam,把参数传进去.

spingboot如何接受前端请求_第14张图片

json格式(重点)

第一步

spingboot如何接受前端请求_第15张图片

2.

spingboot如何接受前端请求_第16张图片

3. 在spingmvcconfig配置里面加

@EnableWebMvc

作用由json数据转化成我们的对象。

spingboot如何接受前端请求_第17张图片

发送方

spingboot如何接受前端请求_第18张图片

接收方

spingboot如何接受前端请求_第19张图片

含应用类型的参数

spingboot如何接受前端请求_第20张图片
数组类型
spingboot如何接受前端请求_第21张图片
spingboot如何接受前端请求_第22张图片

总结

  1. @RequestParam把名字对不上的加上. (用于请求体参数,用不了json)

  1. @requestbody 用于接受json类型参数.

关于请求路径是否有参数疑惑

有参数类型,

spingboot如何接受前端请求_第23张图片

无参数类型

spingboot如何接受前端请求_第24张图片

组合类型

spingboot如何接受前端请求_第25张图片

总结: 发送端有变量名的可以不用写在映射路径里面。没有的一定要写。

你可能感兴趣的:(java项目经验,java,spring,boot,后端)