背景:我们在实现接口逻辑的时候,严谨的做法是给每个请求对象的参数增加相关校验,比如:
比较直接的实现就是方法逻辑加上:
Assert.notNull(, "id不能为空");
Assert.hasText(phone, "手机号不能为空");
这样把参数校验和业务逻辑混在一起,不是很美观,那这部分代码可用独立出去吗?实际上spring
validation 已经帮我们实现了,那么我们要做的就4个步骤:
@RestController
public class MyErrorController extends BasicErrorController {
public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
super(errorAttributes, errorProperties);
}
public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers) {
super(errorAttributes, errorProperties, errorViewResolvers);
}
private static final String METHOD_ARGUMENT_NOT_VALID_EXCEPTION = "org.springframework.web.bind.MethodArgumentNotValidException";
private static final String HTTP_MESSAGE_NOT_READABLE_EXCEPTION = "org.springframework.http.converter.HttpMessageNotReadableException";
@RequestMapping
public ResponseEntity
/** 呼叫中心用户 */
@lombok.Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ChatUserCreateReq extends org.xt.maven.plugin.dto.EntityReq {
/**
* 邮箱
*/
@NotNull(message = "邮箱不能为空")
@Email
private java.lang.String email;
/**
* 手机号
*/
@NotNull
@Pattern(regexp = "")
private java.lang.String phoneNo;
/**
* 用户名
*/
@NotNull
@Length(max = 32)
private java.lang.String username;
/**
* 昵称
*/
private java.lang.String nickName;
/**
* 性别
*/
private java.lang.Integer gender;
/**
* 头像
*/
private java.lang.String avatar;
/**
* 状态
*/
private java.lang.Integer status;
/**
* 最近一次登录ip
*/
private java.lang.String lastLoginIp;
/**
* 最近一次登录时间
*/
private java.time.LocalDateTime lastLoginTime;
/**
* Default constructor.
*/
public ChatUserCreateReq() {
}
}
@EnableAutoConfiguration
@RestController
@RequestMapping("/api/chat/user")
@Api(description = "呼叫中心用户")
public class ChatUserEndpoint{
@javax.annotation.Resource
private ChatUserApiService chatUserApiService;
@ApiOperation("新增")
@RequestMapping(value = "/create",method = RequestMethod.POST)
public Response create(@RequestBody @Validated ChatUserCreateReq req, final HttpServletRequest request) throws SimpleException {
return chatUserApiService.create(req);
}
@ApiOperation(value = "查询")
@RequestMapping(value = "/get", method = RequestMethod.POST)
public Response get(@RequestBody @Validated ChatUserGetReq req, final HttpServletRequest request) throws SimpleException {
return chatUserApiService.get(req);
}
@ApiOperation(value = "更新")
@RequestMapping(value = "/update",method = RequestMethod.POST)
public Response update(@RequestBody @Validated ChatUserUpdateReq req, final HttpServletRequest request) throws SimpleException {
return chatUserApiService.update(req);
}
@ApiOperation(value = "删除")
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public Response delete(@RequestBody @Validated ChatUserDeleteReq req, final HttpServletRequest request) throws SimpleException {
return chatUserApiService.delete(req);
}
@ApiOperation(value = "分页查询")
@RequestMapping(value = "/find", method = RequestMethod.POST)
public Response> find(@RequestBody ChatUserFindReq req, final HttpServletRequest request) throws SimpleException {
return chatUserApiService.find(req);
}
}
@org.springframework.cloud.openfeign.FeignClient(name = "${feign.chat.name}", url = "${feign.chat.url:}", contextId = "feignChatUserApiService", primary = false)
public interface ChatUserApiService {
/**
* 分页查询
*
* @param req
* @return
* @throws SimpleException
*/
@org.springframework.web.bind.annotation.PostMapping("/api/chat/user/find")
Response> find(@RequestBody ChatUserFindReq req);
/**
* 获取对象
*
* @param req
* @return
* @throws SimpleException
*/
@org.springframework.web.bind.annotation.PostMapping("/api/chat/user/get")
Response get(@RequestBody @Validated ChatUserGetReq req);
/**
* 创建对象
*
* @param req
* @return
* @throws SimpleException
*/
@org.springframework.web.bind.annotation.PostMapping("/api/chat/user/create")
Response create(@RequestBody @Validated ChatUserCreateReq req);
/**
* 删除对象
*
* @param req
* @return
* @throws SimpleException
*/
@org.springframework.web.bind.annotation.PostMapping("/api/chat/user/delete")
Response delete(@RequestBody @Validated ChatUserDeleteReq req);
/**
* 更新对象
*
* @param req
* @return
* @throws SimpleException
*/
@org.springframework.web.bind.annotation.PostMapping("/api/chat/user/update")
Response update(@RequestBody @Validated ChatUserUpdateReq req);
}