1.实体类:
package com.thrall.business.vo;import com.thrall.business.vaild.VaildQuoteGroupVOAdd;import com.thrall.business.vaild.VaildQuoteGroupVOEdit;import org.hibernate.validator.constraints.Length;import javax.validation.constraints.NotNull;import javax.validation.constraints.Pattern;import java.io.Serializable;import java.util.Date;public class QuoteGroupVO implements Serializable { private static final long serialVersionUID = 1L; @NotNull(groups = {VaildQuoteGroupVOEdit.class}) private Long id; @Length(min = 3, max = 20, message = "报价组名长度必须在[3-20]之间!", groups = {VaildQuoteGroupVOAdd.class, VaildQuoteGroupVOEdit.class}) @Pattern(regexp = "^[\\u4e00-\\u9fa5a-zA-Z0-9]+$", message = "报价组名不能包含特殊字符!", groups = {VaildQuoteGroupVOAdd.class,VaildQuoteGroupVOEdit.class}) private String name; private Long siteId; @NotNull(message = "报价组启用时间不能为空!") private Date beginDate; @NotNull(message = "报价组结束时间不能为空!") private Date endDate; private Integer shipType; private Date gmtCreate; private String creator; private Date gmtModified; private String editor; private Integer type; private Integer status; private String customers; private Long customerId; private String customerName; private String customerIds; private String beginTime; private String endTime; public String getBeginTime() { return beginTime; } public void setBeginTime(String beginTime) { this.beginTime = beginTime; } public String getEndTime() { return endTime; } public void setEndTime(String endTime) { this.endTime = endTime; } public Long getCustomerId() { return customerId; } public void setCustomerId(Long customerId) { this.customerId = customerId; } public String getCustomers() { return customers; } public void setCustomers(String customers) { this.customers = customers; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Long getSiteId() { return siteId; } public void setSiteId(Long siteId) { this.siteId = siteId; } public Date getBeginDate() { return beginDate; } public void setBeginDate(Date beginDate) { this.beginDate = beginDate; } public Date getEndDate() { return endDate; } public void setEndDate(Date endDate) { this.endDate = endDate; } public Integer getShipType() { return shipType; } public void setShipType(Integer shipType) { this.shipType = shipType; } public Date getGmtCreate() { return gmtCreate; } public void setGmtCreate(Date gmtCreate) { this.gmtCreate = gmtCreate; } public String getCreator() { return creator; } public void setCreator(String creator) { this.creator = creator; } public Date getGmtModified() { return gmtModified; } public void setGmtModified(Date gmtModified) { this.gmtModified = gmtModified; } public String getEditor() { return editor; } public void setEditor(String editor) { this.editor = editor == null ? null : editor.trim(); } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getCustomerIds() { return customerIds; } public void setCustomerIds(String customerIds) { this.customerIds = customerIds; }}
2. Controller类:
package com.thrall.web.controller;import com.commons.Page;import com.thrall.business.service.CustomerQuoteConfigService;import com.thrall.business.service.QuoteGroupService;import com.thrall.business.vaild.ThrallValid;import com.thrall.business.vaild.VaildQuoteGroupVOAdd;import com.thrall.business.vaild.VaildQuoteGroupVOEdit;import com.thrall.business.vo.LoginUserVO;import com.thrall.business.vo.QuoteGroupVO;import com.thrall.common.utils.Constants;import com.thrall.common.utils.date.DateUtils;import com.thrall.common.utils.page.PageParam;import com.thrall.dal.domain.CustomerQuoteConfig;import com.thrall.dal.domain.QuoteGroup;import com.thrall.web.base.BaseSpringController;import com.thrall.web.base.JsonResult;import com.thrall.web.base.LoginHelper;import org.apache.commons.collections.CollectionUtils;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.*;import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.concurrent.CompletableFuture;import java.util.concurrent.ExecutionException;import java.util.stream.Collectors;/** * Created by dongrj on 2017/1/9 12:46:02. * Email: dongrongjun163.com */@RestController@RequestMapping("/quotegroup")@ThrallValidpublic class QuoteGroupController extends BaseSpringController { @Autowired QuoteGroupService quoteGroupService; @Autowired CustomerQuoteConfigService customerQuoteConfigService; /** * 获取报价组 */ @RequestMapping(value = "/page", method = RequestMethod.POST) public JsonResult> selectByPaging(@RequestBody PageParam pageParam) { LoginUserVO loginUserVO = new LoginUserVO(LoginHelper.getLoginSiteId(), LoginHelper.getLoginName()); Page page = quoteGroupService.selectPaging(pageParam, loginUserVO); return success(page); } /** * 添加报价组 */ @RequestMapping(value = "/add", method = RequestMethod.POST) public JsonResult addQuoteGroup(@RequestBody @Validated({VaildQuoteGroupVOAdd.class}) QuoteGroupVO quoteGroupVO) throws ExecutionException, InterruptedException { quoteGroupVO = changeDate(quoteGroupVO); String message = validate(quoteGroupVO); if (StringUtils.isNotEmpty(message)) return fail(500, message); LoginUserVO loginUserVO = new LoginUserVO(LoginHelper.getLoginSiteId(), LoginHelper.getLoginName()); quoteGroupService.addQuoteGroupVO(quoteGroupVO, loginUserVO); return success(); }}
3. ExceptionAdvice
package com.thrall.web.base;import com.thrall.business.vaild.ThrallValid;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.http.HttpStatus;import org.springframework.validation.BindException;import org.springframework.validation.BindingResult;import org.springframework.validation.FieldError;import org.springframework.web.bind.MethodArgumentNotValidException;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.ResponseStatus;/** * Created by dongrj on 2017/1/9 12:46:02. * Email: dongrongjun@cn * 用于处理所有ThrallValid标记的异常情况 */@ControllerAdvice(annotations = ThrallValid.class)@ResponseBodypublic class ExceptionAdvice { private static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class); @ExceptionHandler(BindException.class) public JsonResult handleBindException(BindException e) { logger.error("参数绑定失败", e); BindingResult bindingResult = e.getBindingResult(); FieldError fieldError = bindingResult.getFieldError(); String message = fieldError.getDefaultMessage(); JsonResult result=new JsonResult(); result.setCode(500); result.setMessage(message); return result; } @ExceptionHandler(MethodArgumentNotValidException.class) public JsonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { logger.error("参数验证失败", e); BindingResult bindingResult = e.getBindingResult(); FieldError fieldError = bindingResult.getFieldError(); String message = fieldError.getDefaultMessage(); JsonResult result=new JsonResult (); result.setCode(500); result.setMessage(message); return result; } @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) public void handleException(Exception e) { logger.error("通用异常!", e); }}
4.分组类
package com.thrall.business.vaild;/** * Created by dongrj on 2017/1/17 16:08:43. * Email: dongrongjun@cn */public interface VaildQuoteGroupVOEdit {}