异步回调方法 调用返回Cannot render error page for request xxxxx as the response has already been commited. As a result, 解决方案
错误原因:
响应已经提交之后,仍然尝试渲染错误页面。这种情况可能会导致响应状态码不正确,从而引发其他问题。
解决方法:
1.不要return统一结果包装类,直接return null;
2.返回值直接改为void
返回结果通过:response.getOutputStream().write(new String(tr4XML).getBytes("UTF-8")); 返回。
3.将Controller类的注解:@Controller 改成 @RestController 返回json对象
如下DEMO:
@RestController
@Scope("prototype")
@RequestMapping("/pay/")
public class PayController {
@RequestMapping("notifyRcv")
public ReturnResult notifyRcv(@RequestParam Map<String, String> getParam, HttpServletRequest request, HttpServletResponse response) {
... 回调处理 业务逻辑
return new ReturnResult(0, "调用成功", new HashMap<>());
}
}
public class ReturnResult {
private Integer sub_code;
private String sub_message;
private Map sub_result;
// set get方法 ..
}
调用方式:
Map<String,String> notifyMap = new HashMap<>();
notifyMap.put(”orderId”, “tt12556688”);
notifyMap.put(”status", ”1”);
//前提是:@RestController而不是@Controller 来注解接口
String url = "http://localhost:8081/pay/notifyRcv";
//使用字符串接收
String json = restTemplate.postForObject(url, notifyMap, String.class);
logger.info(">>>>>>构造异步通知返回结果:json=[{}],params=[{}]",json,GsonUtils.toJson(notifyMap));
//仅仅测试,测试通过。使用对象接收也可以
ReturnResult json2 = restTemplate.postForObject(url, notifyMap, ReturnResult.class);
logger.info(">>>>>>构造异步通知返回结果22:json=[{}],params=[{}]",GsonUtils.toJson(json2),GsonUtils.toJson(notifyMap));
link:Controller和RestController注解区别