demo通用返回结果改造

This commit is contained in:
macro 2019-04-13 16:22:41 +08:00
parent cfa9871b5c
commit eca02f24bd
4 changed files with 103 additions and 59 deletions

View File

@ -69,7 +69,7 @@ public class Swagger2Config {
.build(); .build();
} }
List<SecurityReference> defaultAuth() { private List<SecurityReference> defaultAuth() {
List<SecurityReference> result = new ArrayList<>(); List<SecurityReference> result = new ArrayList<>();
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];

View File

@ -1,8 +1,10 @@
package com.macro.mall.demo.controller; package com.macro.mall.demo.controller;
import com.macro.mall.demo.dto.CommonPage;
import com.macro.mall.demo.dto.CommonResult; import com.macro.mall.demo.dto.CommonResult;
import com.macro.mall.demo.dto.PmsBrandDto; import com.macro.mall.demo.dto.PmsBrandDto;
import com.macro.mall.demo.service.DemoService; import com.macro.mall.demo.service.DemoService;
import com.macro.mall.model.PmsBrand;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -14,6 +16,8 @@ import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
/** /**
* 测试controller * 测试controller
*/ */
@ -28,24 +32,24 @@ public class DemoController {
@ApiOperation(value = "获取全部品牌列表") @ApiOperation(value = "获取全部品牌列表")
@RequestMapping(value = "/brand/listAll", method = RequestMethod.GET) @RequestMapping(value = "/brand/listAll", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public Object getBrandList() { public CommonResult<List<PmsBrand>> getBrandList() {
return new CommonResult().success(demoService.listAllBrand()); return CommonResult.success(demoService.listAllBrand());
} }
@ApiOperation(value = "添加品牌") @ApiOperation(value = "添加品牌")
@RequestMapping(value = "/brand/create", method = RequestMethod.POST) @RequestMapping(value = "/brand/create", method = RequestMethod.POST)
@ResponseBody @ResponseBody
public Object createBrand(@Validated @RequestBody PmsBrandDto pmsBrand, BindingResult result) { public CommonResult createBrand(@Validated @RequestBody PmsBrandDto pmsBrand, BindingResult result) {
if (result.hasErrors()) { if (result.hasErrors()) {
return new CommonResult().validateFailed(result.getFieldError().getDefaultMessage()); return CommonResult.validateFailed(result.getFieldError().getDefaultMessage());
} }
CommonResult commonResult; CommonResult commonResult;
int count = demoService.createBrand(pmsBrand); int count = demoService.createBrand(pmsBrand);
if (count == 1) { if (count == 1) {
commonResult = new CommonResult().success(pmsBrand); commonResult = CommonResult.success(pmsBrand);
LOGGER.debug("createBrand success:{}", pmsBrand); LOGGER.debug("createBrand success:{}", pmsBrand);
} else { } else {
commonResult = new CommonResult().failed(); commonResult = CommonResult.failed("操作失败");
LOGGER.debug("createBrand failed:{}", pmsBrand); LOGGER.debug("createBrand failed:{}", pmsBrand);
} }
return commonResult; return commonResult;
@ -54,17 +58,17 @@ public class DemoController {
@ApiOperation(value = "更新品牌") @ApiOperation(value = "更新品牌")
@RequestMapping(value = "/brand/update/{id}", method = RequestMethod.POST) @RequestMapping(value = "/brand/update/{id}", method = RequestMethod.POST)
@ResponseBody @ResponseBody
public Object updateBrand(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandDto pmsBrandDto,BindingResult result) { public CommonResult updateBrand(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandDto pmsBrandDto,BindingResult result) {
if(result.hasErrors()){ if(result.hasErrors()){
return new CommonResult().validateFailed(result.getFieldError().getDefaultMessage()); return CommonResult.validateFailed(result.getFieldError().getDefaultMessage());
} }
CommonResult commonResult; CommonResult commonResult;
int count = demoService.updateBrand(id, pmsBrandDto); int count = demoService.updateBrand(id, pmsBrandDto);
if (count == 1) { if (count == 1) {
commonResult = new CommonResult().success(pmsBrandDto); commonResult = CommonResult.success(pmsBrandDto);
LOGGER.debug("updateBrand success:{}", pmsBrandDto); LOGGER.debug("updateBrand success:{}", pmsBrandDto);
} else { } else {
commonResult = new CommonResult().failed(); commonResult = CommonResult.failed("操作失败");
LOGGER.debug("updateBrand failed:{}", pmsBrandDto); LOGGER.debug("updateBrand failed:{}", pmsBrandDto);
} }
return commonResult; return commonResult;
@ -73,29 +77,30 @@ public class DemoController {
@ApiOperation(value = "删除品牌") @ApiOperation(value = "删除品牌")
@RequestMapping(value = "/brand/delete/{id}", method = RequestMethod.GET) @RequestMapping(value = "/brand/delete/{id}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public Object deleteBrand(@PathVariable("id") Long id) { public CommonResult deleteBrand(@PathVariable("id") Long id) {
int count = demoService.deleteBrand(id); int count = demoService.deleteBrand(id);
if (count == 1) { if (count == 1) {
LOGGER.debug("deleteBrand success :id={}", id); LOGGER.debug("deleteBrand success :id={}", id);
return new CommonResult().success(null); return CommonResult.success(null);
} else { } else {
LOGGER.debug("deleteBrand failed :id={}", id); LOGGER.debug("deleteBrand failed :id={}", id);
return new CommonResult().failed(); return CommonResult.failed("操作失败");
} }
} }
@ApiOperation(value = "分页获取品牌列表") @ApiOperation(value = "分页获取品牌列表")
@RequestMapping(value = "/brand/list", method = RequestMethod.GET) @RequestMapping(value = "/brand/list", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public Object listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) { @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
return new CommonResult().pageSuccess(demoService.listBrand(pageNum, pageSize)); List<PmsBrand> brandList = demoService.listBrand(pageNum, pageSize);
return CommonResult.success(CommonPage.restPage(brandList));
} }
@ApiOperation(value = "根据编号查询品牌信息") @ApiOperation(value = "根据编号查询品牌信息")
@RequestMapping(value = "/brand/{id}", method = RequestMethod.GET) @RequestMapping(value = "/brand/{id}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public Object brand(@PathVariable("id") Long id) { public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {
return new CommonResult().success(demoService.getBrand(id)); return CommonResult.success(demoService.getBrand(id));
} }
} }

View File

@ -0,0 +1,60 @@
package com.macro.mall.demo.dto;
import com.github.pagehelper.PageInfo;
import java.util.List;
/**
* 分页数据封装类
*/
public class CommonPage<T> {
private Integer pageNum;
private Integer pageSize;
private Long totalPage;
private List<T> list;
/**
* 将PageHelper分页后的list转为分页信息
*/
public static <T> CommonPage<T> restPage(List<T> list) {
CommonPage<T> result = new CommonPage<>();
PageInfo<T> pageInfo = new PageInfo<>(list);
result.setTotalPage(pageInfo.getTotal() / pageInfo.getPageSize());
result.setPageNum(pageInfo.getPageNum());
result.setPageSize(pageInfo.getPageSize());
result.setList(pageInfo.getList());
return result;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Long getTotalPage() {
return totalPage;
}
public void setTotalPage(Long totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}

View File

@ -1,58 +1,36 @@
package com.macro.mall.demo.dto; package com.macro.mall.demo.dto;
import com.github.pagehelper.PageInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* 通用返回对象 * 通用返回对象
*/ */
public class CommonResult { public class CommonResult<T> {
public static final int SUCCESS = 0; public static final int SUCCESS = 0;
public static final int FAILED = 1; public static final int FAILED = 1;
public static final int VALIDATE_FAILED = 2; public static final int VALIDATE_FAILED = 2;
private int code; private int code;
private String message; private String message;
private Object data; private T data;
/** /**
* 普通成功返回 * 普通成功返回
* *
* @param data 获取的数据 * @param data 获取的数据
*/ */
public CommonResult success(Object data) { public static <T> CommonResult<T> success(T data) {
this.code = SUCCESS; CommonResult<T> result = new CommonResult<T>();
this.message = "操作成功"; result.setCode(SUCCESS);
this.data = data; result.setData(data);
return this; return result;
}
/**
* 返回分页成功数据
*/
public CommonResult pageSuccess(List data) {
PageInfo pageInfo = new PageInfo(data);
long totalPage = pageInfo.getTotal() / pageInfo.getPageSize();
Map<String, Object> result = new HashMap<>();
result.put("pageSize", pageInfo.getPageSize());
result.put("totalPage", totalPage);
result.put("pageNum", pageInfo.getPageNum());
result.put("list", pageInfo.getList());
this.code = SUCCESS;
this.message = "操作成功";
this.data = result;
return this;
} }
/** /**
* 普通失败提示信息 * 普通失败提示信息
*/ */
public CommonResult failed() { public static <T> CommonResult<T> failed(String message) {
this.code = FAILED; CommonResult result = new CommonResult();
this.message = "操作失败"; result.setCode(FAILED);
return this; result.setMessage(message);
return result;
} }
/** /**
@ -60,10 +38,11 @@ public class CommonResult {
* *
* @param message 错误信息 * @param message 错误信息
*/ */
public CommonResult validateFailed(String message) { public static <T> CommonResult<T> validateFailed(String message) {
this.code = VALIDATE_FAILED; CommonResult result = new CommonResult();
this.message = message; result.setCode(VALIDATE_FAILED);
return this; result.setMessage(message);
return result;
} }
public int getCode() { public int getCode() {
@ -82,11 +61,11 @@ public class CommonResult {
this.message = message; this.message = message;
} }
public Object getData() { public T getData() {
return data; return data;
} }
public void setData(Object data) { public void setData(T data) {
this.data = data; this.data = data;
} }
} }