gpt4 book ai didi

java - @ResponseBody 和 HttpServletResponce 有什么区别

转载 作者:行者123 更新时间:2023-12-01 10:45:23 25 4
gpt4 key购买 nike

当我尝试使用微消息公众平台时,微信服务器会调用我的一个API,我需要返回一个 token 来验证我的身份。但是,当我这样直接返回token时,微信服务器提示验证错误。

@RequestMapping(value="/userFollow", method= RequestMethod.GET)
@ResponseBody
public String weChatToken(HttpServletRequest request,String signature,String timestamp,String nonce,String echostr,HttpServletResponse response) throws IOException, DocumentException {
String result=weChatService.checkSignature(signature,timestamp,nonce,echostr);
return result;
}

然后我更改了代码,如下所示。这次验证正确。

@RequestMapping(value="/userFollow", method= RequestMethod.GET)
@ResponseBody
public String weChatToken(HttpServletRequest request,String signature,String timestamp,String nonce,String echostr,HttpServletResponse response) throws IOException, DocumentException {
String result=weChatService.checkSignature(signature,timestamp,nonce,echostr);
PrintWriter pw=response.getWriter();
pw.write(result);
pw.flush();
return null;
}

我在 Google 上搜索并发现,当使用 @Responsebody 时,Spring 将消息写入响应正文。那么它们之间有什么区别呢?为什么第一种方法是错误的?

最佳答案

HTTP 响应由状态代码、一些 header 和正文组成。使用 @ResponseBody 意味着您的方法给出了正文的内容,而不是其他内容。使用 HttpServletResponse 使您的方法能够设置响应的所有方面,但使用起来有点不方便。

关于java - @ResponseBody 和 HttpServletResponce 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34219823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com