Spring Boot 自定义应用开发框架笔记一—— 基础:项目搭建

最近在做一个全新的平台项目,这个项目又要被拆分成多个子系统项目分配给不同的开发小组开发,因为是同一个平台下的项目,基础设置是统一的一套,而且为了后续不同的小组人员的无缝对接,基础框架和基础设置肯定是要一致的,而离我初次搭建的spring boot框架也过去了3年之久,在使用过程中发现了老的框架欠缺的地方,也有了一些新的想法和新技术的运用,所以这段时间一直在重构一个新的框架,目前暂告一段,趁空闲记录下搭建过程中的一些问题和想法。

先简单讲解下框架的组成,主要分为3个部分。

一、元件库(component)

      这个主要是一些开发过程中经常用到的工具类,尤其是根据公司的一些特有的需求做的工具类,例如一些数学计算,文件处理,excel导入导出处理等。还有就是统一的异常处理和统一的注解的处理,例如数据权限注解,页面限流注解等

二、统一框架(framework)

       顾名思义,此模块就是核心内容,主要就是用到的底层框架的二次封装,例如spring boot、mybatis plus、spring security、redis、统一排程

三、基础项目

       这个主要是基础功能的开发,例如用户,角色,组织,字典等

四、分子项目

        这个是具体的各个项目组需要开发的项目

以上四个部分从下到上,一层依赖一层,我们使用maven去管理。

整个框架流程上其实不是很复杂,但是功能全部搭建完还是比较麻烦的,一步一步的记录不太实际,我也没有那么多时间,前期主要是将springboot + spring security +JWT 的认证管理。

今天就从基础如何搭建一个spring boot 项目开始

一、打开https://start.spring.io/

如上图所示,点击右侧的“ADD Dependencies”按钮,选择Spring Web ,其它的按图上所示选择即可,下载下来后导入eclipse.

二、设置配置文件

  默认的配置文件是“application.properties”,本人习惯用“application.yml”,因为是最简单的空项目,现在能做的就是把端口号改掉,如改为8888

三、启动项目

   找到DemoApplication.java ,直接运行main,出现下图即表示成功

四、编写一个Controller

@RestController
@RequestMapping("index")
public class IndexController {

	@GetMapping("index")
	public Map<String, String> index() {
		final Map<String, String> map = new HashMap<String, String>();
		map.put("name", "hi,半路凉亭");
		return map;
	}
}

重新启动项目后在浏览器中输入:http://127.0.0.1:8888/index/index 打开后如下图

五、前后端分离

现在大多数公司都应该是前后端分离开发,像上面的这种写法一般都不会用了,后端会统一封装成固定的格式把数据传给前端,如我这里会定义一个对象专门用来将数据封装后传给前端。

定义一个抽象类 :AjaxResponse.java

public abstract class AjaxResponse implements Serializable {

	private static final long serialVersionUID = -5858819825109609209L;

	/**
	 * 是否成功
	 */
	private boolean succeed;
	/**
	 * 提示信息编码 如:001
	 */
	private String code;

	/**
	 * 返回信息
	 */
	private Map<String, Object> data = new HashMap<String, Object>();

	/**
	 * @return the code
	 */
	public String getCode() {
		return this.code;
	}

	/**
	 * @return the succeed
	 */
	public boolean getSucceed() {
		return this.succeed;
	}

	/**
	 * @param succeed
	 *            the succeed to set
	 */
	public void setSucceed(final boolean succeed) {
		this.succeed = succeed;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(final String code) {
		this.code = code;
	}

	/**
	 * @return the data
	 */
	public Map<String, Object> getData() {
		return this.data;
	}

	/**
	 * @param data
	 *            the data to set
	 */
	public void setData(final Map<String, Object> data) {
		this.data = data;
	}

	/**
	 *
	 * 添加额外的属性,注意:如果返回前端的属性只有一个是LIST,则统一命名为“list”,或者使用addList()方法
	 *
	 * @param attributeName
	 * @param attributeValue
	 */
	public void addAttribute(final String attributeName, final Object attributeValue) {
		this.data.put(attributeName, attributeValue);
	}

	/**
	 *
	 * 添加额外属性组
	 *
	 * @param attributes
	 */
	public void addAllAttributes(final Map<String, ?> attributes) {
		if (attributes != null) {
			for (final Map.Entry<String, ?> e : attributes.entrySet()) {
				this.addAttribute(e.getKey(), e.getValue());
			}
		}
	}

	/**
	 * 默认一个key为list的集合给前端
	 *
	 * @date 2020年2月13日
	 * @param list
	 *            the list to set
	 */
	public void addList(final Object list) {
		this.addAttribute("list", list);
	}

	/**
	 * 默认将一个对象实体放入到key为‘obj’的变量中返回给前端
	 *
	 * @author huangmin
	 * @date 2021年2月26日
	 * @param entity
	 */
	public void addEntity(final Object entity) {
		this.addAttribute("obj", entity);
	}
}

然后再分别定义成功和失败的对象继承这个抽象类

public class SucceedResponse extends AjaxResponse {

	private static final long serialVersionUID = -4355521516857165427L;

	/**
	 *
	 * ajax 返回对象
	 *
	 * @param code
	 *            提示框提示信息编码 如:000
	 */
	public SucceedResponse(final String code) {
		this.setSucceed(true);
		this.setCode(code);
	}

	public SucceedResponse() {
		this("OK");
	}
}
public class ErrorResponse extends AjaxResponse {

	private static final long serialVersionUID = -8193489457799404643L;

	/**
	 *
	 * ajax 返回对象
	 *
	 * @param code
	 *            提示信息编码 如:001
	 */
	public ErrorResponse(final String code) {
		this.setSucceed(false);
		this.setCode(code);
	}

	public ErrorResponse() {
		this("error");
	}
}

六、模拟前端创建、修改数据

    @PostMapping("create")
	public AjaxResponse create(final String name) {
		System.out.println("新增一条数据姓名为:" + name);
		return new SucceedResponse();
	}

	@PostMapping("update")
	public AjaxResponse update(final String name) {
		try {
			System.out.println(1 / 0 + "修改一条数据姓名为:" + name);
		} catch (final Exception e) {
			e.printStackTrace();
			return new ErrorResponse("修改数据异常:" + e.getMessage());
		}
		return new SucceedResponse();
	}

postman运行的效果

新增数据 (成功)

编辑数据(失败)

编辑数据这里特意写了个异常,模拟保存失败的情况,再实际开发中,异常也是要统一做封装处理的,不会像这里写的在catch里return方式,这个下次再说。

七、小玩意

   每次我们启动spring boot项目时,控制台默认会打印“spring boot”几个大字,这个我们是可以修改的 ,创建一个banner.txt,放到配置文件同目录下,然后通过下面这个网站生成自己想要的字符。如我这里改成我的网名:zhuiyue

Text to ASCII Art Generator (TAAG)

重启项目后,控制台如下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值