作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试使用微消息公众平台时,微信服务器会调用我的一个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/
当我尝试使用微消息公众平台时,微信服务器会调用我的一个API,我需要返回一个 token 来验证我的身份。但是,当我这样直接返回token时,微信服务器提示验证错误。 @RequestMapping(
我尝试寻找两者之间的差异。我找到了代码,其中公共(public)服务方法通过向其传递 HttpServletRequest 和 HttpServletResponse 对象来依次调用 protecte
我是一名优秀的程序员,十分优秀!