創(chuàng)建商品失敗,未知錯誤
debug之后發(fā)現(xiàn)代碼走到itemController層以后直接被父類的baseController的分支return出去了 分支2 這個怎么辦? 沒法進入service層就直接return了status一直是fail,前臺未知錯誤,求解
package?com.miaoshaproject.controller; import?com.miaoshaproject.error.BusinessException; import?com.miaoshaproject.error.EmBusinessError; import?com.miaoshaproject.response.CommonReturnType; import?org.springframework.http.HttpStatus; import?org.springframework.web.bind.annotation.ExceptionHandler; import?org.springframework.web.bind.annotation.ResponseBody; import?org.springframework.web.bind.annotation.ResponseStatus; import?javax.servlet.http.HttpServletRequest; import?java.util.HashMap; import?java.util.Map; public?class?BaseController?{ ????public?static?final?String?CONTENT_TYPE_FORMED="application/x-www-form-urlencoded"; ????//定義exceptionHandler解決未被controller層吸收的exception ????@ExceptionHandler(Exception.class) ????@ResponseStatus(HttpStatus.OK) ????@ResponseBody ????public?Object?handlerException(HttpServletRequest?req,?Exception?ex){ ????????CommonReturnType?commonReturnType?=?new?CommonReturnType(); ????????Map<String,?Object>?responseData?=?new?HashMap<>(); ????????if(ex?instanceof?BusinessException){ ????????????BusinessException?businessException?=?(BusinessException)ex; ????????????responseData.put("errCode",businessException.getErrCode()); ????????????responseData.put("errMsg",businessException.getErrMsg()); ????????????System.out.println("打樁分支1"); ????????}else?{ ????????????responseData.put("errCode",?EmBusinessError.UNKNOWN_EXCEPTION.getErrCode()); ????????????responseData.put("errMsg",EmBusinessError.UNKNOWN_EXCEPTION.getErrMsg()); ????????????System.out.println("打樁分支2"); ????????} ????????return?CommonReturnType.create(responseData,"fail"); ????} }
package?com.miaoshaproject.controller; import?com.miaoshaproject.controller.viewobject.ItemVO; import?com.miaoshaproject.error.BusinessException; import?com.miaoshaproject.response.CommonReturnType; import?com.miaoshaproject.service.ItemService; import?com.miaoshaproject.service.model.ItemModel; import?org.springframework.beans.BeanUtils; import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.stereotype.Controller; import?org.springframework.web.bind.annotation.*; import?java.math.BigDecimal; @Controller("item") @RequestMapping("/item") @CrossOrigin(origins?=?{"*"},?allowCredentials?=?"true") public?class?ItemController?extends??BaseController?{ ????@Autowired ????private?ItemService?itemService; ????//創(chuàng)建商品的Controller ????@RequestMapping(value?=?"/create",method?=?RequestMethod.POST,consumes?=?{CONTENT_TYPE_FORMED}) ????@ResponseBody ????public?CommonReturnType?createItem(@RequestParam(name?=?"title")String?title, ???????????????????????????????????????@RequestParam(name?=?"description")?String?description, ???????????????????????????????????????@RequestParam(name?=?"price")BigDecimal?price, ???????????????????????????????????????@RequestParam(name?=?"stock")Integer?stock, ???????????????????????????????????????@RequestParam(name?=?"imgUrl")String?imgUrl)?throws?BusinessException?{ ????????//封裝Service請求用來創(chuàng)建商品 ????????ItemModel?itemModel?=?new?ItemModel(); ????????itemModel.setTitle(title); ????????itemModel.setDescroption(description); ????????itemModel.setPrice(price); ????????itemModel.setStock(stock); ????????itemModel.setImgUrl(imgUrl); ????????ItemModel?mode?=?itemService.createItem(itemModel); ????????ItemVO?itemVO?=?convertVOFromModel(mode); ????????return?CommonReturnType.create(itemVO); ????} ????private?ItemVO?convertVOFromModel(ItemModel?itemModel){ ????????if(itemModel==null){ ????????????return??null; ????????} ????????ItemVO?itemVO?=?new?ItemVO(); ????????BeanUtils.copyProperties(itemModel,itemVO); ????????return?itemVO; ????} }
2020-09-11
感謝老哥,之前一直找不到錯誤,用你說的debug調試果然一下子就看到錯誤解決了
2019-12-15
找到bug了 在itemModel中Integer變量stock上我用了@NotBlank,應該用@NotNull才對 所以入?yún)⑿r炛苯邮?debug斷點打在BaseController中的Ex上變量有詳細的描述才定位到Bug,搞了幾個小時才搞定 銘記一生
2019-12-15
debug斷點從itemcontroller的
debug斷點從ItemController中的
ItemModel?mode?=?itemService.createItem(itemModel);
方法后就直接跳轉到了
這個spring類庫中的方法中去了
搞不懂!求解釋
2019-12-15
前臺代碼
2019-12-15
2019-12-15 01:08:10.425? INFO 11728 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]? ? ? ?: Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-12-15 01:08:10.426? INFO 11728 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet? ? ? ? : Initializing Servlet 'dispatcherServlet'
2019-12-15 01:08:10.432? INFO 11728 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet? ? ? ? : Completed initialization in 6 ms
2019-12-15 01:08:10.576? INFO 11728 --- [nio-8080-exec-1] com.alibaba.druid.pool.DruidDataSource? ?: {dataSource-1} inited
打樁分支2
這是前臺請求發(fā)送后,IDEA后臺的堆棧信息?