本节应用Spring对RESTful的支持,使用了如@RestController等注解实现RESTful控制器。

如果对Spring中的RESTful不太明白,请查看相关书籍

1、创建一个数据对象,用来统一RESTful API的返回格式

  1. package com.ltsolution.framework.common.msgmodel;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class AppResult {
  7. private String status;
  8. private String error;
  9. private String message;
  10. private Map<String, Object> data = new HashMap<String, Object>();
  11.  
  12. public String getStatus() {
  13. return status;
  14. }
  15.  
  16. public void setStatus(String status) {
  17. this.status = status;
  18. }
  19.  
  20. public String getError() {
  21. return error;
  22. }
  23.  
  24. public void setError(String error) {
  25. this.error = error;
  26. }
  27.  
  28. public String getMessage() {
  29. return message;
  30. }
  31.  
  32. public void setMessage(String message) {
  33. this.message = message;
  34. }
  35.  
  36. public Map<String, Object> getData() {
  37. return data;
  38. }
  39.  
  40. public void setData(Map<String, Object> data) {
  41. this.data = data;
  42. }
  43.  
  44. public AppResult ok(String statusMsg) {
  45. this.status="200";
  46. this.message=statusMsg;
  47. return this;
  48. }
  49. public AppResult error(String statusMsg) {
  50. this.status="500";
  51. this.message=statusMsg;
  52. return this;
  53. }
  54. public AppResult addData(String key,Object object) {
  55. this.data.put(key,object);
  56. return this;
  57. }
  58. }

2、创建RESTful控制器

  1. @RestController
  2. @RequestMapping(value = "/")
  3. public class UserController {
  4.  
  5. @PostMapping("/login")
  6. public AppResult login(HttpServletRequest request, HttpServletResponse response) throws Exception {
  7. return new AppResult().ok("登录成功!").addData("token", 1234);
  8. }
  9. @PostMapping("/test")
  10. public AppResult test(HttpServletRequest request, HttpServletResponse response) throws Exception {
  11. return new AppResult().ok("测试!");
  12. }
  13.  
  14. }

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. BZOJ 1369: [Baltic2003]Gem(树形dp)

    传送门 解题思路 直接按奇偶层染色是错的,\(WA\)了好几次,所以要树形\(dp\),感觉最多\(log\)种颜色,不太会证. 代码 #include<iostream> #includ ...

  2. HBASE(分布式海量NOSQL数据库)

    HBase建表高级属性,hbase应用案例看行键设计,HBase和mapreduce结合,从Hbase中读取数据.分析,写入hdfs,从hdfs中读取数据写入Hbase,协处理器和二级索引 1. Hb ...

  3. outlook使用inline style回复邮件

    Reply with inline comments within the original message text When you reply to email messages in Outl ...

  4. linux下不同服务器间数据传输(rcp,scp,rsync,ftp,sftp,lftp,wget,curl)

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...

  5. Map:template

    ylbtech-Map-Amap|Baidu: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9. ...

  6. 测开之路二十六:Flask基础之最小web程序

    Flask中文文档:http://docs.jinkan.org/docs/flask/ 安装Flask库 选端口号的一种方法(避免和别人选的端口冲突,小于1024的时候重新选) 最小web程序 用1 ...

  7. Asp.Net Core 第07局:路由

    总目录 前言 本文介绍Asp.Net Core 路由. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:路由概述 1.路由主要用于处理特定的请求. ...

  8. The life-saving straw

    English learning   In contemporary world, English learning has gained great popularity and it is of ...

  9. git使用记录八:不同提交的指定文件的差异

    不同提交的指定文件的差异 git diff commit-id1 commit-id2 path-to-filename

  10. layer子窗口赋值给父窗口

    子窗体赋值给父窗体: parent.$('#Receiver').val(typearr); //关闭子弹窗 var index = parent.layer.getFrameIndex(window ...