工程唯一校验、valid注解参数校验、异常拦截器
This commit is contained in:
parent
49cbcb3036
commit
685fd8e14b
|
|
@ -10,6 +10,10 @@ import java.io.Serializable;
|
|||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 工程管理信息表
|
||||
* @author 阮世耀
|
||||
|
|
@ -28,6 +32,7 @@ public class BmProject extends BaseEntity implements Serializable {
|
|||
*/
|
||||
@ApiModelProperty(value = "工程名称")
|
||||
@Excel(name = "工程项目名称")
|
||||
@NotBlank(message = "工程名称不能为空")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
|
|
@ -35,14 +40,15 @@ public class BmProject extends BaseEntity implements Serializable {
|
|||
*/
|
||||
@ApiModelProperty(value = "实施单位")
|
||||
@Excel(name = "实施单位")
|
||||
@NotBlank(message = "实施单位不能为空")
|
||||
private String impUnit;
|
||||
|
||||
|
||||
/**
|
||||
* 项目类型。 1:线路工程;2:变电工程;3:业务工程;4:其他工程
|
||||
*/
|
||||
@ApiModelProperty(value = "项目类型。 1:线路工程;2:变电工程;3:业务工程;4:其他工程")
|
||||
@Excel(name = "工程类型")
|
||||
@Min(value = 1, message = "项目类型不能为空")
|
||||
private Integer projectType;
|
||||
|
||||
/**
|
||||
|
|
@ -102,6 +108,7 @@ public class BmProject extends BaseEntity implements Serializable {
|
|||
* 工程状态
|
||||
*/
|
||||
@ApiModelProperty(value = "工程状态")
|
||||
@NotBlank(message = "工程状态不能为空")
|
||||
@Excel(name = "工程状态")
|
||||
private String stats;
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,9 @@ import lombok.RequiredArgsConstructor;
|
|||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public final class ResultBean<T> {
|
||||
public final class ResultBean<T> implements java.io.Serializable{
|
||||
|
||||
private final static long serialVersionUID = 6674412622288634845L;
|
||||
|
||||
private final int code;
|
||||
|
||||
|
|
|
|||
|
|
@ -36,8 +36,7 @@ public class GlobalExceptionHandler
|
|||
* 权限码异常
|
||||
*/
|
||||
@ExceptionHandler(NotPermissionException.class)
|
||||
public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request)
|
||||
{
|
||||
public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
|
||||
String requestUri = request.getRequestURI();
|
||||
log.error("请求地址'{}',权限码校验失败'{}'", requestUri, e.getMessage());
|
||||
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
|
||||
|
|
@ -47,8 +46,7 @@ public class GlobalExceptionHandler
|
|||
* 角色权限异常
|
||||
*/
|
||||
@ExceptionHandler(NotRoleException.class)
|
||||
public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request)
|
||||
{
|
||||
public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request) {
|
||||
String requestUri = request.getRequestURI();
|
||||
log.error("请求地址'{}',角色权限校验失败'{}'", requestUri, e.getMessage());
|
||||
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
|
||||
|
|
@ -69,8 +67,7 @@ public class GlobalExceptionHandler
|
|||
* 业务异常
|
||||
*/
|
||||
@ExceptionHandler(ServiceException.class)
|
||||
public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
|
||||
{
|
||||
public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {
|
||||
log.error(e.getMessage(), e);
|
||||
Integer code = e.getCode();
|
||||
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
|
||||
|
|
@ -102,8 +99,7 @@ public class GlobalExceptionHandler
|
|||
* 拦截未知的运行时异常
|
||||
*/
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
|
||||
{
|
||||
public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
||||
String msg=e.getMessage();
|
||||
if (StringUtils.hasText(msg)) {
|
||||
if (msg.contains(BODY_ERROR)){
|
||||
|
|
@ -122,8 +118,7 @@ public class GlobalExceptionHandler
|
|||
* 系统异常
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
public AjaxResult handleException(Exception e, HttpServletRequest request)
|
||||
{
|
||||
public AjaxResult handleException(Exception e, HttpServletRequest request) {
|
||||
String requestUri = request.getRequestURI();
|
||||
log.error("请求地址'{}',发生系统异常.", requestUri, e);
|
||||
return AjaxResult.error(e.getMessage());
|
||||
|
|
@ -133,8 +128,7 @@ public class GlobalExceptionHandler
|
|||
* 自定义验证异常
|
||||
*/
|
||||
@ExceptionHandler(BindException.class)
|
||||
public AjaxResult handleBindException(BindException e)
|
||||
{
|
||||
public AjaxResult handleBindException(BindException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String message = e.getAllErrors().get(0).getDefaultMessage();
|
||||
return AjaxResult.error(message);
|
||||
|
|
@ -144,13 +138,21 @@ public class GlobalExceptionHandler
|
|||
* 自定义验证异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
|
||||
{
|
||||
public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
||||
return AjaxResult.error(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理系统异常,在其他异常拦截均未捕获时进行兜底处理
|
||||
*/
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public AjaxResult defaultHandleException(Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部认证异常
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.bonus.base.controller;
|
|||
import com.bonus.base.api.domain.BmProject;
|
||||
import com.bonus.base.service.BmProjectService;
|
||||
import com.bonus.common.core.domain.ResultBean;
|
||||
import com.bonus.common.core.utils.StringHelper;
|
||||
import com.bonus.common.core.utils.poi.ExcelUtil;
|
||||
import com.bonus.common.core.web.controller.BaseController;
|
||||
import com.bonus.common.core.web.domain.AjaxResult;
|
||||
|
|
@ -11,9 +12,14 @@ import com.bonus.common.security.annotation.RequiresPermissions;
|
|||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.constraints.Positive;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -64,7 +70,9 @@ public class BmProjectController extends BaseController {
|
|||
*/
|
||||
@GetMapping("{id}")
|
||||
@RequiresPermissions("base:project:query")
|
||||
public ResultBean<BmProject> queryById(@PathVariable("id") Integer id) {
|
||||
public ResultBean<BmProject> queryById(@PathVariable("id")
|
||||
@NotNull(message = "ID不能为空")
|
||||
@Positive(message = "ID必须为正整数") Integer id) {
|
||||
return ResultBean.success(this.bmProjectService.selectByPrimaryKey(id));
|
||||
}
|
||||
|
||||
|
|
@ -76,7 +84,9 @@ public class BmProjectController extends BaseController {
|
|||
*/
|
||||
@PostMapping(value = "/add")
|
||||
@RequiresPermissions("base:project:add")
|
||||
public ResultBean<Boolean> add(@RequestBody BmProject obj) {
|
||||
public ResultBean<Boolean> add(@NotNull @Valid @RequestBody BmProject obj) {
|
||||
ResultBean<Boolean> error = validRecord(obj);
|
||||
if (error != null) return error;
|
||||
int result = this.bmProjectService.insertSelective(obj);
|
||||
return result > 0 ? ResultBean.success(true) : ResultBean.error(0, "增加失败");
|
||||
}
|
||||
|
|
@ -89,11 +99,25 @@ public class BmProjectController extends BaseController {
|
|||
*/
|
||||
@PutMapping(value = "/update")
|
||||
@RequiresPermissions("base:project:edit")
|
||||
public ResultBean<Boolean> edit(@RequestBody BmProject obj) {
|
||||
public ResultBean<Boolean> edit(@NotNull @RequestBody BmProject obj) {
|
||||
ResultBean<Boolean> validResult = validRecord(obj);
|
||||
if (validResult != null) return validResult;
|
||||
this.bmProjectService.updateByPrimaryKeySelective(obj);
|
||||
return ResultBean.success(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数校验,检验新增、修改的参数是否符合要求
|
||||
* @param obj 请求对象
|
||||
* @return 校验结果
|
||||
*/
|
||||
private ResultBean<Boolean> validRecord(BmProject obj) {
|
||||
if (this.bmProjectService.selectByName(obj.getName()) != 0) {
|
||||
return ResultBean.error("工程名称重复");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 逻辑删除
|
||||
*
|
||||
|
|
@ -102,7 +126,9 @@ public class BmProjectController extends BaseController {
|
|||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
@RequiresPermissions("base:project:remove")
|
||||
public ResultBean<String> deleteById(@PathVariable("id") Integer id) {
|
||||
public ResultBean<String> deleteById(@PathVariable("id")
|
||||
@NotNull(message = "ID不能为空")
|
||||
@Positive(message = "ID必须为正整数") Integer id) {
|
||||
return ResultBean.toIsSuccess(this.bmProjectService.updateIsActive(id),"删除失败");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
package com.bonus.base.mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import com.bonus.base.api.domain.BmProject;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
|
@ -46,6 +47,10 @@ public interface BmProjectMapper {
|
|||
*/
|
||||
BmProject selectByPrimaryKey(Integer id);
|
||||
|
||||
int selectByName(@Param("name")String name);
|
||||
|
||||
|
||||
|
||||
List<BmProject> selectAll(BmProject bmProject);
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -62,4 +62,9 @@ public class BmProjectService{
|
|||
return bmProjectMapper.updateByPrimaryKey(record);
|
||||
}
|
||||
|
||||
|
||||
public int selectByName(String name){
|
||||
return bmProjectMapper.selectByName(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -690,4 +690,10 @@
|
|||
update bm_project set is_active = '0' where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
|
||||
<!--by syruan on 2024-08-14-->
|
||||
<select id="selectByName" resultType="java.lang.Integer">
|
||||
select count(*)
|
||||
from bm_project
|
||||
where `name`= #{name,jdbcType=VARCHAR}
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
package com.bonus.material.mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import com.bonus.material.domain.MaHouseSet;
|
||||
import java.util.List;
|
||||
|
|
@ -60,4 +61,6 @@ public interface MaHouseSetMapper {
|
|||
int updateByPrimaryKey(MaHouseSet record);
|
||||
|
||||
List<MaHouseSet> selectAll(MaHouseSet record);
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -144,4 +144,5 @@
|
|||
</trim>
|
||||
</insert>
|
||||
|
||||
|
||||
</mapper>
|
||||
Loading…
Reference in New Issue