编辑一个SUCCESS类和ERROR类,他们都有state、msg、result,那么就创建一个公用的父类base。
创建【com.item.res】包
Base:
package com.item.res; public class BASE { private boolean state; private String msg; private Object result; public BASE(boolean state, String msg, Object result) { this.state = state; this.msg = msg; this.result = result; } public boolean isState() { return state; } public void setState(boolean state) { this.state = state; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getResult() { return result; } public void setResult(Object result) { this.result = result; } }
ERROR:
package com.item.res; public class ERROR extends BASE { public ERROR(String msg, Object result) { super(false, msg, result); } }
SUCCESS:
package com.item.res; public class SUCCESS extends BASE { public SUCCESS(Object result) { super(false, "操作成功", result); } }
返回修改:
package com.item.controller; import com.item.model.Users; import com.item.res.ERROR; import com.item.res.SUCCESS; import com.item.service.UsersService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.List; @Api("用户操作接口") @RestController @CrossOrigin public class UsersController { @Autowired private UsersService usersService; /** * 获取所有信息 * @return */ @GetMapping("/GetInfoApi") @ApiOperation(value = "获取信息",notes = "没啥留言的") public Object GetInfoApi(){ List<Users> list=usersService.GetInfo(); return new SUCCESS(list); } @GetMapping("/GetName") @ApiOperation(value = "获取信息",notes = "没啥留言的") @ApiImplicitParams({ @ApiImplicitParam(name = "nickName",required = true,paramType = "query",dataType = "String",value = "通过昵称模糊查询") }) public Object GetName(HttpServletRequest request,Model model){ String nickName = request.getParameter("nickName"); List<Users> list=usersService.SelectName(nickName); return new SUCCESS(list); } /** * 添加信息 * @param userName * @param pwd * @param nickName * @return */ @PostMapping(value = "/UserAddInfoApi") @ApiOperation(value = "添加",notes = "没啥留言的") @ApiImplicitParams({ @ApiImplicitParam(name = "userName",required = true,paramType = "query",dataType = "String",value = "用户名"), @ApiImplicitParam(name = "pwd",required = true,paramType = "query",dataType = "String",value = "密码"), @ApiImplicitParam(name = "nickName",required = true,paramType = "query",dataType = "String",value = "昵称") }) public Object UserAddInfoApi(String userName,String pwd,String nickName){ HashMap<String,Object> map=new HashMap<String,Object>(); if( StringUtils.isEmpty(userName)|| StringUtils.isEmpty(pwd)|| StringUtils.isEmpty(nickName) ){ return new ERROR("参数为空","参数错误"); } usersService.UsersAddInfo(userName, pwd, nickName); return new SUCCESS("添加成功"); } /** * 单个查询 * @param id * @return */ @GetMapping("/UsersSelectById") @ApiOperation(value = "id查询",notes = "没啥留言的") @ApiImplicitParams({ @ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号") }) public Object UsersSelectById(String id){ Users users = usersService.UsersSelectById(Integer.parseInt(id)); HashMap<String,Object> map=new HashMap<String,Object>(); map.put("state",true); map.put("msg","成功"); map.put("result",users); return map; } /** * 修改api * @param id * @param pwd * @return */ @PostMapping(value = "/UserUpdateInfoApi") @ApiOperation(value = "添加",notes = "没啥留言的") @ApiImplicitParams({ @ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号"), @ApiImplicitParam(name = "pwd",required = true,paramType = "query",dataType = "String",value = "密码"), }) public Object UserUpdateInfoApi(String id,String pwd){ usersService.UsersUpdateInfo(pwd,Integer.parseInt(id)); HashMap<String,Object> map=new HashMap<String,Object>(); map.put("state",true); map.put("msg","成功"); map.put("result",""); return map; } /** * 删除api * @param id * @return */ @GetMapping(value = "/UsersDeleteById") @ApiOperation(value = "根据id删除",notes = "没啥留言的") @ApiImplicitParams({ @ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号") }) public Object UsersDeleteById(String id){ usersService.UsersDeleteById(Integer.parseInt(id)); HashMap<String,Object> map=new HashMap<String,Object>(); map.put("state",true); map.put("msg","成功"); map.put("result",""); return map; } }
使用swagger访问测试返回效果如下:
设置完成。
注意问题:
每个人的习惯方式均不同,需要看看公司具体用什么方式:
也可能是这样返回:
{
“code”: -9999,
“message”: “Invalid Request”,
“data”:{ }
}
注意:无论是【ERROR】还是【SUCCESS】他们的返回结果都需要一致,否则前端在处理的时候就会很麻烦,对是一套解析,错又是一套解析,很麻烦。
例如:
正确返回:
{ "state": true, "message": "访问成功", "data": [ { "id": 77, "userName": "子玉等于摸鱼", "pwd": "074FD28EFF0F5ADEA071694061739E55", "nickName": "高大上,牛逼吼吼吼吼吼" } ] }
错误返回:
{ "state": false, "message": "访问失败", "data":"失败" }
这就没法玩了。。。。 会挨骂的。
避免层级过深的URI
/ 在url中表达层级,用于按实体关联关系进行对象导航,一般根据id导航。
过深的导航容易导致url膨胀,不易维护,如 GET /zoos/1/areas/3/animals/4,尽量使用查询参数代替路劲中的实体导航,如GET /animals?zoo=1&area=3。
结果过滤,排序,搜索
url最好越简短越好,对结果过滤、排序、搜索相关的功能都应该通过参数实现。
过滤:例如你想限制GET /tickets 的返回结果:只返回那些open状态的ticket, GET /tickets?state=open 这里的state就是过滤参数。
排序:和过滤一样,一个好的排序参数应该能够描述排序规则,而不和业务相关。复杂的排序规则应该通过组合实现。排序参数通过 , 分隔,排序参数前加 - 表示降序排列。