There was an unexpected error (type=Bad Request, status=400). Required Long parameter 'isPay' is not

推荐阅读
>>我的博客
>>学习交流或获取更多资料欢迎加入QQ群

异常信息

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Mar 24 10:42:00 CST 2019
There was an unexpected error (type=Bad Request, status=400).
Required Long parameter 'isPay' is not present

场景描述

package com.ysy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ysy.api.order.OrderService;
import com.ysy.base.BaseApiService;
import com.ysy.base.ResponseBase;
import com.ysy.dao.OrderDao;

@RestController
public class OrderServiceImpl extends BaseApiService implements OrderService {
	@Autowired
	private OrderDao orderDao;

	@Override
	public ResponseBase updateOrder(@RequestParam("isPay") Long isPay, @RequestParam("payId") String aliPayId,
			@RequestParam("orderNumber") String orderNumber) {
		int updateOrder = orderDao.updateOrder(isPay, aliPayId, orderNumber);
		if (updateOrder <= 0) {
			return setReasultError("系统错误!");
		}
		return setReasultSuccess();
	}

}

异常原因

一旦我们在方法中定义了@RequestParam变量,如果访问的URL中不带有相应的参数,就会抛出异常——这是显然的,Spring尝试帮我们进行绑定,然而没有成功。但有的时候,参数确实不一定永远都存在,这是我们可以通过定义required属性:

@RequestParam(name="id",required=false)
//在参数不存在的情况下,可能希望变量有一个默认值
@RequestParam(name="id",required=false,defaultValue="0")

解决方案

package com.ysy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ysy.api.order.OrderService;
import com.ysy.base.BaseApiService;
import com.ysy.base.ResponseBase;
import com.ysy.dao.OrderDao;

@RestController
public class OrderServiceImpl extends BaseApiService implements OrderService {
	@Autowired
	private OrderDao orderDao;

	@Override
	public ResponseBase updateOrder(@RequestParam(value = "payState", required = false) Long payState,
			@RequestParam(value = "payId", required = false) String payId,
			@RequestParam(value = "orderNumber", required = false) String orderNumber) {
		int updateOrder = orderDao.updateOrder(payState, payId, orderNumber);
		if (updateOrder <= 0) {
			return setReasultError("系统错误!");
		}
		return setReasultSuccess();
	}

}

参考资料

在这里插入图片描述

这里是引用

你可能感兴趣的:(异常纪录)