創(chuàng)建商品失敗,未知錯(cuò)誤
debug之后發(fā)現(xiàn)代碼走到itemController層以后直接被父類的baseController的分支return出去了 分支2 這個(gè)怎么辦? 沒(méi)法進(jìn)入service層就直接return了status一直是fail,前臺(tái)未知錯(cuò)誤,求解
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請(qǐng)求用來(lái)創(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
感謝老哥,之前一直找不到錯(cuò)誤,用你說(shuō)的debug調(diào)試果然一下子就看到錯(cuò)誤解決了
2019-12-15
找到bug了 在itemModel中Integer變量stock上我用了@NotBlank,應(yīng)該用@NotNull才對(duì) 所以入?yún)⑿r?yàn)直接失敗,debug斷點(diǎn)打在BaseController中的Ex上變量有詳細(xì)的描述才定位到Bug,搞了幾個(gè)小時(shí)才搞定 銘記一生
2019-12-15
debug斷點(diǎn)從itemcontroller的
debug斷點(diǎn)從ItemController中的
ItemModel?mode?=?itemService.createItem(itemModel);
方法后就直接跳轉(zhuǎn)到了
這個(gè)spring類庫(kù)中的方法中去了
搞不懂!求解釋
2019-12-15
前臺(tái)代碼
<!DOCTYPE?html><html><head>????<meta?charset="utf-8"><script?src="node_modules\jquery\dist\jquery.js"?type="text/javascript"></script><link?href="node_modules\bootstrap\dist\css\bootstrap.css"style="stylesheet"?type="text/css"><link?href="static/assets/global/css/components.css"rel="stylesheet"?type="text/css"><link?href="static/assets/admin/pages/css/login.css"rel="stylesheet"?type="text/css"></head><body?class="login">????<div?class="content">?????????<h3?class="form-title">創(chuàng)建商品</h3>????????<div?class="form-group">????????????<label?class="control-label">商品名</label>????????????<div>????????????????<input?class="form-control"?type="text"?placeholder="商品名"?name="title"?id="title">????????????</div>????????</div>????????<div?class="form-group">????????????<label?class="control-label">商品描述</label>????????????<div>????????????????<input?class="form-control"?type="text"??placeholder="商品描述"?name="description"?id="description">????????????</div>????????</div>????????<div?class="form-group">????????????<label?class="control-label">價(jià)格</label>????????????<div>????????????????<input?class="form-control"?type="text"?placeholder="價(jià)格"?name="price"?id="price">????????????</div>????????</div>????????<div?class="form-group">????????????<label?class="control-label">圖片</label>????????????<div>????????????????<input?class="form-control"?type="text"?placeholder="圖片"??name="imgUrl"?id="imgUrl">????????????</div>????????</div>????????<div?class="form-group">????????????<label?class="control-label">庫(kù)存</label>????????????<div>????????????????<input?class="form-control"?type="text"?placeholder="庫(kù)存"?name="stock"?id="stock">????????????</div>????????</div>????????????????????????<div?class="form-actions">????????????<button?class="btn?blue"?type="submit"?id="create">提交創(chuàng)建</button>????????</div>????</div>????</body><script?type="text/javascript">????jQuery(document).ready(function(){????????$("#create").on("click",function(){????????????var?title?=?$("#title").val();????????????var?description?=?$("#description").val();????????????var?price?=?$("#price").val();????????????var?imgUrl?=?$("#imgUrl").val();????????????var?stock?=?$("#stock").val();????????????if(title==null?||?title?==?""){????????????????alert("商品名不能為空!");????????????????return?false;????????????}????????????if(description==null?||?description?==?""){????????????????alert("描述不能為空!");????????????????return?false;????????????}????????????if(price==null?||?price?==?""){????????????????alert("價(jià)格不能為空!");????????????????return?false;????????????}????????????if(imgUrl==null?||?imgUrl?==?""){????????????????alert("圖片url不能為空!");????????????????return?false;????????????}????????????if(stock==null?||?stock?==?""){????????????????alert("庫(kù)存不能為空!");????????????????return?false;????????????}????????????$.ajax({????????????????type:"POST",????????????????contentType:"application/x-www-form-urlencoded",????????????????url:"http://localhost:8080/item/create",????????????????data:{????????????????????"title":title,????????????????????"description":description,????????????????????"price":price,????????????????????"imgUrl":imgUrl,????????????????????"stock":stock????????????????},????????????????????xhrFields:{withCredentials:true},????????????????success:function(data){????????????????????if(data.status?=="success"){????????????????????????alert("創(chuàng)建成功")????????????????????}else{????????????????????????alert("創(chuàng)建失敗了"+data.data.errMsg)????????????????????}????????????????},????????????????error:function(data){????????????????????alert("創(chuàng)建失敗"+data.responseText)????????????????}????????????});????????????return?false;????????})????})????</script></html>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
這是前臺(tái)請(qǐng)求發(fā)送后,IDEA后臺(tái)的堆棧信息?