Commit 3fd44976 by tyfanchz

1、功能开发

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