在 Spring Boot 中,@Controller 注解是专门用于处理 Http 请求处理的,是以 MVC 为核心的设计思想的控制层。@RestController 则是 @Controller 的衍生注解。
1 Spring Boot Controller
1.1 原理
Spring Boot 本身就 Spring MVC 的简化版本。是在 Spring MVC 的基础上实现了自动配置,简化了开发人员开发过程。
Spring MVC 是通过一个叫 DispatcherServlet
前端控制器的来拦截请求的。而在 Spring Boot 中 使用自动配置把 DispatcherServlet
前端控制器自动配置到框架中。
例如,我们来解析 /users 这个请求
DispatcherServlet
前端控制器拦截请求 /usersservlet
决定使用哪个handler
处理- Spring 检测哪个控制器匹配
/users
,Spring 从 @RquestMapping 中查找出需要的信息 - Spring 找到正确的 Controller 方法后,开始执行 Controller 方法
- 返回 users 对象列表
- 根据与客户端交互需要返回 Json 或者 Xml 格式
1.2 相关注解
在 Spring Boot 中使用到 @Controller 及相关的注解如下,主要分为三个层面进行,请求前,处理中,返回。
应用场景 | 注解 | 注解说明 |
---|---|---|
处理请求 | @Controller | 处理 Http 请求 |
处理请求 | @RestController | @Controller 的衍生注解 |
路由请求 | @RequestMapping | 路由请求 可以设置各种操作方法 |
路由请求 | @GetMapping | GET 方法的路由 |
路由请求 | @PostMapping | POST 方法的路由 |
路由请求 | @PutMapping | PUT 方法的路由 |
路由请求 | @DeleteMapping | DELETE 方法的路由 |
请求参数 | @PathVariable | 处理请求 url 路径中的参数 /user/{id} |
请求参数 | @RequestParam | 处理问号后面的参数 |
请求参数 | @RequestBody | 请求参数以json格式提交 |
返回参数 | @ResponseBody | 返回 json 格式 |
注意以上注解需要强调的是 @RestController 是 @Controller 的子集。
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping 是 @RequestMapping 的子集。所以实际上我们只需要掌握 @Controller 和 @RequestMapping 就可以了。
1.3 @Controller 与 @RestController 区别
@Controller 包括了 @RestController。@RestController 是 Spring4 后新加的注解,从 RestController 类源码可以看出 @RestController 是 @Controller 和 @ResponseBody 两个注解的结合体。
@Controller=@RestController+@ResponseBody
如下 @RestController 的源码可以看出他们的关系
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#2b91af">@Target({ElementType.TYPE})</span>
<span style="color:#2b91af">@Retention(RetentionPolicy.RUNTIME)</span>
<span style="color:#2b91af">@Documented</span>
<span style="color:#2b91af">@Controller</span>
<span style="color:#2b91af">@ResponseBody</span>
<span style="color:#0000ff">public</span> <span style="color:#2b91af">@interface</span> RestController {
<span style="color:#2b91af">@AliasFor(
annotation = Controller.class
)</span>
String <span style="color:#a31515">value</span>() <span style="color:#0000ff">default</span> <span style="color:#a31515">""</span>;
}
</code></span></span>
1.4 @Controller 与 @RestController应用场景
-
@Controller 一般应用在有返回界面的应用场景下.
例如,管理后台使用了 thymeleaf 作为模板开发,需要从后台直接返回 Model 对象到前台,那么这时候就需要使用 @Controller 来注解。
-
@RestController 如果只是接口,那么就用 RestController 来注解.
例如前端页面全部使用了 Html、Jquery来开发,通过 Ajax 请求服务端接口,那么接口就使用 @RestController 统一注解。
1.5 @RequestMapping 说明
首先我们来看看 @RequestMapping 的源码,我在上面加了注释
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#2b91af">@Target({ElementType.TYPE, ElementType.METHOD})</span>
<span style="color:#2b91af">@Retention(RetentionPolicy.RUNTIME)</span>
<span style="color:#2b91af">@Documented</span>
<span style="color:#2b91af">@Mapping</span>
<span style="color:#0000ff">public</span> <span style="color:#2b91af">@interface</span> RequestMapping {
String <span style="color:#a31515">name</span>() <span style="color:#0000ff">default</span> <span style="color:#a31515">""</span>;
<span style="color:#008000">//指定请求的实际地址</span>
<span style="color:#2b91af">@AliasFor("path")</span>
String[] value() <span style="color:#0000ff">default</span> {};
<span style="color:#2b91af">@AliasFor("value")</span>
String[] path() <span style="color:#0000ff">default</span> {};
<span style="color:#008000">//指定请求的method类型, GET、POST、PUT、DELETE等</span>
RequestMethod[] method() <span style="color:#0000ff">default</span> {};
<span style="color:#008000">//指定request中必须包含某些参数值是,才让该方法处理。</span>
String[] params() <span style="color:#0000ff">default</span> {};
<span style="color:#008000">//指定request中必须包含某些指定的header值,才能让该方法处理请求。</span>
String[] headers() <span style="color:#0000ff">default</span> {};
<span style="color:#008000">//指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;</span>
String[] consumes() <span style="color:#0000ff">default</span> {};
<span style="color:#008000">//指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;</span>
String[] produces() <span style="color:#0000ff">default</span> {};
}
</code></span></span>
示例说明:
示例 | 说明 |
---|---|
@RequestMapping("/index") | 默认为 GET 方法的路由 /index |
@RequestMapping(value="/index",method = RequestMethod.GET) | 同上面一条 |
@RequestMapping(value="/add",method = RequestMethod.POST) | 路由为 /add 的 POST 请求 |
@RequestMapping(value="/add",method = RequestMethod.POST),consumes="application/json" | 路由为 /add 的 POST 请求,但仅仅处理 application/json 的请求 |
@RequestMapping(value="/add",method = RequestMethod.POST),produces="application/json" | 路由为 /add 的 POST 请求,强调返回为 JSON 格式 |
@RequestMapping(value="/add",method = RequestMethod.POST),params="myParam=xyz" | 路由为 /add 的 POST 请求,但仅仅处理头部包括 myParam=xyz 的请求 |
@RequestMapping(value="/add",method = RequestMethod.POST),headers="Referer=http://www.xyz.com/" | 路由为 /add 的 POST 请求,但仅仅处理 来源为 www.xyz.com 的请求 |
2 @Controller 和 @RestController 示例
本章节,将对两个注解配合其他注解编写一系列示例,为了演示 @Controller 返回对应页面功能,我们在示例中引入了 thymeleaf 模板。具体在 pom.xml 中有说明。
编号 | 路由 | Http方法 | 方法说明 |
---|---|---|---|
1 | /user/index | GET | 获取用户列表并返回列表页面 |
1 | /user/add | GET | 用户新增页面 |
1 | /user/save | POST | 新增用户的api |
1 | /user/edit | GET | 用户编辑的页面 |
1 | /user/update | POST | 编辑用户的api |
1 | /user/del | GET | 删除用户页面 |
1 | /user/deleted | POST | 删除用户页面的api |
2.1 新建 Spring Boot 项目
- File > New > Project,如下图选择
Spring Initializr
然后点击 【Next】下一步 - 填写
GroupId
(包名)、Artifact
(项目名) 即可。点击 下一步
groupId=com.fishpro
artifactId=restcontroller - 选择依赖
Spring Web Starter
前面打钩,在模板列中勾选thymeleaf
。 - 项目名设置为
spring-boot-study-restcontroller
.
2.2 依赖 Pom.xml 配置
本项目引入了 web 和 thymeleaf ,具体一人如下代码:
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-xml"><span style="color:#0000ff"><<span style="color:#444444">dependencies</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">dependency</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">groupId</span>></span>org.springframework.boot<span style="color:#0000ff"></<span style="color:#444444">groupId</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">artifactId</span>></span>spring-boot-starter-thymeleaf<span style="color:#0000ff"></<span style="color:#444444">artifactId</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">dependency</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">dependency</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">groupId</span>></span>org.springframework.boot<span style="color:#0000ff"></<span style="color:#444444">groupId</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">artifactId</span>></span>spring-boot-starter-web<span style="color:#0000ff"></<span style="color:#444444">artifactId</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">dependency</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">dependency</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">groupId</span>></span>org.springframework.boot<span style="color:#0000ff"></<span style="color:#444444">groupId</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">artifactId</span>></span>spring-boot-starter-test<span style="color:#0000ff"></<span style="color:#444444">artifactId</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">scope</span>></span>test<span style="color:#0000ff"></<span style="color:#444444">scope</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">dependency</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">dependencies</span>></span>
</code></span></span>
并把配置文件重命名为 application.yml ,修改默认测试端口
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-yaml"><span style="color:#ff0000">server:</span>
<span style="color:#ff0000">port:</span> <span style="color:#880000">8087</span>
</code></span></span>
2.3 基于 @Controller 的示例代码
本代码实例中新增了如下页面,注意 templates
表示 /resources/tempalates/
,controller
表示 com.fishpro.restcontroller.controller
包。
- controller/UserController.java 控制层-用户类
- domain/UserDO.java 用户控实体类
- templates/user/index.html 视图-用户列表页面
- templates/user/add.html 视图-新增用户页面
- templates/user/edit.html 视图-编辑用户页面
2.3.1 返回用户列表信息 /user/index
首先构件一个虚拟的用户数据
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-java"> <span style="color:#008000">/**
* 模拟一组数据
* */</span>
<span style="color:#0000ff">private</span> List<UserDO> <span style="color:#a31515">getData</span>(){
List<UserDO> list=<span style="color:#0000ff">new</span> <span style="color:#a31515">ArrayList</span><>();
UserDO userDO=<span style="color:#0000ff">new</span> <span style="color:#a31515">UserDO</span>();
userDO.setUserId(<span style="color:#880000">1</span>);
userDO.setUserName(<span style="color:#a31515">"admin"</span>);
list.add(userDO);
userDO=<span style="color:#0000ff">new</span> <span style="color:#a31515">UserDO</span>();
userDO.setUserId(<span style="color:#880000">2</span>);
userDO.setUserName(<span style="color:#a31515">"heike"</span>);
list.add(userDO);
userDO=<span style="color:#0000ff">new</span> <span style="color:#a31515">UserDO</span>();
userDO.setUserId(<span style="color:#880000">3</span>);
userDO.setUserName(<span style="color:#a31515">"tom"</span>);
list.add(userDO);
userDO=<span style="color:#0000ff">new</span> <span style="color:#a31515">UserDO</span>();
userDO.setUserId(<span style="color:#880000">4</span>);
userDO.setUserName(<span style="color:#a31515">"mac"</span>);
list.add(userDO);
<span style="color:#0000ff">return</span> list;
}
</code></span></span>
1 创建一个用户列表页面 /user/index
UserController 增加方法如下图所示,使用 @RequestMapping 注解,注意 @RequestMapping(method = RequestMethod.GET,value = "/index")
等于 @RequestMapping(value = "/index")
也等于 @RequestMapping("/index")
也等于 @GetMapping("/index")
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-java"> <span style="color:#008000">/**
* GET 返回用户列表信息
* */</span>
<span style="color:#2b91af">@RequestMapping(method = RequestMethod.GET,value = "/index")</span>
<span style="color:#0000ff">public</span> String <span style="color:#a31515">index</span>(Model model){
List<UserDO> list =getData();
model.addAttribute(<span style="color:#a31515">"list"</span>,list);<span style="color:#008000">//返回 用户 list</span>
<span style="color:#0000ff">return</span> <span style="color:#a31515">"user/index"</span>;
}
</code></span></span>
2 创建 templates/index.html 对应上面的路由 /user/index
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-html language-xml"><span style="color:#2b91af"><!DOCTYPE <span style="color:#0000ff">html</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">html</span> <span style="color:#444444">lang</span>=<span style="color:#a31515">"en"</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">head</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">meta</span> <span style="color:#444444">charset</span>=<span style="color:#a31515">"UTF-8"</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">title</span>></span>Title<span style="color:#0000ff"></<span style="color:#444444">title</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">head</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">body</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">div</span> <span style="color:#444444">class</span>=<span style="color:#a31515">"title"</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">div</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">div</span> <span style="color:#444444">th:each</span>=<span style="color:#a31515">"user :${list}"</span>></span>
用户ID:<span style="color:#0000ff"><<span style="color:#444444">span</span> <span style="color:#444444">th:text</span>=<span style="color:#a31515">"${user.userId}"</span>></span><span style="color:#0000ff"></<span style="color:#444444">span</span>></span>
用户名:<span style="color:#0000ff"><<span style="color:#444444">span</span> <span style="color:#444444">th:text</span>=<span style="color:#a31515">"${user.userName}"</span>></span><span style="color:#0000ff"></<span style="color:#444444">span</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">div</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">body</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">html</span>></span>
</code></span></span>
3 在浏览器中查看
右键 RestControllerApplication > Run 在浏览器输入 http://localhost:8087/user/index
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-html language-xml">用户ID:1 用户名:admin
用户ID:2 用户名:heike
用户ID:3 用户名:tom
用户ID:4 用户名:mac
</code></span></span>
2.3.3 新增用户页面 /user/add
在 UserController 中增加 add 路由 /user/add
UserController 增加方法如下图所示,使用 @RequestMapping 注解,并创建了两个路由
- /user/add 对应前端页面
- /user/save 对应前端页面提交按钮的接口
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-java"> <span style="color:#008000">/**
* GET 返回add页面
* <span style="color:#808080">@GetMapping</span>("/add") = <span style="color:#808080">@RequestMapping</span>(method = RequestMethod.GET,value = "/add")
* */</span>
<span style="color:#2b91af">@GetMapping("/add")</span>
<span style="color:#0000ff">public</span> String <span style="color:#a31515">add</span>(){
<span style="color:#0000ff">return</span> <span style="color:#a31515">"user/add"</span>;
}
<span style="color:#008000">/**
* POST 新增用户api
* <span style="color:#808080">@return</span> 返回 map对象
* */</span>
<span style="color:#2b91af">@RequestMapping(method = RequestMethod.POST,value = "/save")</span>
<span style="color:#2b91af">@ResponseBody</span>
<span style="color:#0000ff">public</span> Object <span style="color:#a31515">save</span>(UserDO user){
List<UserDO> list= getData();
list.add(user);<span style="color:#008000">//模拟向列表中增加数据</span>
Map<String,Object> map=<span style="color:#0000ff">new</span> <span style="color:#a31515">HashMap</span><>();
<span style="color:#0000ff">if</span>(<span style="color:#a31515">null</span>==user){
map.put(<span style="color:#a31515">"status"</span>,<span style="color:#880000">3</span>);
map.put(<span style="color:#a31515">"message"</span>,<span style="color:#a31515">"没有传任何对象"</span>);
<span style="color:#0000ff">return</span> map;
}
map.put(<span style="color:#a31515">"status"</span>,<span style="color:#880000">0</span>);
map.put(<span style="color:#a31515">"data"</span>,user);
<span style="color:#0000ff">return</span> map;
}
</code></span></span>
创建 templates/add.htm 文件对应路由 /user/add
在对应的 templates/add.html中增加代码
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-html language-xml"><span style="color:#2b91af"><!DOCTYPE <span style="color:#0000ff">html</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">html</span> <span style="color:#444444">lang</span>=<span style="color:#a31515">"en"</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">head</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">meta</span> <span style="color:#444444">charset</span>=<span style="color:#a31515">"UTF-8"</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">title</span>></span>用户新增页面<span style="color:#0000ff"></<span style="color:#444444">title</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">script</span> <span style="color:#444444">src</span>=<span style="color:#a31515">"https://cdn.bootcss.com/jquery/1.11.3/jquery.js"</span>></span><span style="color:#0000ff"></<span style="color:#444444">script</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">head</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">body</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">form</span> <span style="color:#444444">id</span>=<span style="color:#a31515">"form1"</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">div</span>></span> <span style="color:#0000ff"><<span style="color:#444444">input</span> <span style="color:#444444">name</span>=<span style="color:#a31515">"userId"</span> <span style="color:#444444">id</span>=<span style="color:#a31515">"userId"</span> <span style="color:#444444">placeholder</span>=<span style="color:#a31515">"请输入userid"</span>></span><span style="color:#0000ff"></<span style="color:#444444">div</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">div</span>></span> <span style="color:#0000ff"><<span style="color:#444444">input</span> <span style="color:#444444">name</span>=<span style="color:#a31515">"userName"</span> <span style="color:#444444">id</span>=<span style="color:#a31515">"userName"</span> <span style="color:#444444">placeholder</span>=<span style="color:#a31515">"请输入username"</span>></span><span style="color:#0000ff"></<span style="color:#444444">div</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">div</span>></span> <span style="color:#0000ff"><<span style="color:#444444">input</span> <span style="color:#444444">type</span>=<span style="color:#a31515">"button"</span> <span style="color:#444444">value</span>=<span style="color:#a31515">"新增"</span> <span style="color:#444444">id</span>=<span style="color:#a31515">"btnSave"</span>/></span><span style="color:#0000ff"></<span style="color:#444444">div</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">form</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">script</span>></span>
$(<span style="color:#0000ff">function</span> () {
$(<span style="color:#a31515">"#btnSave"</span>).<span style="color:#a31515">click</span>(<span style="color:#0000ff">function</span> () {
$.<span style="color:#a31515">ajax</span>({
<span style="color:#ff0000">cache</span>: <span style="color:#a31515">true</span>,
<span style="color:#ff0000">type</span>: <span style="color:#a31515">"POST"</span>,
<span style="color:#ff0000">url</span>: <span style="color:#a31515">"/user/save"</span>,
<span style="color:#ff0000">data</span>:$(<span style="color:#a31515">'#form1'</span>).<span style="color:#a31515">serialize</span>(),
<span style="color:#ff0000">dataType</span>:<span style="color:#a31515">"json"</span>,
<span style="color:#ff0000">async</span>: <span style="color:#a31515">false</span>,
<span style="color:#ff0000">error</span>: <span style="color:#0000ff">function</span> (request) {
<span style="color:#008000">console</span>.<span style="color:#a31515">log</span>(<span style="color:#a31515">"Connection error"</span>);
},
<span style="color:#ff0000">success</span>: <span style="color:#0000ff">function</span> (data) {
<span style="color:#0000ff">if</span> (data.code == <span style="color:#880000">0</span>) {
<span style="color:#008000">console</span>.<span style="color:#a31515">log</span>(<span style="color:#a31515">"成功"</span>);
} <span style="color:#0000ff">else</span> {
<span style="color:#008000">console</span>.<span style="color:#a31515">log</span>(<span style="color:#a31515">"失败"</span>);
}
}
});
});
});
<span style="color:#0000ff"></<span style="color:#444444">script</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">body</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">html</span>></span>
</code></span></span>
在浏览器中测试效果
右键 RestControllerApplication > Run 在浏览器输入 http://localhost:8087/user/add
2.3.3 编辑用户页面 /user/edit
这部分代码原理同 2.3.2 代码原理 通过构件一个编辑页面,点击编辑页面的【保存】提交到后端的 api 中。
本示例中创建了2个接口 1个文件
- /user/edit 对应前端页面
- /user/update 对应前端页面提交按钮的接口
- /templates/user/edit.html
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-java"> <span style="color:#008000">/**
* GET 返回编辑页面
* <span style="color:#808080">@GetMapping</span>("/edit/{id}") = <span style="color:#808080">@RequestMapping</span>(method = RequestMethod.GET,value = "/edit/{id}")
* <span style="color:#808080">@PathVariable</span>("id") 表示路由中的动态参数部分
* <span style="color:#808080">@param</span> id 表示要编辑的用户id
* <span style="color:#808080">@param</span> model 表示将要输出到页面的 Model 对象
* <span style="color:#808080">@return</span> 返回到 user/edit页面
* */</span>
<span style="color:#2b91af">@GetMapping("/edit/{id}")</span>
<span style="color:#0000ff">public</span> String <span style="color:#a31515">edit</span>(<span style="color:#2b91af">@PathVariable("id")</span> String id, Model model){
<span style="color:#a31515">UserDO</span> <span style="color:#008000">user</span> <span style="color:#ab5656">=</span><span style="color:#0000ff">new</span> <span style="color:#a31515">UserDO</span>();
user.setUserId(<span style="color:#880000">3</span>);
user.setUserName(<span style="color:#a31515">"fishpro"</span>);
model.addAttribute(<span style="color:#a31515">"user"</span>,user);
<span style="color:#0000ff">return</span> <span style="color:#a31515">"user/edit"</span>;
}
<span style="color:#008000">/**
* POST 修改用户api
* <span style="color:#808080">@RequestBody</span> 表示参数使用 json 对象传输
* <span style="color:#808080">@return</span> 返回 map对象
* */</span>
<span style="color:#2b91af">@PostMapping("/update")</span>
<span style="color:#2b91af">@ResponseBody</span>
<span style="color:#0000ff">public</span> Object <span style="color:#a31515">update</span>(<span style="color:#2b91af">@RequestBody</span> UserDO user){
Map<String,Object> map=<span style="color:#0000ff">new</span> <span style="color:#a31515">HashMap</span><>();
<span style="color:#0000ff">if</span>(<span style="color:#a31515">null</span>==user){
map.put(<span style="color:#a31515">"status"</span>,<span style="color:#880000">3</span>);
map.put(<span style="color:#a31515">"message"</span>,<span style="color:#a31515">"没有传任何对象"</span>);
<span style="color:#0000ff">return</span> map;
}
<span style="color:#008000">//更新逻辑</span>
map.put(<span style="color:#a31515">"status"</span>,<span style="color:#880000">0</span>);
<span style="color:#0000ff">return</span> map;
}
</code></span></span>
edit.html
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-html language-xml"><span style="color:#2b91af"><!DOCTYPE <span style="color:#0000ff">html</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">html</span> <span style="color:#444444">lang</span>=<span style="color:#a31515">"en"</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">head</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">meta</span> <span style="color:#444444">charset</span>=<span style="color:#a31515">"UTF-8"</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">title</span>></span>用户编辑页面<span style="color:#0000ff"></<span style="color:#444444">title</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">script</span> <span style="color:#444444">src</span>=<span style="color:#a31515">"https://cdn.bootcss.com/jquery/1.11.3/jquery.js"</span>></span><span style="color:#0000ff"></<span style="color:#444444">script</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">head</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">body</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">form</span> <span style="color:#444444">id</span>=<span style="color:#a31515">"form1"</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">div</span>></span> <span style="color:#0000ff"><<span style="color:#444444">input</span> <span style="color:#444444">name</span>=<span style="color:#a31515">"userId"</span> <span style="color:#444444">id</span>=<span style="color:#a31515">"userId"</span> <span style="color:#444444">placeholder</span>=<span style="color:#a31515">"请输入userid"</span> <span style="color:#444444">th:value</span>=<span style="color:#a31515">${user.userId}</span>></span> <span style="color:#0000ff"></<span style="color:#444444">div</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">div</span>></span> <span style="color:#0000ff"><<span style="color:#444444">input</span> <span style="color:#444444">name</span>=<span style="color:#a31515">"userName"</span> <span style="color:#444444">id</span>=<span style="color:#a31515">"userName"</span> <span style="color:#444444">placeholder</span>=<span style="color:#a31515">"请输入username"</span> <span style="color:#444444">th:value</span>=<span style="color:#a31515">${user.userName}</span>></span><span style="color:#0000ff"></<span style="color:#444444">div</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">div</span>></span> <span style="color:#0000ff"><<span style="color:#444444">input</span> <span style="color:#444444">type</span>=<span style="color:#a31515">"button"</span> <span style="color:#444444">value</span>=<span style="color:#a31515">"新增"</span> <span style="color:#444444">id</span>=<span style="color:#a31515">"btnSave"</span>/></span><span style="color:#0000ff"></<span style="color:#444444">div</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">form</span>></span>
<span style="color:#0000ff"><<span style="color:#444444">script</span>></span>
$(<span style="color:#0000ff">function</span> () {
$(<span style="color:#a31515">"#btnSave"</span>).<span style="color:#a31515">click</span>(<span style="color:#0000ff">function</span> () {
$.<span style="color:#a31515">ajax</span>({
<span style="color:#ff0000">cache</span>: <span style="color:#a31515">true</span>,
<span style="color:#ff0000">type</span>: <span style="color:#a31515">"POST"</span>,
<span style="color:#ff0000">url</span>: <span style="color:#a31515">"/user/update"</span>,
<span style="color:#ff0000">data</span>:$(<span style="color:#a31515">'#form1'</span>).<span style="color:#a31515">serialize</span>(),
<span style="color:#ff0000">dataType</span>:<span style="color:#a31515">"json"</span>,
<span style="color:#ff0000">async</span>: <span style="color:#a31515">false</span>,
<span style="color:#ff0000">error</span>: <span style="color:#0000ff">function</span> (request) {
<span style="color:#008000">console</span>.<span style="color:#a31515">log</span>(<span style="color:#a31515">"Connection error"</span>);
},
<span style="color:#ff0000">success</span>: <span style="color:#0000ff">function</span> (data) {
<span style="color:#0000ff">if</span> (data.code == <span style="color:#880000">0</span>) {
<span style="color:#008000">console</span>.<span style="color:#a31515">log</span>(<span style="color:#a31515">"成功"</span>);
} <span style="color:#0000ff">else</span> {
<span style="color:#008000">console</span>.<span style="color:#a31515">log</span>(<span style="color:#a31515">"失败"</span>);
}
}
});
});
});
<span style="color:#0000ff"></<span style="color:#444444">script</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">body</span>></span>
<span style="color:#0000ff"></<span style="color:#444444">html</span>></span>
</code></span></span>
2.3.3 删除用户 /user/delete
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#008000">/**
* POST 修改用户api
* <span style="color:#808080">@return</span> 返回 map对象
* */</span>
<span style="color:#2b91af">@PostMapping("/delete/{id}")</span>
<span style="color:#2b91af">@ResponseBody</span>
<span style="color:#0000ff">public</span> Object <span style="color:#a31515">delete</span>(<span style="color:#2b91af">@PathVariable("id")</span> Integer id){
List<UserDO> list= getData();
UserDO userDO=<span style="color:#a31515">null</span>;
<span style="color:#0000ff">for</span> (UserDO user:list
) {
<span style="color:#0000ff">if</span>(id.equals(user.getUserId().toString())){
<span style="color:#008000">//删除用户</span>
userDO=user;
<span style="color:#0000ff">break</span>;
}
}
Map<String,Object> map=<span style="color:#0000ff">new</span> <span style="color:#a31515">HashMap</span><>();
map.put(<span style="color:#a31515">"status"</span>,<span style="color:#880000">0</span>);
map.put(<span style="color:#a31515">"data"</span>,userDO);
<span style="color:#0000ff">return</span> map;
}
</code></span></span>
2.4 基于 @RestController 的示例代码
本章节中是基于 2.3版本中的接口,其实是一样的功能,详细见下面的代码,为了完成代码功能示例,我们新建了 UserRestController.java,不同的是 我们给类加了 @RestController 修饰符。
<span style="color:#333333"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#2b91af">@RestController</span>
<span style="color:#2b91af">@RequestMapping("user2")</span>
<span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#a31515">UserRestController</span> {
<span style="color:#008000">//从 UserController 搬过来代码即可</span>
}
</code></span></span>
关联阅读: