1.前言
哈喽大家好,今天来给大家带来spring相关的学习,主要内容有概念的讲解以及如何分别通过java代码和工具postman来建立连接,那么话不多说让我们开始吧。
2.正文
2.1springmvc是什么
spring mvc 是 spring 框架的一个模块,专门用于构建 web 应用程序。它基于经典的 mvc 设计模式(model-view-controller),将应用程序分为三层:
- model(模型):封装数据(如数据库查询结果)。
- view(视图):展示数据(如 html 页面)。
- controller(控制器):处理用户请求,协调 model 和 view。
官方文档:
spring web mvc :: spring framework
https://docs.spring.io/spring-framework/reference/web/webmvc.html
使用它有哪些优点呢:
- 简化开发:通过注解和配置减少代码量。
- 灵活扩展:支持多种视图技术(jsp、thymeleaf 等)。
- 与 spring 生态无缝集成:如 spring security、spring data 等。
那么springmvc是如何处理用户发送过来的http请求呢:
dispatcherservlet(前端控制器)
- 所有请求的入口,相当于“调度中心”。
- 负责将请求分发给对应的 controller。
handlermapping(处理器映射器)
- 根据请求的 url,找到对应的 controller 和方法。
controller(控制器)
- 处理业务逻辑(如查询数据库),返回 model 和视图名称。
viewresolver(视图解析器)
- 将视图名称(如
"home"
)解析为具体的视图(如/web-inf/home.jsp
)。
view(视图)
- 渲染页面,将 model 数据展示给用户。
流程:
用户请求 → dispatcherservlet → handlermapping → controller → 返回 modelandview → viewresolver → view → 响应给用户
2.2详解@requestmapping注解
@requestmapping
是 spring mvc 中最核心的注解之一,用于将 http 请求映射到具体的控制器方法。它支持灵活的 url 匹配、请求方法限制、参数过滤等功能。(学习这一部分时,建议搭配后文使用)
基本用法:在类或方法上使用 @requestmapping
,指定 url 路径:
@controller @requestmapping("/user") // 类级别的公共路径,类路径 public class usercontroller { @requestmapping("/info") // 完整路径是 /user/info,方法路径 public string getuserinfo() { return "user_info"; } }
那么该注解到底是get请求还是post请求呢?这个时候我们就要来借助以下抓包工具:
测试代码:
package com.example.demo; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class test1 { @requestmapping("/hello") public string hello(){ return "hello"; } }
此时可以看出来是get请求。那有没有可能是post请求呢?让我们来测试下:
我们在static创建一个html文件:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>title</title> </head> <body> <form action="/hello" method="post"> <input type = "submit" value = "传送"> </form> </body> </html>
就会发现此时是post请求 。
那么我们如何定义该注解是get请求还是post请求呢?
@requestmapping(method=get) @requestmapping(method=post)
利用method设置一下即可。
总结下核心属性:
属性 | 作用 | 示例 |
---|---|---|
value 或 path | 指定 url 路径 | @requestmapping("/user") |
method | 限制请求方法(get、post 等) | method = requestmethod.post |
params | 要求请求必须包含特定参数 | params = "id=100" (参数 id 必须为 100) |
headers | 要求请求头包含特定字段 | headers = "content-type=text/json" |
consumes | 限制请求的 content-type(如接收 json) | consumes = "application/json" |
produces | 指定响应的 content-type(如返回 json) | produces = "application/json" |
2.3创建spring项目
既然我们学习了该注解,那么就要想办法应用出来,所以我们们需要先创建一个spring项目
在idea社区版装上spring插件或者使用spring专业版,创建项目时勾选:
接着导入springweb相关模块:
这样就完成了spring项目的创建。
2.4建立连接
下面我们在idea中建立连接:
package com.example.demo; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class test1 { @requestmapping("/hello") public string hello(){ return "hello"; } }
资源路径:类路径+方法路径。
核心注解说明:
1.
@restcontroller
- 是
@controller
+@responsebody
的组合注解。- 作用:
- 标记这个类是一个控制器,能接收 http 请求。
- 所有方法的返回值会直接写入 http 响应体(response body),而不是跳转到视图页面。
- 适合场景:开发 restful api(返回 json/xml 数据)。
2.
@requestmapping("/hello")
- 作用:将 http 请求路径
/hello
映射到hello()
方法。- 默认行为:
- 支持所有 http 方法(get、post、put 等)。
- 返回的字符串 "hello" 会直接作为响应内容返回给客户端。
当我们运行项目并在浏览器访问访问 http://localhost:8080/hello时:
这样就代表连接成功。
如果注解相同,那么就必须保证资源路径(类路径+方法路径)不同:
package com.example.demo; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class test1 { @requestmapping("/hello") public string hello(){ return "hello"; } @requestmapping("/hello") public string hello1(string name){ return "hello" + name; } }
项目直接启动报错并退出:
红线标注部分“error”就是错误日志了。
package com.example.demo; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class test1 { @requestmapping("/hello") public string hello(){ return "hello"; } @requestmapping("/hello1") public string hello1(string name){ return "hello" + name; } }
这样即可正常运行:
2.5postman
官网:download postman | get started for free
https://www.postman.com/downloads/
postman是什么呢:
postman 是一款api 开发与测试工具,被广泛用于设计、调试、测试和文档化 http 接口。无论是后端开发者、测试工程师,还是前端开发者,都可以用它快速验证 api 的功能和可靠性。
- 发送 http 请求(get/post/put/delete 等)。
- 自动化测试 api 接口。
- 管理 api 集合(分类、分组、共享)。
- 生成 api 文档。
- 模拟服务器(mock server)。
- 团队协作(共享工作区)。
页面看起来还是很清新的。
就还拿上一段代码来测试,第二个方法还有一个参数name,我们在postman里可以快捷在请求中添加参数,注意参数名一定要相同。
发送成功。
3.小结
今天的分享到这里就结束了,喜欢的小伙伴点点赞点点关注,你的支持就是对我最大的鼓励,大家加油!
到此这篇关于springmvc 概念引入与连接的文章就介绍到这了,更多相关springmvc 概念引入与连接内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论