Spring Cloud 常见错误汇总

1.Method has too many Body parameters

解决:在接口参数前面加@RequestParam("xzh")注解。

    ResponseMessage userCouponsList(@RequestParam("xzh")String xzh, @RequestParam("type")Integer type);

2.JSON parse error: Can not construct instance of

解决:将服务之间的调用方式改为一致,比如:接口是post请求,那么客户端也用post。

 服务提供者:

    @PostMapping("/test")
    ResponseMessage userCouponsList(@RequestParam("xzh")String xzh, @RequestParam("type")Integer type);

服务调用端:

@FeignClient(value = "coupon-service", path = "/coupon", fallback = CouponClientFallBack.class)
public interface CouponClient {

    @RequestMapping(value = "/userCouponsList", method = RequestMethod.POST)
    ResponseMessage<PageBean<CouponVO>> userCouponsList(CouponReq couponReq);
}

3.服务之间调用时参数封装不进去

解决:在Controller层方法参数前面添加@RequestBody注解。

@Api(tags = "我的消息服务")
@RequestMapping("message")
@RestController
public class MessageController implements MessageService {

    @Resource(name = "messageService")
    private MessageService messageService;

    @Override
    public ResponseMessage<PageBean<MessageVO>> messageList(@RequestBody messageReq req) {
        return messageService.messageList(req);
    }
}

4.FeignException: status 404

解决:接口路径错误,需填写接口正确路径。

5.Can not deserialize value of type java.util.Date from String "2018-11-28 14:01:08": not a valid representation (error: Failed to parse Date value '2018-11-28 14:01:08': Can not parse date "2018-11-28 14:01:08": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))

两个服务之间调用的时候,传输的数据是Sting类型的json字符串,B服务的Date字段被转化成了String,再用Date类型接收,就出现类型异常了。

解决:时间参数格式转换错误,建议将时间格式定义成String,在查询的时候进行转换。

DATE_FORMAT(`time`,'%Y-%m-%d') as `time`

6.Required request body is missing

解决:因为GetMapping 不支持@RequestBody,将接口改为POST请求。

    @PostMapping("/spreadList")
    ResponseMessage<PageBean<SpreadVO>> spreadList(SpreadReq req);

猜你喜欢

转载自blog.csdn.net/Anenan/article/details/84754419