【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】03、创建RESTful API,并统一处理返回值
本节应用Spring对RESTful的支持,使用了如@RestController等注解实现RESTful控制器。
如果对Spring中的RESTful不太明白,请查看相关书籍
1、创建一个数据对象,用来统一RESTful API的返回格式
- package com.ltsolution.framework.common.msgmodel;
- import java.util.HashMap;
- import java.util.Map;
- public class AppResult {
- private String status;
- private String error;
- private String message;
- private Map<String, Object> data = new HashMap<String, Object>();
- public String getStatus() {
- return status;
- }
- public void setStatus(String status) {
- this.status = status;
- }
- public String getError() {
- return error;
- }
- public void setError(String error) {
- this.error = error;
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public Map<String, Object> getData() {
- return data;
- }
- public void setData(Map<String, Object> data) {
- this.data = data;
- }
- public AppResult ok(String statusMsg) {
- this.status="200";
- this.message=statusMsg;
- return this;
- }
- public AppResult error(String statusMsg) {
- this.status="500";
- this.message=statusMsg;
- return this;
- }
- public AppResult addData(String key,Object object) {
- this.data.put(key,object);
- return this;
- }
- }
2、创建RESTful控制器
- @RestController
- @RequestMapping(value = "/")
- public class UserController {
- @PostMapping("/login")
- public AppResult login(HttpServletRequest request, HttpServletResponse response) throws Exception {
- return new AppResult().ok("登录成功!").addData("token", 1234);
- }
- @PostMapping("/test")
- public AppResult test(HttpServletRequest request, HttpServletResponse response) throws Exception {
- return new AppResult().ok("测试!");
- }
- }
3、测试API
【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】03、创建RESTful API,并统一处理返回值的更多相关文章
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】06、Mybatis+SQLServer集成
1.增加POM依赖 注意pagehelper插件,我重写过,可以到我的这篇文章了解https://www.cnblogs.com/LiveYourLife/p/9176934.html <dep ...
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】05、Shiro集成
1.POM文件中加入Shiro和fastJSON依赖 <dependency> <groupId>org.apache.shiro</groupId> <ar ...
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】04、统一处理异常
本节讨论如何使用Spring的异常处理机制,当我们程序出现错误时,以相同的一种格式,把错误信息返回给客户端 1.创建一些自定义异常 public class TipsException extends ...
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】01、环境准备
开发环境 windows+STS(一个针对Spring优化的Eclipse版本)+Maven+SQLServer 环境部署 1.安装SQLServer(使用版本2008R2) 自行安装,此处略过 2. ...
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】02、创建新的SpringBoot项目
1.创建项目 得到项目架构 2.测试项目Web功能 默认端口为8080,运行后,输入localhost:8080/index即可访问到网页 到这里,项目构建成功!
- Myeclipse下使用Maven搭建spring boot项目(第二篇)
现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来. 一.在pom.xml中引入spring-boot-start-parent,spring官方的叫st ...
- Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创
原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...
- Myeclipse下使用Maven搭建spring boot项目
开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...
- Maven 搭建spring boot多模块项目
Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...
随机推荐
- BZOJ 1369: [Baltic2003]Gem(树形dp)
传送门 解题思路 直接按奇偶层染色是错的,\(WA\)了好几次,所以要树形\(dp\),感觉最多\(log\)种颜色,不太会证. 代码 #include<iostream> #includ ...
- HBASE(分布式海量NOSQL数据库)
HBase建表高级属性,hbase应用案例看行键设计,HBase和mapreduce结合,从Hbase中读取数据.分析,写入hdfs,从hdfs中读取数据写入Hbase,协处理器和二级索引 1. Hb ...
- outlook使用inline style回复邮件
Reply with inline comments within the original message text When you reply to email messages in Outl ...
- linux下不同服务器间数据传输(rcp,scp,rsync,ftp,sftp,lftp,wget,curl)
因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...
- Map:template
ylbtech-Map-Amap|Baidu: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9. ...
- 测开之路二十六:Flask基础之最小web程序
Flask中文文档:http://docs.jinkan.org/docs/flask/ 安装Flask库 选端口号的一种方法(避免和别人选的端口冲突,小于1024的时候重新选) 最小web程序 用1 ...
- Asp.Net Core 第07局:路由
总目录 前言 本文介绍Asp.Net Core 路由. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:路由概述 1.路由主要用于处理特定的请求. ...
- The life-saving straw
English learning In contemporary world, English learning has gained great popularity and it is of ...
- git使用记录八:不同提交的指定文件的差异
不同提交的指定文件的差异 git diff commit-id1 commit-id2 path-to-filename
- layer子窗口赋值给父窗口
子窗体赋值给父窗体: parent.$('#Receiver').val(typearr); //关闭子弹窗 var index = parent.layer.getFrameIndex(window ...