Can not deserialize instance of java.lang.Integer out of START_OBJECT token 错误解决

今天在跟前端联调一个新的接囗时遇到这个错误 ,错误代码
   /**
     * 扫描车牌号码得到车辆信息
     * @param plateId
     * @return
     */
    @RequestMapping(value = "scanPlateNumber")
    @ResponseBody
    public ResponseResult scanPlateNumber(@RequestBody Integer plateId) {
        VehicleEntity vehicleEntity;
        try{
            logger.info("批次发货--添加车牌id:"+plateId);
因为@RequestBody是将post请求中content值转为一个整体对象。@RequestBody的解析有两个条件:
1.POST请求中content的值必须为json格式(存储形式可以是字符串,也可以是byte数组);
2.@RequestBody注解的参数类型必须是完全可以接收参数值的类型,比如:Map,JSONObject,或者对应的JavaBean;
所以Integer类型不能作为@RequestBody注解的参数类型,将上述代码修改如下(前端改成get请求)即可:
 
   /**
     * 扫描车牌号码得到车辆信息
     * @param plateId
     * @return
     */
    @RequestMapping(value = "scanPlateNumber")
    @ResponseBody
    public ResponseResult scanPlateNumber(@RequestParam Integer plateId) {
        VehicleEntity vehicleEntity;
        try{
            logger.info("批次发货--添加车牌id:"+plateId);
            vehicleEntity = vehicleDAO.findVehicleById(plateId);

 下面总结下我所在做项目常用接囗格式:

     1.post请求,参数通常为一个javaBean

    /**
     * 包裹发货(中心仓)
     * @param orderPackageDeliveryParam
     * @return
     */
    @RequestMapping("deliveryOrderPackage")
    @ResponseBody
    public ResponseResult deliveryOrderPackage(@RequestBody OrderPackageDeliveryParam orderPackageDeliveryParam) {
        try {
            List orderPackageAllEntityList = orderPackageDeliveryParam.getOrderPackageDeliveryVo();
    2.get请求

a.参数放在url里(旧的接囗常用)

   /**
     * 扫描仓位码得到仓位信息
     * @param cityId
     * @return
     */
    @RequestMapping(value = "scanWarehousePositionCode/{cityId}")
    @ResponseBody
    public ResponseResult scanWarehousePositionCode(@PathVariable("cityId") Long cityId) {
        TimeAddressEntity timeAddressEntity;
        try {
            timeAddressEntity = timeAddressService.findTimeAddressWarehouseById(cityId);

                b.参数放在url后

   /**
     * 扫描车牌号码得到车辆信息
     * @param plateId
     * @return
     */
    @RequestMapping(value = "scanPlateNumber")
    @ResponseBody
    public ResponseResult scanPlateNumber(@RequestParam Integer plateId) {
        VehicleEntity vehicleEntity;
        try{
            logger.info("批次发货--添加车牌id:"+plateId);
            vehicleEntity = vehicleDAO.findVehicleById(plateId);

你可能感兴趣的:(java)