swagger 返回json字符串_【swagger】2.swagger提供开发者文档--返回统一格式篇【spring mvc】【spring boot】...

接着上一篇来说,

不管正常返回结果还是后台出现异常,应该返回给前台统一的响应格式。

所以这一篇就为了应对解决这个问题。

========================================================================

1.首先,定义一个统一返回类【所有返回的格式都是这个类的格式】

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.sxd.sweeping.response;importcom.alibaba.fastjson.JSON;import lombok.*;importjava.io.Serializable;importjava.util.Objects;/*** 统一JSON返回类

*@authorsxd

*@since2018/4/1*/@Getter

@Setter

@NoArgsConstructor

@AllArgsConstructorpublic class GenericResponse implementsSerializable{/*** 程序定义状态码*/

private intcode;/*** 必要的提示信息*/

privateString message;/*** 业务数据*/

privateObject datas;/*** 对业务数据单独处理

*@return

*/@OverridepublicString toString() {if(Objects.isNull(this.datas)){this.setDatas(newObject());

}return JSON.toJSONString(this);

}

}

View Code

2.定义统一返回数据格式【这里定义了一些常用返回code+msg,用于填充上面的统一格式】

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.sxd.sweeping.response;importcom.google.common.collect.Maps;importjava.util.Map;/*** 统一返回客户端数据格式

*@authorsxd

*@since2018/4/1*/

public classResponseFormat {private static Map messageMap =Maps.newHashMap();//初始化状态码与文字说明

static{/*成功状态码*/messageMap.put(200, "成功");/*服务器错误*/messageMap.put(1000,"服务器错误");/*参数错误:10001-19999*/messageMap.put(10001, "参数无效");

messageMap.put(10002, "参数为空");

messageMap.put(10003, "参数类型错误");

messageMap.put(10004, "参数缺失");/*用户错误:20001-29999*/messageMap.put(20001, "用户未登录");

messageMap.put(20002, "账号不存在或密码错误");

messageMap.put(20003, "账号已被禁用");

messageMap.put(20004, "用户不存在");

messageMap.put(20005, "用户已存在");/*业务错误:30001-39999*/messageMap.put(30001, "某业务出现问题");/*系统错误:40001-49999*/messageMap.put(40001, "系统繁忙,请稍后重试");/*数据错误:50001-599999*/messageMap.put(50001, "数据未找到");

messageMap.put(50002, "数据有误");

messageMap.put(50003, "数据已存在");

messageMap.put(50004,"查询出错");/*接口错误:60001-69999*/messageMap.put(60001, "内部系统接口调用异常");

messageMap.put(60002, "外部系统接口调用异常");

messageMap.put(60003, "该接口禁止访问");

messageMap.put(60004, "接口地址无效");

messageMap.put(60005, "接口请求超时");

messageMap.put(60006, "接口负载过高");/*权限错误:70001-79999*/messageMap.put(70001, "无权限访问");

}public staticGenericResponse retParam(Integer status,Object data) {

GenericResponse json= newGenericResponse(status, messageMap.get(status), data);returnjson;

}

}

View Code

3.定义自己的异常类 因为spring 对于 RuntimeException 异常才会进行事务回滚,所以继承的是RuntimeException【可用于代码中自己throw异常】

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.sxd.sweeping.handler;importlombok.Getter;importlombok.Setter;/*** spring 对于 RuntimeException 异常才会进行事务回滚。

*@authorsxd

*@since2018/4/1*/@Getter

@Setterpublic class MyException extendsRuntimeException {publicMyException(Integer code, Exception exception) {this.code =code;this.exception =exception;

}privateInteger code;privateException exception;

}

View Code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger Maven Plugin是一个用于生成Swagger接口文档的Maven插件。它可以帮助开发人员在构建项目时自动生成Swagger规范的JSON或YAML文件,以便于API文档的管理和使用。 使用Swagger Maven Plugin生成接口文档swagger.jsonswagger.yaml的步骤如下: 1. 在项目的pom.xml文件中添加Swagger Maven Plugin的依赖配置: ```xml <build> <plugins> <plugin> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.1.8</version> <configuration> <!-- 配置Swagger文档的基本信息 --> <apiSources> <apiSource> <springmvc>true</springmvc> <locations>com.example.controller</locations> <basePath>/api</basePath> <info> <title>API文档</title> <version>1.0.0</version> <description>API接口文档</description> <termsOfServiceUrl>http://example.com/terms-of-service</termsOfServiceUrl> <contact> <email>contact@example.com</email> </contact> <license> <name>Apache 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.html</url> </license> </info> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 在项目根目录下执行以下命令生成Swagger接口文档: ``` mvn compile swagger:generate ``` 3. 执行完上述命令后,Swagger Maven Plugin会根据配置的信息扫描项目中的接口,并生成Swagger规范的JSON或YAML文件。生成的文件默认保存在项目的target目录下的swagger目录中。 生成的Swagger接口文档可以通过访问http://localhost:8080/api/swagger-ui.html(假设项目部署在本地的8080端口)来查看和测试API接口。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值