本节应用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. LOJ 2541 「PKUWC2018」猎人杀——思路+概率+容斥+分治

    题目:https://loj.ac/problem/2541 看了题解才会……有三点很巧妙. 1.分母如果变动,就很不好.所以考虑把操作改成 “已经选过的人仍然按 \( w_i \) 的概率被选,但是 ...

  2. JavaScript实现Word、Excel、PPT在线预览

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_27626333/article/d ...

  3. Autoresize UIView to fit subviews

    @interface UIView (resizeToFit) -(void)resizeToFitSubviews; -(void)resizeHightToFitSubviews; -(void) ...

  4. 源码分析笔记Vector

    概述 继承抽象类AbStractList,实现接口List.RandomAccess.Cloneable以及序列化接口默认容量大小为10,扩容增量为0,扩容为原容量的2倍如设置的增量大于0,则扩容为( ...

  5. iPad如何恢复

    iPad在有网络连接的情况下,可以通过iCloud进行恢复. 没有连接WiFi的情况下,只能通过USB连接才能恢复. ①下载最新版本的iTunes:https://support.apple.com/ ...

  6. MySQL复制表结构和内容到另一个表中

    一:(低版本的mysql不支持,mysql4.0.25 不支持,mysql5已经支持了)1.复制表结构到新表CREATE TABLE 新表LIKE 旧表 2.复制旧表的数据到新表(假设两个表结构一样) ...

  7. spring controller使用了@ResponseBody却返回xml

    使用ajax请求数据 $.ajax({ url:message.rootPath +"/sendMessage.xhtml", type:"post", dat ...

  8. 科普:std::sort干了什么

    std::sort算是STL中对OIer比较友好的函数了,但你有想过sort是如何保证它的高速且稳定吗? 正文 我们首先来到第一层:sort函数 template<typename _Rando ...

  9. 系统安装1---U盘启动器制作

    1.下载PE制作工具,现在有很多的PE制作工具,如老毛桃,大白菜等.下面我使用的是IT天空的U盘PE制作工具. 下载地址:https://www.itiankong.net/thread-369670 ...

  10. 用css3写出的倒三角形

    <!DOCTYPE html><html><head><meta charset="gb2312" /><title>无 ...