使用类对象返回统一格式的JSON
1.创建ServerResponse实体类 如下:
类中具体需要哪些方法 可以看情况而定
package com.lanou.util;
import java.io.Serializable;
public class ServerResponse implements Serializable {
private int status;
private String msg;
private T data;
private ServerResponse(int status,T data,String msg){
this.status = status;
this.data = data;
this.msg = msg;
}
private ServerResponse(int status,String msg){
this.status = status;
this.msg = msg;
}
public static ServerResponse createSuccess(T data){
return new ServerResponse(0,data,"成功");
}
public static ServerResponse createSuccess(String msg,T data){
return new ServerResponse(0,data,msg);
}
public static ServerResponse createError(int status,String msg,T data){
return new ServerResponse(status,data,msg);
}
public static ServerResponse createError(int status,String msg){
return new ServerResponse(status,msg);
}
public static ServerResponse createError(int status){
return new ServerResponse(status,"失败");
}
public ServerResponse() {
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ServerResponse{" +
"status=" + status +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
2.实体类创建完成 在controller方法中就可以使用该类来返回统一的JSON格式 如下:
@ApiOperation(value = "查询所用户",notes = "所有用户")
@RequestMapping(value = "/findUser",method = RequestMethod.POST)
public ServerResponse> findUser(){
List users = userMapper.selectAll();
return ServerResponse.createSuccess("数据记录数为"+users.size(), users);
}
返回格式如下所示:
birthday是因为为data类型,没有转格式,所以出现以下情况
{
"status": 0,
"msg": "数据记录数为7",
"data": [{
"id": 1,
"username": "dp",
"password": "202cb962ac59075b964b07152d234b70",
"email": "qq@123",
"birthday": 823536000000,
"deleteid": 1
}, {
"id": 2,
"username": "gs",
"password": "457",
"email": "qq@456",
"birthday": 803404800000,
"deleteid": 1
}, {
"id": 3,
"username": "xm",
"password": "789",
"email": "qq@789",
"birthday": 777484800000,
"deleteid": 1
}]
}
自动生成接口文档
1.引入jar包依赖
io.springfox
springfox-swagger2
2.6.1
io.springfox
springfox-swagger-ui
2.6.1
com.github.xiaoymin
swagger-bootstrap-ui
1.6
2.在util包下创建Swagger2类
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.ApiInfo
import springfox.documentation.service.Contact
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.lanou.controller"))
.paths(PathSelectors.any())
.build()
}
private ApiInfo apiInfo(){
Contact contact = new Contact("lck","api","[email protected]")
return new ApiInfo("lckApi","lckdec",
"1.0.0","sha?",contact,
"license","licenseUrl")
}
}
3.在controller中使用@ApiIgnore和@ApiOperation来标识是否要为该接口生成文档 如下所示:
@ApiOperation(value = "查询所用户",notes = "所有用户")
@RequestMapping(value = "/findUser",method = RequestMethod.POST)
public ServerResponse> findUser(){
List users = userMapper.selectAll()
// 使用类对象返回统一格式的JSON
return ServerResponse.createSuccess("数据记录数为"+users.size(), users)
}
@ApiIgnore
@RequestMapping("/addUser")
public ServerResponse addUser(User user){
int count = userMapper.insert(user)
if (count == 1){
// 使用类对象返回统一格式的JSON
return ServerResponse.createSuccess("数据添加成功")
}
return ServerResponse.createError(10,"数据添加失败")
}
设置完成后 在本地服务器上 可以在http://localhost:8095/swagger-ui.html和
http://localhost:8095/doc.html路径下查看自动生成的接口文档
注意:如果在controller方法中没有标明使用哪种请求 那么自动生成的接口文档 会把所有请求的接口文档都自动生成一遍