異常沒被捕獲呀。。。
我按課程代碼寫的異常沒被捕獲,什么情況? package handler; import com.imooc.domain.Result; import com.imooc.utils.ResultUtil; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by MBENBEN on 2017/6/18. */ @ControllerAdvice public class ExceptionHandle { ? ?@ExceptionHandler(value = Exception.class) ? ?@ResponseBody ? ?public Result handle(Exception e){ ? ? ? ? ?return ResultUtil.error(100,e.getMessage()); ? ?} } service里 public void getAge(Integer id) throws Exception{ ? ? ? ? Girl girl= girlRepository.findOne(id); ? ? ? ?Integer age=girl.getAge(); ? ? ? ?if(age<10){ throw="" new="" else="" age="">10 && age<16){ ? ? ? ? ? ?//你可能還在上初中 ? ? ? ? ? ?throw new Exception("你可能還在上初中"); ? ? ? ?} ? ? ? ?//如果大于16歲 ? ?} controller里 @GetMapping(value = "girls/getAge/{id}") ? ?public void getAge(@PathVariable("id") Integer id) throws Exception{ ? ? ? ?girlService.getAge(id); ? ?}
2018-01-30
感謝感謝哈哈哈哈
2017-07-21
謝謝老鐵
2017-06-26
感謝了,老鐵
2017-06-18
后來發(fā)現(xiàn)問題了,代碼都是正確的,注意handle包必須和controller包和service包在同一級包下才行!!