2、架构设计

摘要:
3.API结构设计_为了满足实际需求,制定了标准来封装API格式。代码如下:packagecom.houseSearch。base/***API格式封装*Createdby Xiaoshao*/PublicclassApiResponse{privateintcode;//自定义请求状态代码privateStringmessage;//自定义的请求对应信息描述privateObjectdata;//请求目标数据privateboolean;publicApiRespresponse{this.code=code;this.message=message;this.data=data;}publicApiResponse(){this.code=Status.SSUCCESS.getCode();this.message=Status.sSUCCESS.getStandardMessage();}publicintgetCode(){returncode;}publicvoisetCode{this.code=code;}publicStringgetMessage(){returnmessage;}publicvoisetMessage{this.message=message;}publicObjectgetData(){returndata;}publicvoidsetData{this.data=data;}publicbooleanisMore(){returnmore;}publicvoidsetMore{this.more=more;}消息的publicstaticApiResponse{returnnewApiResponse;}publicstaticApiResponseofSuccess{returnnewApiResponse;}publicstaticApiResponseofStatus{returnnewApiResponse;}publicenumStatus{SUCCESS,BAD_REQUEST,NOT_FOUND,INTERNAL_SERVER_ERROR,NOT_VALID_PARAM,NOT_SUPPORTED_OPERATION,NOT_LOGIN;privateintcode;privateStringstandardMessage;状态{this.code=code;this.standardMessage=standardMessage;}publicintgetCode(){returncode;}publicvoisetCode{this.code=code;}publicStringgetStandardMessage(){returnstandardMessage;}publicvoidsetStandardMessage{this.standardMessage=standardMessage;}}然后将以下代码添加到HomeController文件中,运行结果如下:4.API结构设计_异常拦截器项目运行过程中,存在许多未知情况,如页面或界面异常、用户访问的页面不存在、用户权限不足等。

1、架构设计与分层

 2、架构设计第1张

2、架构设计第2张

 2、架构设计第3张

2、API结构设计_RESTFul API

什么是REST?

  REST,即Representational State Transfer的缩写,中文是"表现层状态转化"。
  它是一种互联网应用程序的API设计理念:可以用URL定位资源,用HTTP动词(GET,POST,DELETE,DETC)描述操作来解释什么是REST。
  其实全称是 Resource Representational State Transfer:通俗来讲就是:资源在网络中以某种表现形式进行状态转移。(再通俗来说,就是通过HTTP请求服务器上的某资源,使该资源copy了一份到服务请求方那去了(get动作)。个人这么理解)
  我们分解开来进行解释:

    Resource:资源,即数据它可以是一段文本、一张图片、一首歌曲等;
    Representational:某种表现形式,比如用JSON,XML,JPEG等;HTTP请求的头信息中用Accept和Content-Type字段指定,这两个字段才是对"表现形式"的描述。
    State Transfer:状态变化。通过HTTP动词实现。
  注:互联网通信协议HTTP协议,是一个无状态协议。**这意味着,所有的状态都保存在服务器端。**因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生"状态转化"(State Transfer)。HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源。

什么是REST ful API ?

  基于REST构建的API就是Restful风格。

3、API结构设计_标准制定

为了满足实际需求,自己封装API格式,代码如下:

package com.houseSearch.base;

/**
 * API格式封装
 * Created by 小劭.
 */
public class ApiResponse {
    private int code;//自定义请求状态码
    private String message;//自定义请求相应信息描述
    private Object data;//请求目标数据
    private boolean more;

    public ApiResponse(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ApiResponse() {
        this.code = Status.SUCCESS.getCode();
        this.message = Status.SUCCESS.getStandardMessage();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public boolean isMore() {
        return more;
    }

    public void setMore(boolean more) {
        this.more = more;
    }

    public static ApiResponse ofMessage(int code, String message) {
        return new ApiResponse(code, message, null);
    }

    public static ApiResponse ofSuccess(Object data) {
        return new ApiResponse(Status.SUCCESS.getCode(), Status.SUCCESS.getStandardMessage(), data);
    }

    public static ApiResponse ofStatus(Status status) {
        return new ApiResponse(status.getCode(), status.getStandardMessage(), null);
    }

    public enum Status {
        SUCCESS(200, "OK"),
        BAD_REQUEST(400, "Bad Request"),
        NOT_FOUND(404, "Not Found"),
        INTERNAL_SERVER_ERROR(500, "Unknown Internal Error"),
        NOT_VALID_PARAM(40005, "Not valid Params"),
        NOT_SUPPORTED_OPERATION(40006, "Operation not supported"),
        NOT_LOGIN(50000, "Not Login");

        private int code;
        private String standardMessage;

        Status(int code, String standardMessage) {
            this.code = code;
            this.standardMessage = standardMessage;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getStandardMessage() {
            return standardMessage;
        }

        public void setStandardMessage(String standardMessage) {
            this.standardMessage = standardMessage;
        }
    }
}

然后在HomeController文件添加如下代码以及运行结果如下:

2、架构设计第4张

2、架构设计第5张

4、API结构设计_异常拦截器

 项目运行过程中,有很多未知的情况,比如:页面或者接口的异常、用户访问的页面不存在、用户的权限不足等等。所以这里设计一个异常拦截器,主要体现着两个方面:

  1、页面异常拦截器

  2、API异常拦截器

如下,未知的请求,会显示springboot默认的 “Whitelabel Error Page”页面信息。所以要在application.properties配置文件中加上

server.error.whitelabel.enabled=false表示关闭springBoot默认的错误页面显示。

2、架构设计第6张

实现步骤:编写AppErrorController web错误 全局配置类,然后添加相应403、404、500等错误页面,这里不展示页面相关代码。

package com.houseSearch.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * web错误 全局配置
 * Created by 小劭.
 */
@Controller
public class AppErrorController implements ErrorController {
    private static final String ERROR_PATH = "/error";

    private ErrorAttributes errorAttributes;

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    @Autowired
    public AppErrorController(ErrorAttributes errorAttributes) {
        this.errorAttributes = errorAttributes;
    }

    /**
     * Web页面错误处理
     */
    @RequestMapping(value = ERROR_PATH, produces = "text/html")
    public String errorPageHandler(HttpServletRequest request, HttpServletResponse response) {
        int status = response.getStatus();
        switch (status) {
            case 403:
                return "403";
            case 404:
                return "404";
            case 500:
                return "500";
        }

        return "index";
    }

    /**
     * 除Web页面外的错误处理,比如Json/XML等
     */
    @RequestMapping(value = ERROR_PATH)
    @ResponseBody
    public ApiResponse errorApiHandler(HttpServletRequest request) {
        RequestAttributes requestAttributes = new ServletRequestAttributes(request);

        Map<String, Object> attr = this.errorAttributes.getErrorAttributes(requestAttributes, false);
        int status = getStatus(request);

        return ApiResponse.ofMessage(status, String.valueOf(attr.getOrDefault("message", "error")));
    }

    private int getStatus(HttpServletRequest request) {
        Integer status = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (status != null) {
            return status;
        }

        return 500;
    }
}

免责声明:文章转载自《2、架构设计》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ExtJs使用总结(非常详细)LDAP Browser/Editor v2.8.2下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

kafka消费组创建和删除原理

0.10.0.0版本的kafka的消费者和消费组已经不在zk上注册节点了,那么消费组是以什么形式存在的呢? 1 入口 看下kafka自带的脚本kafka-consumer-groups.sh,可见脚本调用了kafka.admin.ConsumerGroupCommand exec $(dirname $0)/kafka-run-class.sh kafka...

4_Selenium框架封装

1 封装WebDriver 封装代码编写 package com.selenium.test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDr...

实用向—总结一些唯一ID生成方式

在日常的项目开发中,我们经常会遇到需要生成唯一ID的业务场景,不同的业务对唯一ID的生成方式与要求都会不尽相同,一是生成方式多种多样,如UUID、雪花算法、数据库递增等;其次业务要求上也各有不同,有的只要保证唯一性即可,有的需要加上时间戳,有的要保证按顺序递增等。以下是我结合实际业务中的使用总结了几种唯一ID的生成方式,  要求就是在一般的应用场景下一方面...

C#中的多线程

原文:http://www.albahari.com/threading/part2.aspx 文章来源:http://blog.gkarch.com/threading/part2.html 1同步概要 在第 1 部分:基础知识中,我们描述了如何在线程上启动任务、配置线程以及双向传递数据。同时也说明了局部变量对于线程来说是私有的,以及引用是如何在线程之间...

Hibernate之主键生成策略

Hibernate之主键生成策略 1.1 程序员自己控制:assigned 1.2 数据库控制: identity(标识列/自动增长) sequence 1.3 hibernate控制:increment uuid/uuid.hex 1.4 其它:native hibernate.cfg.xml核心配置文件 Student.hbm.xml 1 <...

c# Winform PropertyGrid 实现下拉框 多选

1 usingPropertyGridHelpers.Controls; 2 usingSystem; 3 usingSystem.Collections.Generic; 4 usingSystem.ComponentModel; 5 usingSystem.Drawing.Design; 6 usingSystem.Windows.Forms;...