update > controller template & exception handler for issue #13

master
lihengming 7 years ago
parent 0367d4a147
commit 1e8e1bdd6a
  1. 32
      src/main/java/com/company/project/configurer/WebMvcConfigurer.java
  2. 3
      src/test/resources/generator/template/controller-restful.ftl
  3. 7
      src/test/resources/generator/template/controller.ftl

@ -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.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
@ -62,28 +63,27 @@ public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
exceptionResolvers.add(new HandlerExceptionResolver() { exceptionResolvers.add(new HandlerExceptionResolver() {
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
Result result = new Result(); Result result = new Result();
if (handler instanceof HandlerMethod) { if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误”
HandlerMethod handlerMethod = (HandlerMethod) handler; result.setCode(ResultCode.FAIL).setMessage(e.getMessage());
logger.info(e.getMessage());
if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误” } else if (e instanceof NoHandlerFoundException) {
result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在");
logger.info(e.getMessage()); } else if (e instanceof ServletException) {
} else { result.setCode(ResultCode.FAIL).setMessage(e.getMessage());
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员"); } else {
String message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", String message;
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s",
request.getRequestURI(), request.getRequestURI(),
handlerMethod.getBean().getClass().getName(), handlerMethod.getBean().getClass().getName(),
handlerMethod.getMethod().getName(), handlerMethod.getMethod().getName(),
e.getMessage()); e.getMessage());
logger.error(message, e);
}
} else {
if (e instanceof NoHandlerFoundException) {
result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在");
} else { } else {
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(e.getMessage()); message = e.getMessage();
logger.error(e.getMessage(), e);
} }
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(message);
logger.error(e.getMessage(), e);
} }
responseResult(response, result); responseResult(response, result);
return new ModelAndView(); return new ModelAndView();

@ -37,6 +37,7 @@ public class ${modelNameUpperCamel}Controller {
${modelNameLowerCamel}Service.update(${modelNameLowerCamel}); ${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
return ResultGenerator.genSuccessResult(); return ResultGenerator.genSuccessResult();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public Result detail(@PathVariable Integer id) { public Result detail(@PathVariable Integer id) {
${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id); ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id);
@ -44,7 +45,7 @@ public class ${modelNameUpperCamel}Controller {
} }
@GetMapping @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); PageHelper.startPage(page, size);
List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll(); List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll();
PageInfo pageInfo = new PageInfo(list); PageInfo pageInfo = new PageInfo(list);

@ -7,6 +7,7 @@ import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -28,7 +29,7 @@ public class ${modelNameUpperCamel}Controller {
} }
@PostMapping("/delete") @PostMapping("/delete")
public Result delete(Integer id) { public Result delete(@RequestParam Integer id) {
${modelNameLowerCamel}Service.deleteById(id); ${modelNameLowerCamel}Service.deleteById(id);
return ResultGenerator.genSuccessResult(); return ResultGenerator.genSuccessResult();
} }
@ -40,13 +41,13 @@ public class ${modelNameUpperCamel}Controller {
} }
@PostMapping("/detail") @PostMapping("/detail")
public Result detail(Integer id) { public Result detail(@RequestParam Integer id) {
${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id); ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id);
return ResultGenerator.genSuccessResult(${modelNameLowerCamel}); return ResultGenerator.genSuccessResult(${modelNameLowerCamel});
} }
@PostMapping("/list") @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); PageHelper.startPage(page, size);
List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll(); List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll();
PageInfo pageInfo = new PageInfo(list); PageInfo pageInfo = new PageInfo(list);

Loading…
Cancel
Save