From 1e8e1bdd6a8d0b9f321064f13588334a6403e914 Mon Sep 17 00:00:00 2001 From: lihengming <89921218@qq.com> Date: Tue, 4 Jul 2017 11:07:39 +0800 Subject: [PATCH] update > controller template & exception handler for issue #13 --- .../project/configurer/WebMvcConfigurer.java | 32 +++++++++---------- .../generator/template/controller-restful.ftl | 3 +- .../generator/template/controller.ftl | 7 ++-- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/company/project/configurer/WebMvcConfigurer.java b/src/main/java/com/company/project/configurer/WebMvcConfigurer.java index ab3a85f..3125567 100644 --- a/src/main/java/com/company/project/configurer/WebMvcConfigurer.java +++ b/src/main/java/com/company/project/configurer/WebMvcConfigurer.java @@ -24,6 +24,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -62,28 +63,27 @@ public class WebMvcConfigurer extends WebMvcConfigurerAdapter { exceptionResolvers.add(new HandlerExceptionResolver() { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { Result result = new Result(); - if (handler instanceof HandlerMethod) { - HandlerMethod handlerMethod = (HandlerMethod) handler; - - if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误” - result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); - logger.info(e.getMessage()); - } else { - result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员"); - String message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", + if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误” + result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); + logger.info(e.getMessage()); + } else if (e instanceof NoHandlerFoundException) { + result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在"); + } else if (e instanceof ServletException) { + result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); + } else { + String message; + if (handler instanceof HandlerMethod) { + HandlerMethod handlerMethod = (HandlerMethod) handler; + message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod().getName(), e.getMessage()); - logger.error(message, e); - } - } else { - if (e instanceof NoHandlerFoundException) { - result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在"); } else { - result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(e.getMessage()); - logger.error(e.getMessage(), e); + message = e.getMessage(); } + result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(message); + logger.error(e.getMessage(), e); } responseResult(response, result); return new ModelAndView(); diff --git a/src/test/resources/generator/template/controller-restful.ftl b/src/test/resources/generator/template/controller-restful.ftl index ad423d2..bf72458 100644 --- a/src/test/resources/generator/template/controller-restful.ftl +++ b/src/test/resources/generator/template/controller-restful.ftl @@ -37,6 +37,7 @@ public class ${modelNameUpperCamel}Controller { ${modelNameLowerCamel}Service.update(${modelNameLowerCamel}); return ResultGenerator.genSuccessResult(); } + @GetMapping("/{id}") public Result detail(@PathVariable Integer id) { ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id); @@ -44,7 +45,7 @@ public class ${modelNameUpperCamel}Controller { } @GetMapping - public Result list(Integer page, Integer size) { + public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) { PageHelper.startPage(page, size); List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll(); PageInfo pageInfo = new PageInfo(list); diff --git a/src/test/resources/generator/template/controller.ftl b/src/test/resources/generator/template/controller.ftl index 45e6583..720cbc0 100644 --- a/src/test/resources/generator/template/controller.ftl +++ b/src/test/resources/generator/template/controller.ftl @@ -7,6 +7,7 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @@ -28,7 +29,7 @@ public class ${modelNameUpperCamel}Controller { } @PostMapping("/delete") - public Result delete(Integer id) { + public Result delete(@RequestParam Integer id) { ${modelNameLowerCamel}Service.deleteById(id); return ResultGenerator.genSuccessResult(); } @@ -40,13 +41,13 @@ public class ${modelNameUpperCamel}Controller { } @PostMapping("/detail") - public Result detail(Integer id) { + public Result detail(@RequestParam Integer id) { ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id); return ResultGenerator.genSuccessResult(${modelNameLowerCamel}); } @PostMapping("/list") - public Result list(Integer page, Integer size) { + public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) { PageHelper.startPage(page, size); List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll(); PageInfo pageInfo = new PageInfo(list);