diff --git a/common/core/src/main/java/com/springboot/cloud/common/core/entity/form/BaseForm.java b/common/core/src/main/java/com/springboot/cloud/common/core/entity/form/BaseForm.java index d3f713102bddb7fb32aba0aeb1b6b97db50582ba..774edc5c0bb5fadf63ccc8ad925a18974695eb11 100644 --- a/common/core/src/main/java/com/springboot/cloud/common/core/entity/form/BaseForm.java +++ b/common/core/src/main/java/com/springboot/cloud/common/core/entity/form/BaseForm.java @@ -1,4 +1,29 @@ package com.springboot.cloud.common.core.entity.form; -public class BaseForm { +import com.springboot.cloud.common.core.entity.po.BasePo; +import io.swagger.annotations.ApiModel; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; + +@ApiModel +@Slf4j +public class BaseForm { + /** + * From转化为Po,进行后续业务处理 + * + * @param clazz + * @return + */ + public T toPo(Class clazz) { + T t = null; + try { + t = clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + log.error("Po NewInstance Error"); + } + BeanUtils.copyProperties(this, t); + return t; + } + + } diff --git a/common/core/src/main/java/com/springboot/cloud/common/core/entity/form/BaseQueryForm.java b/common/core/src/main/java/com/springboot/cloud/common/core/entity/form/BaseQueryForm.java new file mode 100644 index 0000000000000000000000000000000000000000..1a8202b5464fadfb82eebb6f616aae25f501fe82 --- /dev/null +++ b/common/core/src/main/java/com/springboot/cloud/common/core/entity/form/BaseQueryForm.java @@ -0,0 +1,29 @@ +package com.springboot.cloud.common.core.entity.form; + +import com.springboot.cloud.common.core.entity.param.BaseParam; +import io.swagger.annotations.ApiModel; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; + +@ApiModel +@Slf4j +public class BaseQueryForm

extends BaseForm { + + /** + * Form转化为Param + * + * @param clazz + * @return + */ + public P toParam(Class

clazz) { + P p = null; + try { + p = clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + log.error("Param NewInstance Error"); + } + BeanUtils.copyProperties(this, p); + return p; + } + +} diff --git a/common/core/src/main/java/com/springboot/cloud/common/core/entity/po/BasePo.java b/common/core/src/main/java/com/springboot/cloud/common/core/entity/po/BasePo.java index f8022a87ce786614ba2f60a319234af26c1fd4b7..7d426bd1d3c0690db97815c43a9d5cb95b2da6bb 100644 --- a/common/core/src/main/java/com/springboot/cloud/common/core/entity/po/BasePo.java +++ b/common/core/src/main/java/com/springboot/cloud/common/core/entity/po/BasePo.java @@ -2,13 +2,16 @@ package com.springboot.cloud.common.core.entity.po; import lombok.Data; +import java.io.Serializable; +import java.time.ZonedDateTime; import java.util.Date; @Data -public class BasePo { +public class BasePo implements Serializable { + public final static String DEFAULT_USERNAME = "system"; private Long id; - private String createdBy; - private String updatedBy; - private Date createdTime; - private Date updatedTime; + private String createdBy = DEFAULT_USERNAME; + private String updatedBy = DEFAULT_USERNAME; + private Date createdTime = Date.from(ZonedDateTime.now().toInstant()); + private Date updatedTime = Date.from(ZonedDateTime.now().toInstant()); } diff --git a/common/core/src/main/java/com/springboot/cloud/common/core/entity/vo/Result.java b/common/core/src/main/java/com/springboot/cloud/common/core/entity/vo/Result.java index a8288fe690dcc951dce2b85c828b9509d9a19916..fd1a8b60202ebe00de309249f36c1e8f34f27c4e 100644 --- a/common/core/src/main/java/com/springboot/cloud/common/core/entity/vo/Result.java +++ b/common/core/src/main/java/com/springboot/cloud/common/core/entity/vo/Result.java @@ -1,6 +1,8 @@ package com.springboot.cloud.common.core.entity.vo; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.springboot.cloud.common.core.exception.BaseException; +import com.springboot.cloud.common.core.exception.ErrorType; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Getter; @@ -14,8 +16,6 @@ public class Result { public static final String SUCCESSFUL_CODE = "000000"; public static final String SUCCESSFUL_MESG = "处理成功"; - public static final String ERROR_CODE = "-1"; - public static final String ERROR_MESG = "系统异常"; @ApiModelProperty(value = "处理结果code", required = true) private String code; @@ -27,26 +27,39 @@ public class Result { private T data; public Result() { + this.timestamp = ZonedDateTime.now().toInstant(); } /** - * @param code - * @param mesg + * @param errorType */ - public Result(String code, String mesg) { - this.code = code; - this.mesg = mesg; + public Result(ErrorType errorType) { + this.code = errorType.getCode(); + this.mesg = errorType.getMesg(); this.timestamp = ZonedDateTime.now().toInstant(); } /** + * @param errorType + * @param data + */ + public Result(ErrorType errorType, T data) { + this(errorType); + this.data = data; + } + + /** + * 内部使用,用于构造成功的结果 + * * @param code * @param mesg * @param data */ - public Result(String code, String mesg, T data) { - this(code, mesg); + private Result(String code, String mesg, T data) { + this.code = code; + this.mesg = mesg; this.data = data; + this.timestamp = ZonedDateTime.now().toInstant(); } /** @@ -65,7 +78,7 @@ public class Result { * @return Result */ public static Result success() { - return new Result(SUCCESSFUL_CODE, SUCCESSFUL_MESG); + return success(null); } /** @@ -74,7 +87,17 @@ public class Result { * @return Result */ public static Result fail() { - return new Result(ERROR_CODE, ERROR_MESG); + return new Result(ErrorType.SYSTEM_ERROR); + } + + /** + * 系统异常类没有返回数据 + * + * @param baseException + * @return Result + */ + public static Result fail(BaseException baseException) { + return fail(baseException, null); } /** @@ -83,22 +106,42 @@ public class Result { * @param data * @return Result */ - public static Result fail(Object data) { - return new Result<>(ERROR_CODE, ERROR_MESG, data); + public static Result fail(BaseException baseException, Object data) { + return new Result<>(baseException.getErrorType(), data); } /** * 系统异常类并返回结果数据 * - * @param code - * @param mesg + * @param errorType * @param data * @return Result */ - public static Result fail(String code, String mesg, Object data) { - return new Result<>(code, mesg, data); + public static Result fail(ErrorType errorType, Object data) { + return new Result<>(errorType, data); + } + + /** + * 系统异常类并返回结果数据 + * + * @param errorType + * @return Result + */ + public static Result fail(ErrorType errorType) { + return Result.fail(errorType, null); } + /** + * 系统异常类并返回结果数据 + * + * @param data + * @return Result + */ + public static Result fail(Object data) { + return new Result<>(ErrorType.SYSTEM_ERROR, data); + } + + /** * 成功code=000000 * diff --git a/common/core/src/main/java/com/springboot/cloud/common/core/exception/BaseException.java b/common/core/src/main/java/com/springboot/cloud/common/core/exception/BaseException.java index 156dc745eab0a1eab8115c5e848d5760733881aa..cd71379295ee90ab6d79a1fc94eb62022dd8cc3c 100644 --- a/common/core/src/main/java/com/springboot/cloud/common/core/exception/BaseException.java +++ b/common/core/src/main/java/com/springboot/cloud/common/core/exception/BaseException.java @@ -1,35 +1,32 @@ -package com.springboot.cloud.common.core.entity.exception; +package com.springboot.cloud.common.core.exception; import lombok.Getter; -/** - * Created by zhoutaoo on 2018/5/31. - */ @Getter public class BaseException extends RuntimeException { + /** + * 异常对应的错误类型 + */ + private ErrorType errorType; - private String code; - private String mesg; - - public BaseException(String code, String mesg, String message, Throwable cause) { - super(message, cause); - this.code = code; - this.mesg = mesg; + /** + * 默认是系统异常 + */ + public BaseException() { + this.errorType = ErrorType.SYSTEM_ERROR; } - public BaseException(String code, String mesg, String message) { - super(message); - this.code = code; - this.mesg = mesg; + public BaseException(ErrorType errorType) { + this.errorType = errorType; } - public BaseException(String code, String mesg) { - this(code); - this.mesg = mesg; + public BaseException(ErrorType errorType, String message) { + super(message); + this.errorType = errorType; } - public BaseException(String code) { - super(); - this.code = code; + public BaseException(ErrorType errorType, String message, Throwable cause) { + super(message, cause); + this.errorType = errorType; } } diff --git a/common/core/src/main/java/com/springboot/cloud/common/core/exception/ErrorType.java b/common/core/src/main/java/com/springboot/cloud/common/core/exception/ErrorType.java new file mode 100644 index 0000000000000000000000000000000000000000..d8e8b24704267ff68942822d0e9a71e695be6ec4 --- /dev/null +++ b/common/core/src/main/java/com/springboot/cloud/common/core/exception/ErrorType.java @@ -0,0 +1,31 @@ +package com.springboot.cloud.common.core.exception; + +import lombok.Getter; + +/** + * Created by zhoutaoo on 2018/6/2. + */ +@Getter +public enum ErrorType { + + SYSTEM_ERROR("-1", "系统异常"), + + SYSTEM_BUSY("000001", "系统繁忙,请稍候再试"), + + ARGUMENT_NOT_VALID("010000", "请求参数校验不通过"), + UPLOAD_FILE_SIZE_LIMIT("010001", "上传文件大小超过限制"); + + /** + * 错误类型码 + */ + private String code; + /** + * 错误类型描述信息 + */ + private String mesg; + + ErrorType(String code, String mesg) { + this.code = code; + this.mesg = mesg; + } +} diff --git a/common/core/src/main/java/com/springboot/cloud/common/core/exception/ServiceException.java b/common/core/src/main/java/com/springboot/cloud/common/core/exception/ServiceException.java new file mode 100644 index 0000000000000000000000000000000000000000..0742efbd958994694b0c99f0ce19ac71e1a769f0 --- /dev/null +++ b/common/core/src/main/java/com/springboot/cloud/common/core/exception/ServiceException.java @@ -0,0 +1,11 @@ +package com.springboot.cloud.common.core.exception; + +/** + * Created by zhoutaoo on 2018/6/2. + */ +public class ServiceException extends BaseException { + + //TODO 对业务异常的返回码进行校验,规范到一定范围内 + + +} diff --git a/gateway/gateway-web/src/main/java/com/springboot/cloud/gateway/filter/AccessGatewayFilter.java b/gateway/gateway-web/src/main/java/com/springboot/cloud/gateway/filter/AccessGatewayFilter.java index 41c4c70f13e8e14921abd00100941ec18e21c19b..483f891151f1950cd2dc33138759b000c803a975 100644 --- a/gateway/gateway-web/src/main/java/com/springboot/cloud/gateway/filter/AccessGatewayFilter.java +++ b/gateway/gateway-web/src/main/java/com/springboot/cloud/gateway/filter/AccessGatewayFilter.java @@ -23,7 +23,7 @@ import reactor.core.publisher.Mono; public class AccessGatewayFilter implements GlobalFilter { @Autowired - IAuthService authService; + private IAuthService authService; /** * 1.首先网关检查token是否有效,无效直接返回401,不调用签权服务 diff --git a/gateway/gateway-web/src/main/java/com/springboot/cloud/gateway/service/impl/AuthService.java b/gateway/gateway-web/src/main/java/com/springboot/cloud/gateway/service/impl/AuthService.java index 9bac555eb9d34e9625eda06cf7026904f0b51acc..dc36d506053d8e5bea5262b70fc24f126bdb6a21 100644 --- a/gateway/gateway-web/src/main/java/com/springboot/cloud/gateway/service/impl/AuthService.java +++ b/gateway/gateway-web/src/main/java/com/springboot/cloud/gateway/service/impl/AuthService.java @@ -16,15 +16,12 @@ import org.springframework.stereotype.Service; import java.util.Optional; import java.util.stream.Stream; -/** - * Created by zhoutaoo on 2018/5/27. - */ @Service @Slf4j public class AuthService implements IAuthService { @Autowired - AuthProvider authProvider; + private AuthProvider authProvider; /** * Authorization认证开头是"bearer " diff --git a/services/producer/src/main/java/com/springboot/services/producer/ProducerApplication.java b/services/producer/src/main/java/com/springboot/services/producer/ProducerApplication.java index debac6ff00cac0716c2e46631c509fc17c68db98..a65a5f6de2121b6b4263f013ca4d87a1a01af882 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/ProducerApplication.java +++ b/services/producer/src/main/java/com/springboot/services/producer/ProducerApplication.java @@ -9,7 +9,6 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @EnableCircuitBreaker public class ProducerApplication { - public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); } diff --git a/services/producer/src/main/java/com/springboot/services/producer/SwaggerConfig.java b/services/producer/src/main/java/com/springboot/services/producer/SwaggerConfig.java index dd5df6a5032f2290210e80a94d3115c55d2525a4..89692d20a1760fb1b10ef6e6440446bf5f3e9233 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/SwaggerConfig.java +++ b/services/producer/src/main/java/com/springboot/services/producer/SwaggerConfig.java @@ -18,7 +18,7 @@ public class SwaggerConfig { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() - .apis(RequestHandlerSelectors.basePackage("com.springboot.producer")) + .apis(RequestHandlerSelectors.basePackage("com.springboot.services.producer")) .paths(PathSelectors.any()) .build(); } diff --git a/services/producer/src/main/java/com/springboot/services/producer/dao/ProductMapper.java b/services/producer/src/main/java/com/springboot/services/producer/dao/ProductMapper.java index 365bfa15782809b4c1306e42b2c23e1daeb20a2b..e50f760f4aff8d332a90761b78004383189fe8f0 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/dao/ProductMapper.java +++ b/services/producer/src/main/java/com/springboot/services/producer/dao/ProductMapper.java @@ -1,7 +1,7 @@ package com.springboot.services.producer.dao; -import com.springboot.services.producer.entity.po.Product; import com.springboot.services.producer.entity.param.ProductQueryParam; +import com.springboot.services.producer.entity.po.Product; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; @@ -11,7 +11,8 @@ import java.util.List; @Repository public interface ProductMapper { - @Insert("insert into products(name,description,updated_time,created_time,updated_by,created_by) values(#{name},#{description},now(),now(),#{updatedBy},#{createdBy})") + @Insert("insert into products(name,description,updated_time,created_time,updated_by,created_by)" + + " values(#{name},#{description},now(),now(),#{updatedBy},#{createdBy})") int insert(Product product); @Delete("delete from products where id=#{id}") @@ -20,9 +21,20 @@ public interface ProductMapper { @Update("update products set name=#{name},updated_time=now() where id=#{id}") void update(Product product); - @Select("select id,name,description,updated_time,created_time,updated_by,created_by from products where id=#{id}") + @Select("select id,name,description,updated_time,created_time,updated_by,created_by" + + " from products where id=#{id}") Product select(long id); - @Select("select id,name,description,updated_time,created_time,updated_by,created_by from products where name=#{name}") + @Select("") List query(ProductQueryParam productQueryParam); } \ No newline at end of file diff --git a/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductAddForm.java b/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductForm.java similarity index 63% rename from services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductAddForm.java rename to services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductForm.java index bb3d6b1a6f466d2d0993d0a776fcf0bb59b31d19..2fcc4b17a361f2a7f2341e4019d550ea86545489 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductAddForm.java +++ b/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductForm.java @@ -1,5 +1,7 @@ package com.springboot.services.producer.entity.form; +import com.springboot.cloud.common.core.entity.form.BaseForm; +import com.springboot.services.producer.entity.po.Product; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -8,10 +10,10 @@ import javax.validation.constraints.NotBlank; @ApiModel @Data -public class ProductAddForm { +public class ProductForm extends BaseForm { @NotBlank(message = "产品名称不能为空") - @ApiModelProperty(value = "产品名称", required = true) + @ApiModelProperty(value = "产品名称") private String name; @ApiModelProperty(value = "产品描述") diff --git a/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductQueryForm.java b/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductQueryForm.java index d29e25f62f79443c279e12db89ad3cdf6c42f475..2b332c4df026eb5cbccb2777751127a328ce853b 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductQueryForm.java +++ b/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductQueryForm.java @@ -1,5 +1,7 @@ package com.springboot.services.producer.entity.form; +import com.springboot.cloud.common.core.entity.form.BaseQueryForm; +import com.springboot.services.producer.entity.param.ProductQueryParam; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -11,12 +13,18 @@ import java.util.Date; @ApiModel @Data -public class ProductQueryForm { - @ApiModelProperty(value = "产品名称", required = true) +public class ProductQueryForm extends BaseQueryForm { @NotEmpty(message = "名称不能为空") + @ApiModelProperty(value = "产品名称", required = true) private String name; - @Past(message = "查询日期必须小于当前日期") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) - private Date createdDate; + @Past(message = "查询开始时间必须小于当前日期") + @ApiModelProperty(value = "查询开始时间") + private Date createdTimeStart; + + @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) + @Past(message = "查询结束时间必须小于当前日期") + @ApiModelProperty(value = "查询结束时间") + private Date createdTimeEnd; } diff --git a/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductUpdateForm.java b/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductUpdateForm.java deleted file mode 100644 index 7d2a0792fcab528dbb68d6b7141bf30e21316376..0000000000000000000000000000000000000000 --- a/services/producer/src/main/java/com/springboot/services/producer/entity/form/ProductUpdateForm.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.springboot.services.producer.entity.form; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; - -@ApiModel -@Data -public class ProductUpdateForm { - @ApiModelProperty(value = "产品名称") - private String name; -} diff --git a/services/producer/src/main/java/com/springboot/services/producer/entity/param/ProductQueryParam.java b/services/producer/src/main/java/com/springboot/services/producer/entity/param/ProductQueryParam.java index fe02f0faf06e91908369d35c721151a8b383f6ab..88dbe646ba4b26d2c097083caea32a030fffea0a 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/entity/param/ProductQueryParam.java +++ b/services/producer/src/main/java/com/springboot/services/producer/entity/param/ProductQueryParam.java @@ -1,10 +1,17 @@ package com.springboot.services.producer.entity.param; +import com.springboot.cloud.common.core.entity.param.BaseParam; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; @Data @AllArgsConstructor -public class ProductQueryParam { +@NoArgsConstructor +public class ProductQueryParam extends BaseParam { private String name; + private Date createdTimeStart; + private Date createdTimeEnd; } diff --git a/services/producer/src/main/java/com/springboot/services/producer/entity/po/Product.java b/services/producer/src/main/java/com/springboot/services/producer/entity/po/Product.java index 8be289b840b144027cf9e6ba41eb1cc0dbf74620..86f9cf85c0e44c742cbe4afb2245771f3acf42e2 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/entity/po/Product.java +++ b/services/producer/src/main/java/com/springboot/services/producer/entity/po/Product.java @@ -1,19 +1,16 @@ package com.springboot.services.producer.entity.po; import com.springboot.cloud.common.core.entity.po.BasePo; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import javax.validation.constraints.NotBlank; - @Data +@Builder @NoArgsConstructor +@AllArgsConstructor public class Product extends BasePo { private String name; private String description; - - public Product(@NotBlank(message = "产品名称不能为空") String name, String description) { - this.name = name; - this.description = description; - } } diff --git a/services/producer/src/main/java/com/springboot/services/producer/exception/GlobalExceptionHandlerAdvice.java b/services/producer/src/main/java/com/springboot/services/producer/exception/GlobalExceptionHandlerAdvice.java index a792fbcb92402c7572c725bbdf9bc8e6d619dbdd..7df6cca2a833c5e3ee9f08082882f037735f161c 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/exception/GlobalExceptionHandlerAdvice.java +++ b/services/producer/src/main/java/com/springboot/services/producer/exception/GlobalExceptionHandlerAdvice.java @@ -1,24 +1,47 @@ package com.springboot.services.producer.exception; import com.springboot.cloud.common.core.entity.vo.Result; -import com.springboot.cloud.common.core.entity.exception.BaseException; +import com.springboot.cloud.common.core.exception.BaseException; +import com.springboot.cloud.common.core.exception.ErrorType; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.multipart.MultipartException; @RestControllerAdvice public class GlobalExceptionHandlerAdvice { + @ExceptionHandler(value = {MissingServletRequestParameterException.class}) + public Result missingServletRequestParameterException(MissingServletRequestParameterException ex) { + return Result.fail(ErrorType.ARGUMENT_NOT_VALID, ex.getMessage()); + } + + @ExceptionHandler(value = {MultipartException.class}) + public Result uploadFileLimitException(MultipartException ex) { + return Result.fail(ErrorType.UPLOAD_FILE_SIZE_LIMIT, ex.getMessage()); + } + + @ExceptionHandler(value = {MethodArgumentNotValidException.class}) + public Result serviceException(MethodArgumentNotValidException ex) { + return Result.fail(ErrorType.ARGUMENT_NOT_VALID, ex.getMessage()); + } + @ExceptionHandler(value = {BaseException.class}) - public Result serviceException(BaseException ex) { - return Result.fail(ex.getCode(), ex.getMesg(), ex.getMessage()); + public Result baseException(BaseException ex) { + return Result.fail(ex.getErrorType(), ex.getMessage()); } @ExceptionHandler(value = {Exception.class}) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Result exception() { return Result.fail(); } @ExceptionHandler(value = {Throwable.class}) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Result throwable() { return Result.fail(); } diff --git a/services/producer/src/main/java/com/springboot/services/producer/rest/ProductController.java b/services/producer/src/main/java/com/springboot/services/producer/rest/ProductController.java index 492894349c11097165a5bd3667deea981958b5a2..6e51cd787fe13e1f224d780d5807dc125e674421 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/rest/ProductController.java +++ b/services/producer/src/main/java/com/springboot/services/producer/rest/ProductController.java @@ -1,9 +1,8 @@ package com.springboot.services.producer.rest; import com.springboot.cloud.common.core.entity.vo.Result; -import com.springboot.services.producer.entity.form.ProductAddForm; +import com.springboot.services.producer.entity.form.ProductForm; import com.springboot.services.producer.entity.form.ProductQueryForm; -import com.springboot.services.producer.entity.form.ProductUpdateForm; import com.springboot.services.producer.entity.param.ProductQueryParam; import com.springboot.services.producer.entity.po.Product; import com.springboot.services.producer.service.IProductService; @@ -26,13 +25,11 @@ public class ProductController { private IProductService productService; @ApiOperation(value = "新增产品", notes = "新增一个产品") - @ApiImplicitParam(name = "productAddForm", value = "新增产品form表单", required = true, dataType = "ProductAddForm") - @RequestMapping(method = RequestMethod.POST) - public Result add(@Valid @RequestBody ProductAddForm productAddForm) { - log.info("name:", productAddForm); - Product product = new Product(productAddForm.getName(), productAddForm.getDescription()); - product.setCreatedBy("system"); - product.setUpdatedBy("system"); + @ApiImplicitParam(name = "productForm", value = "新增产品form表单", required = true, dataType = "ProductForm") + @RequestMapping(value = "/", method = RequestMethod.POST) + public Result add(@Valid @RequestBody ProductForm productForm) { + log.info("name:", productForm); + Product product = productForm.toPo(Product.class); return Result.success(productService.add(product)); } @@ -47,13 +44,12 @@ public class ProductController { @ApiOperation(value = "修改产品", notes = "修改指定产品信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"), - @ApiImplicitParam(name = "productUpdateForm", value = "产品实体", required = true, dataType = "ProductUpdateForm") + @ApiImplicitParam(name = "productForm", value = "产品实体", required = true, dataType = "ProductForm") }) @RequestMapping(value = "/{id}", method = RequestMethod.PUT) - public Result update(@PathVariable long id, @Valid @RequestBody ProductUpdateForm productUpdateForm) { - Product product = new Product(); + public Result update(@PathVariable long id, @Valid @RequestBody ProductForm productForm) { + Product product = productForm.toPo(Product.class); product.setId(id); - product.setName(productUpdateForm.getName()); productService.update(product); return Result.success(); } @@ -74,16 +70,18 @@ public class ProductController { @RequestMapping(value = "/", method = RequestMethod.GET) public Result query(@RequestParam String name) { log.info("query with name:{}", name); - return Result.success(productService.query(new ProductQueryParam(name))); + ProductQueryParam productQueryParam = new ProductQueryParam(); + productQueryParam.setName(name); + return Result.success(productService.query(productQueryParam)); } @ApiOperation(value = "搜索产品", notes = "根据条件查询产品信息") @ApiImplicitParam(name = "productQueryForm", value = "产品查询参数", required = true, dataType = "ProductQueryForm") @ApiResponse(code = 200, message = "处理成功", response = Result.class) - @RequestMapping(value = "/", method = RequestMethod.POST) + @RequestMapping(value = "/conditions", method = RequestMethod.POST) public Result search(@Valid @RequestBody ProductQueryForm productQueryForm) { log.info("search with productQueryForm:", productQueryForm); - return Result.success(productService.query(new ProductQueryParam(productQueryForm.getName()))); + return Result.success(productService.query(productQueryForm.toParam(ProductQueryParam.class))); } } diff --git a/services/producer/src/main/java/com/springboot/services/producer/service/ProductService.java b/services/producer/src/main/java/com/springboot/services/producer/service/ProductService.java index 2f9ae018301c903c5c3e1e9e8a51b477d61c6331..07119996b1411bd95e3c2be4a9f11d90b5429fa4 100644 --- a/services/producer/src/main/java/com/springboot/services/producer/service/ProductService.java +++ b/services/producer/src/main/java/com/springboot/services/producer/service/ProductService.java @@ -1,8 +1,8 @@ package com.springboot.services.producer.service; import com.springboot.services.producer.dao.ProductMapper; -import com.springboot.services.producer.entity.po.Product; import com.springboot.services.producer.entity.param.ProductQueryParam; +import com.springboot.services.producer.entity.po.Product; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -14,19 +14,14 @@ public class ProductService implements IProductService { @Autowired private ProductMapper productMapper; - @Override - public Product get(long id) { - return productMapper.select(id); - } - @Override public long add(Product product) { return productMapper.insert(product); } @Override - public List query(ProductQueryParam productQueryParam) { - return productMapper.query(productQueryParam); + public void delete(long id) { + productMapper.delete(id); } @Override @@ -35,7 +30,12 @@ public class ProductService implements IProductService { } @Override - public void delete(long id) { - productMapper.delete(id); + public Product get(long id) { + return productMapper.select(id); + } + + @Override + public List query(ProductQueryParam productQueryParam) { + return productMapper.query(productQueryParam); } } diff --git a/services/producer/src/main/resources/application.yml b/services/producer/src/main/resources/application.yml index f1bba2bb6a398cb6d5ed1d1c182adfffd08a01fa..2a001222e96692fcbe45ebf04fc15b0e271693d9 100644 --- a/services/producer/src/main/resources/application.yml +++ b/services/producer/src/main/resources/application.yml @@ -28,12 +28,14 @@ spring: baseUrl: http://localhost:8091 mvc: throw-exception-if-no-handler-found: true - resources: - add-mappings: false + servlet: + multipart: + max-request-size: 2 + max-file-size: 2 logging: level: - com.springboot.producer.dao: debug + com.springboot.services.producer.dao: debug org.springframework.web: debug org.apache.ibatis: debug java.sql.PreparedStatement: debug