为了账号安全,请及时绑定邮箱和手机立即绑定

创建商品失败,未知错误

debug之后发现代码走到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;

    //创建商品的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请求用来创建商品
        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;
    }
}


正在回答

5 回答

感谢老哥,之前一直找不到错误,用你说的debug调试果然一下子就看到错误解决了

0 回复 有任何疑惑可以回复我~
#1

慕沐604797

请问是怎么解决的
2021-09-22 回复 有任何疑惑可以回复我~

找到bug了 在itemModel中Integer变量stock上我用了@NotBlank,应该用@NotNull才对 所以入参校验直接失败,debug断点打在BaseController中的Ex上变量有详细的描述才定位到Bug,搞了几个小时才搞定 铭记一生

0 回复 有任何疑惑可以回复我~
#1

慕慕0284258

谢谢老哥,我也是找了一晚上一直找不到,看了你这个发现我的图片的@NotBlank放在sales上了
2020-05-15 回复 有任何疑惑可以回复我~

debug断点从itemcontroller的


debug断点从ItemController中的

ItemModel mode = itemService.createItem(itemModel);

方法后就直接跳转到了

InvocableHandlerMethod

这个spring类库中的方法中去了

搞不懂!求解释

0 回复 有任何疑惑可以回复我~

前台代码

<!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">创建商品</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">价格</label>            <div>                <input class="form-control" type="text" placeholder="价格" 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">库存</label>            <div>                <input class="form-control" type="text" placeholder="库存" name="stock" id="stock">            </div>        </div>                        <div class="form-actions">            <button class="btn blue" type="submit" id="create">提交创建</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("价格不能为空!");                return false;            }            if(imgUrl==null || imgUrl == ""){                alert("图片url不能为空!");                return false;            }            if(stock==null || stock == ""){                alert("库存不能为空!");                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("创建成功")                    }else{                        alert("创建失败了"+data.data.errMsg)                    }                },                error:function(data){                    alert("创建失败"+data.responseText)                }            });            return false;        })    })    </script></html>


0 回复 有任何疑惑可以回复我~

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

这是前台请求发送后,IDEA后台的堆栈信息 

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
SpringBoot构建电商基础秒杀项目
  • 参与学习       49064    人
  • 解答问题       954    个

应用SpringBoot快速搭建拥有用户、商品、交易及秒杀活动的电商秒杀应用。

进入课程

创建商品失败,未知错误

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信