创建商品失败,未知错误
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;
}
}