前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >异步回调方法 调用返回Cannot render error page for request [xxxxx] as the response has already been commited. A

异步回调方法 调用返回Cannot render error page for request [xxxxx] as the response has already been commited. A

作者头像
oktokeep
发布2025-03-25 13:37:02
发布2025-03-25 13:37:02
4600
代码可运行
举报
文章被收录于专栏:第三方工具第三方工具
运行总次数:0
代码可运行

异步回调方法 调用返回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:

代码语言:javascript
代码运行次数:0
运行
复制
@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方法 ..    
}

调用方式:

代码语言:javascript
代码运行次数:0
运行
复制
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注解区别

https://cloud.tencent.com/developer/article/2507772

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-03-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档