wordpress 扒站,最近最新新闻,郴州网站制作公司,asp怎么样做网站后台文章目录 1.RequestMapping 注解介绍2. RequestMapping 使用3. RequestMapping 是 GET 还是 POST 请求#xff1f;GET 请求POST 请求指定 GET/POST 方法类型 2. Postman 介绍1. 创建请求2. 传参介绍1. 普通传参2. form-data3. x-www-form-urlencoded form 表单#xff0c;对应… 文章目录 1.RequestMapping 注解介绍2. RequestMapping 使用3. RequestMapping 是 GET 还是 POST 请求GET 请求POST 请求指定 GET/POST 方法类型 2. Postman 介绍1. 创建请求2. 传参介绍1. 普通传参2. form-data3. x-www-form-urlencoded form 表单对应 Content-Type: application/x-www-from-urlencoded4. raw 1.RequestMapping 注解介绍
RequestMapping 是 Spring Web MVC 引用程序中最常被用到的注解之一它是用来注册接口的路由映射的表示服务收到请求时路径为 /sayHello 的请求就会调用 sayHi 这个方法的代码
路由映射当用户访问一个 URL 时将用户的请求对应到程序中某个类的某个方法的过程就叫路由映射 既然 RequestMapping 已经可以达到我们的目的了我们为什么还要加 RestController 呢
我们把 RestController 去掉再来访问一次可以看到程序报了 404找不到该页面这就是 RestController 起到的作用 一个项目中会有很多类每个类可能会有很多的方法Spring 程序怎么知道要执行哪个方法呢
Spring 会对所有的类进行扫描如果类加了注解 RestControllerSpring 才会去看这个类里面的方法有没有加 RequestMapping 这个注解 2. RequestMapping 使用
RequestMapping 既可修饰方法也可修饰类。 当修饰类和方法时访问的地址是类路径方法路径 RequestMapping 标识一个类设置映射请求的请求路径的初识信息RequestMapping 标识一个方法设置映射请求请求路径的具体信息 import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; RequestMapping(/user)
RestController
public class UserController { RequestMapping(/sayHello) public String sayHi() { return hello, spring mvc; }
}访问地址 http://127.0.0.1:8080/user/sayHello 注意RequestMapping 的 URL 路径最前面加不加 / 都可以Spring 程序启动时会进行判断如果前面没有 /Spring 会拼接上一个 / 通常情况下我们加上 /RequestMapping 的 URL 路径也可以是多层的最终访问时依然是类路径方法路径
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; RequestMapping(/user/m1)
RestController
public class UserController { RequestMapping(/sayHello) public String sayHi() { return hello, spring mvc; }
}3. RequestMapping 是 GET 还是 POST 请求
我们来测试一下就知道了
GET 请求
浏览器发送的请求类型都是 GET通过以上案例可以看出来 RequestMapping 支持 GET 请求
POST 请求
我们通过 form 表单来构造请求 创建 test.htmlHTML代码
!DOCTYPE html
html langen
head meta charsetUTF-8 titleTitle/title
/head
body form actionuser/sayHello methodpost input typesubmit value提交 /form
/body
/html前端代码放在 static 目录下访问方式为 http://127.0.0.1:8080/test.html 如果有多层目录访问链接从 static 目录开始写如上图访问链接为127.0.0.1:8080/html/test.html 从运行结果可以看出RequestMapping 既支持 GET 请求又支持 POST 请求。同理也支持其他的请求方式那如何指定 GET 或者 POST 类型呢 指定 GET/POST 方法类型
我们可以显示指定的 RequestMapping 来接收 POST 的情况如下所示
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; RestController
public class UserController { RequestMapping(value /getRequest, method RequestMethod.POST) public String sayHi() { return hello, spring mvc; }
}2. Postman 介绍
从上面的案例中发现了一个新的问题就是我们测试后端方法时还需要去写前端代码。这对我们来说是一件麻烦又痛苦的事情。
随着互联网的发展也随着项目难度的增加企业也按照开发的功能把人员拆分成了不同的团队。界面显示交给“前端开发工程师”业务逻辑的实现交给了“后端开发工程师”。后端开发工程师不要求也不需要掌握前端的技能了。
那后端开发工程师如何测试自己的程序呢使用专业的接口测试工具—— Postman 1. 创建请求 界面介绍 2. 传参介绍
1. 普通传参
也就是通过查询字符串来传参
学习 HTTP 的时候我们通过 URL 来访问互联网上的某一个资源URL 的格式如下 其中查询字符串就是请求的参数 2. form-data
完整表示为multipart/form-data。表单提交的数据在 form 标签中加上 enctypedmultipart/form-data通常用于提交图片/文件。对应 Content-Type: multipart/form-data 3. x-www-form-urlencoded
form 表单对应 Content-Type: application/x-www-from-urlencoded
4. raw
可以上传任意格式的文本可以上传 text、json、xml、html 等