Commit 3fd44976 by tyfanchz

1、功能开发

2、细化统一异常处理方法
3、去除无用校验
1 parent 3c24d08d
package com.glsx.admin.controller;
import javax.validation.groups.Default;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.glsx.admin.exception.BusinessException;
import com.glsx.admin.exception.ResultEnum;
import com.glsx.admin.pojo.GroupA;
import com.glsx.admin.pojo.request.ProductReq;
import com.glsx.admin.pojo.response.DeviceOrderResp;
import com.glsx.admin.pojo.response.ProductResp;
......@@ -49,16 +51,13 @@ public class ProductController {
/**
* 根据 id 查询产品详情。
*
* @param id 产品 id
* @param req 请求体
* @return 结果
*/
@RequestMapping(value = "/getById", method = {RequestMethod.GET, RequestMethod.POST})
public R getById(Long id) {
log.info("$$$$ /product/getById param: {}", id);
if (id == null) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
ProductResp resp = productFeignClient.getById(id);
public R getById(@Validated({Default.class}) ProductReq req) {
log.info("$$$$ /product/getById param: {}", JSON.toJSONString(req));
ProductResp resp = productFeignClient.getById(req);
return R.ok().data(resp);
}
......@@ -66,9 +65,10 @@ public class ProductController {
* 保存产品。无则插入,有则更新。
*
* @param req 产品
* @return 结果
*/
@RequestMapping(value = "/save", method = {RequestMethod.POST})
public R save(@RequestBody @Validated ProductReq req) {
public R save(@RequestBody @Validated({GroupA.class}) ProductReq req) {
log.info("$$$$ /product/save param: {}", JSON.toJSONString(req));
productFeignClient.save(req);
return R.ok();
......@@ -77,45 +77,42 @@ public class ProductController {
/**
* 根据产品 id 获取指令集。
*
* @param id 产品 id
* @param req 请求体
* @return 结果
*/
@RequestMapping(value = "/getCommandSetById", method = {RequestMethod.GET, RequestMethod.POST})
public R getCommandSetById(String id) {
log.info("$$$$ /product/getCommandSetById param: {}", id);
if (StringUtils.isBlank(id)) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
List<DeviceOrderResp> respList = productFeignClient.getCommandSetById(id);
public R getCommandSetById(@Validated({Default.class}) ProductReq req) {
log.info("$$$$ /product/getCommandSetById param: {}", JSON.toJSONString(req));
List<DeviceOrderResp> respList = productFeignClient.getCommandSetById(req);
return R.ok().data(respList);
}
/**
* 校验商城商品 id。
*
* @param shopProductId 商品 id
* @param req 请求体
* @return 结果
*/
@RequestMapping(value = "/checkShopProductId", method = {RequestMethod.GET, RequestMethod.POST})
public R checkShopProductId(String shopProductId) {
log.info("$$$$ /product/checkShopProductId param: {}", shopProductId);
if (StringUtils.isBlank(shopProductId)) {
public R checkShopProductId(ProductReq req) {
log.info("$$$$ /product/checkShopProductId param: {}", JSON.toJSONString(req));
if (StringUtils.isBlank(req.getShopProductId())) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
Boolean productIdValid = productFeignClient.checkShopProductId(shopProductId);
Boolean productIdValid = productFeignClient.checkShopProductId(req);
return R.ok().data(productIdValid);
}
/**
* 上下架产品。
*
* @param id 产品 id
* @param req 请求体
* @return 结果
*/
@RequestMapping(value = "/putOnOff", method = {RequestMethod.GET, RequestMethod.POST})
public R putOnOff(Long id) {
log.info("$$$$ /product/putOnOff param: {}", id);
if (id == null) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
productFeignClient.putOnOff(id);
public R putOnOff(@Validated({Default.class}) ProductReq req) {
log.info("$$$$ /product/putOnOff param: {}", JSON.toJSONString(req));
productFeignClient.putOnOff(req);
return R.ok();
}
}
package com.glsx.admin.controller;
import javax.validation.groups.Default;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.glsx.admin.exception.BusinessException;
import com.glsx.admin.exception.ResultEnum;
import com.glsx.admin.pojo.request.SmartProductTypeReq;
import com.glsx.admin.pojo.GroupA;
import com.glsx.admin.pojo.request.ProductTypeReq;
import com.glsx.admin.pojo.response.ProductTypeResp;
import com.glsx.admin.remote.ProductTypeFeignClient;
import com.glsx.plat.core.web.R;
......@@ -38,7 +38,7 @@ public class ProductTypeController {
* @return 结果
*/
@RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
public R getList(SmartProductTypeReq req) {
public R getList(ProductTypeReq req) {
log.info("$$$$ /productType/getList param: {}", JSON.toJSONString(req));
PageInfo<ProductTypeResp> pageInfo = productTypeFeignClient.getList(req);
return R.ok().putPageData(pageInfo);
......@@ -47,16 +47,13 @@ public class ProductTypeController {
/**
* 根据 id 获取产品类型。
*
* @param id 产品类型 id
* @param req 请求体
* @return 结果
*/
@RequestMapping(value = "/getById", method = {RequestMethod.GET, RequestMethod.POST})
public R getById(Long id) {
log.info("$$$$ /productType/getById param: {}", id);
if (id == null) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
ProductTypeResp resp = productTypeFeignClient.getById(id);
public R getById(@Validated({Default.class}) ProductTypeReq req) {
log.info("$$$$ /productType/getById param: {}", JSON.toJSONString(req));
ProductTypeResp resp = productTypeFeignClient.getById(req);
return R.ok().data(resp);
}
......@@ -64,16 +61,17 @@ public class ProductTypeController {
* 保存产品类型。
*
* @param req 产品类型
* @return 结果
*/
@RequestMapping(value = "/save", method = {RequestMethod.POST})
public R save(@RequestBody @Validated SmartProductTypeReq req) {
public R save(@RequestBody @Validated({GroupA.class}) ProductTypeReq req) {
log.info("$$$$ /productType/save param: {}", JSON.toJSONString(req));
productTypeFeignClient.save(req);
return R.ok();
}
/**
* 获取商城标签
* 获取商城标签。
*
* @return 结果
*/
......
package com.glsx.admin.exception;
import java.util.List;
import java.util.concurrent.TimeoutException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.StringUtils;
......@@ -8,6 +10,7 @@ import com.netflix.hystrix.exception.HystrixRuntimeException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
......@@ -33,7 +36,26 @@ public class GlobalExceptionHandler {
BindException.class})
public R handleBindException(Exception e) {
log.error("!!!! 参数错误", e);
return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
if ((e instanceof MethodArgumentNotValidException) || (e instanceof BindException)) {
List<FieldError> errorList;
if (e instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
errorList = ex.getBindingResult().getFieldErrors();
} else {
BindException ex = (BindException) e;
errorList = ex.getFieldErrors();
}
StringBuffer messageSb = new StringBuffer();
for (FieldError error : errorList) {
String fieldMessage = String.format("%s: %s, ",
error.getField(), error.getDefaultMessage());
messageSb.append(fieldMessage);
}
return R.error(ResultEnum.PARAM_ERROR.getCode(),
messageSb.toString().replaceAll(", +$", ""));
} else {
return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
}
}
@ExceptionHandler({HystrixRuntimeException.class})
......@@ -42,6 +64,10 @@ public class GlobalExceptionHandler {
if (cause instanceof FeignException) {
FeignException feignException = (FeignException) cause;
return this.handleFeignException(feignException);
} else if (cause instanceof TimeoutException) {
log.error("!!!! 服务调用超时", e);
return R.error(ResultEnum.REMOTE_SERVER_TIME_OUT.getCode(),
ResultEnum.REMOTE_SERVER_TIME_OUT.getMessage());
} else {
log.error("!!!! Hystrix熔断错误", e);
return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
......
......@@ -19,6 +19,7 @@ public enum ResultEnum {
PRODUCT_TYPE_NOT_FOUND(3001, "产品类型ID未找到"),
REMOTE_SERVICE_ERROR(9001, "服务调用错误"),
REMOTE_SERVER_TIME_OUT(9002, "远程服务超时,请重试"),
;
private final Integer code;
......
package com.glsx.admin.pojo.request;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import java.util.Date;
import com.glsx.admin.pojo.BaseDTO;
import com.glsx.admin.pojo.GroupA;
import lombok.Data;
/**
......@@ -22,42 +25,49 @@ public class ProductReq extends BaseDTO {
/**
* 标识ID
*/
@NotNull(message = "id不能为空", groups = {Default.class})
private Long id;
/**
* 产品名称
*/
@NotBlank
@NotBlank(message = "产品名称不能为空", groups = {GroupA.class})
private String productName;
/**
* 图片ID 关联smart_sys_file中id
*/
@NotNull(message = "缩略图不能为空", groups = {GroupA.class})
private Long icon;
/**
* 产品类型ID 关联smart_product_type中id
*/
@NotNull(message = "产品类型不能为空", groups = {GroupA.class})
private Long typeId;
/**
* 商城产品id
*/
@NotBlank(message = "商品id不能为空", groups = {GroupA.class})
private String shopProductId;
/**
* 接入平台key 关联smart_sys_code中dict_key
*/
@NotBlank(message = "接入平台不能为空", groups = {GroupA.class})
private String platformKey;
/**
* 接入平台产品id
*/
@NotBlank(message = "产品id不能为空", groups = {GroupA.class})
private String platformProductId;
/**
* 配网方式key 关联smart_sys_code中dict_key
*/
@NotBlank(message = "配网模式不能为空", groups = {GroupA.class})
private String networkKey;
/**
......
package com.glsx.admin.pojo.request;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import java.util.Date;
import com.glsx.admin.pojo.BaseDTO;
import com.glsx.admin.pojo.GroupA;
import lombok.Data;
/**
......@@ -18,26 +21,29 @@ import lombok.Data;
* 车家智能产品类型表
*/
@Data
public class SmartProductTypeReq extends BaseDTO {
public class ProductTypeReq extends BaseDTO {
/**
* 标识ID
*/
@NotNull(message = "id不能为空", groups = {Default.class})
private Long id;
/**
* 类型名称
*/
@NotBlank
@NotBlank(message = "产品类型名称不能为空", groups = {GroupA.class})
private String typeName;
/**
* 类别key 关联smart_sys_code中dict_key
*/
@NotBlank(message = "分类不能为空", groups = {GroupA.class})
private String categoryKey;
/**
* 商城标签
*/
@NotBlank(message = "商城标签不能为空", groups = {GroupA.class})
private String shopTag;
/**
......
......@@ -10,7 +10,6 @@ import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
......@@ -28,7 +27,7 @@ public interface ProductFeignClient {
@RequestMapping(value = "/product/getById", method = {RequestMethod.GET})
@ResponseBody
ProductResp getById(@RequestParam Long id);
ProductResp getById(@SpringQueryMap ProductReq req);
@RequestMapping(value = "/product/save", method = {RequestMethod.POST})
@ResponseBody
......@@ -36,13 +35,13 @@ public interface ProductFeignClient {
@RequestMapping("/product/getCommandSetById")
@ResponseBody
List<DeviceOrderResp> getCommandSetById(@RequestParam String id);
List<DeviceOrderResp> getCommandSetById(@SpringQueryMap ProductReq req);
@RequestMapping(value = "/product/checkShopProductId", method = {RequestMethod.GET})
@ResponseBody
Boolean checkShopProductId(@RequestParam String shopProductId);
Boolean checkShopProductId(@SpringQueryMap ProductReq req);
@RequestMapping(value = "/product/putOnOff", method = {RequestMethod.POST})
@ResponseBody
void putOnOff(@RequestParam Long id);
void putOnOff(@SpringQueryMap ProductReq req);
}
......@@ -2,14 +2,13 @@ package com.glsx.admin.remote;
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.glsx.admin.pojo.request.SmartProductTypeReq;
import com.glsx.admin.pojo.request.ProductTypeReq;
import com.glsx.admin.pojo.response.ProductTypeResp;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
......@@ -23,15 +22,15 @@ import org.springframework.web.bind.annotation.ResponseBody;
public interface ProductTypeFeignClient {
@RequestMapping(value = "/productType/getList", method = {RequestMethod.GET})
@ResponseBody
PageInfo<ProductTypeResp> getList(@SpringQueryMap SmartProductTypeReq req);
PageInfo<ProductTypeResp> getList(@SpringQueryMap ProductTypeReq req);
@RequestMapping(value = "/productType/getById", method = {RequestMethod.GET})
@ResponseBody
ProductTypeResp getById(@RequestParam Long id);
ProductTypeResp getById(@SpringQueryMap ProductTypeReq req);
@RequestMapping(value = "/productType/save", method = {RequestMethod.POST})
@ResponseBody
void save(@RequestBody SmartProductTypeReq req);
void save(@RequestBody ProductTypeReq req);
@RequestMapping(value = "/productType/getShopTagList", method = {RequestMethod.GET})
@ResponseBody
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!