本节应用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,并统一处理返回值的更多相关文章

  1. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】06、Mybatis+SQLServer集成

    1.增加POM依赖 注意pagehelper插件,我重写过,可以到我的这篇文章了解https://www.cnblogs.com/LiveYourLife/p/9176934.html <dep ...

  2. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】05、Shiro集成

    1.POM文件中加入Shiro和fastJSON依赖 <dependency> <groupId>org.apache.shiro</groupId> <ar ...

  3. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】04、统一处理异常

    本节讨论如何使用Spring的异常处理机制,当我们程序出现错误时,以相同的一种格式,把错误信息返回给客户端 1.创建一些自定义异常 public class TipsException extends ...

  4. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】01、环境准备

    开发环境 windows+STS(一个针对Spring优化的Eclipse版本)+Maven+SQLServer 环境部署 1.安装SQLServer(使用版本2008R2) 自行安装,此处略过 2. ...

  5. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】02、创建新的SpringBoot项目

    1.创建项目 得到项目架构 2.测试项目Web功能 默认端口为8080,运行后,输入localhost:8080/index即可访问到网页 到这里,项目构建成功!

  6. Myeclipse下使用Maven搭建spring boot项目(第二篇)

    现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来. 一.在pom.xml中引入spring-boot-start-parent,spring官方的叫st ...

  7. Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创

    原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...

  8. Myeclipse下使用Maven搭建spring boot项目

    开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...

  9. Maven 搭建spring boot多模块项目

    Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...

随机推荐

  1. python3下tomorow模块报语法错误def async(n, base_type, timeout=None): ^ SyntaxError: invalid syntax

    python3 pip 安装tomorrow模块,调用时候会报错:def async(n, base_type, timeout=None): ^ SyntaxError: invalid synta ...

  2. vue 中使用scss

    1.下载 npm install --save-dev sass-loader npm install --save-dev node-sass npm install sass-loader --s ...

  3. CTF | bugku | 秋名山车神

    ''' @Modify Time @Author ------------ ------- 2019/8/31 19:55 laoalo ''' import requests from lxml i ...

  4. VB.NET导出Excel 轻松实现Excel的服务器与客户端交换 服务器不安装Office

    说来VB.Net这个也是之前的一个项目中用到的.今天拿来总结下用途,项目需求,不让在服务器安装Office办公软件.这个也是煞费了一顿. 主要的思路就是 在导出的时候,利用DataTable做中间变量 ...

  5. ruby+selenium-webdriver一步一步完成自动化测试-----准备篇

    这一系列文章目的不是讲ruby,也不是讲selenium-webdriver,而是通过一个登录soso首页的小例子讲怎样一步一步写好自动化测试.目标是:面向对象编程,实现逻辑与数据分离. 如果你觉得已 ...

  6. 使用Docker快速部署Gitlab

    使用Docker部署Gitlab 1. 下载gitlab镜像 docker pull gitlab/gitlab-ce 2. 运行gitlab实例 GITLAB_HOME=`pwd`/data/git ...

  7. android 关联某些后缀使用app打开

    <intent-filter> <action android:name="android.intent.action.VIEW" /> <categ ...

  8. 使用Xftp连接Centos 6.6服务器详细图文教程

    这篇文章主要介绍了使用Xftp连接Centos 6.6服务器详细图文教程,本文用详细的图文说明讲解了连接服务器和操作服务器的步骤,适合新手,需要的朋友可以参考下 一,打开Xftp软件(下载地址:Xma ...

  9. TP5数据库事务操作

    使用事务处理的话,需要数据库引擎支持事务处理.比如 MySQL 的 MyISAM 不支持事务处理,需要使用 InnoDB 引擎. 使用 transaction 方法操作数据库事务,当发生异常会自动回滚 ...

  10. 垃圾循环li

    function hezhu() { // var lis= $("#aaa").find("treeview"); ////var lis= document ...