当我尝试使用微信公共平台时,微信服务器将调用我的一个API,我需要返回一个令牌来验证我的身份。但是,当我直接返回令牌时,weChat服务器会提示验证错误。@ResponseBody和HttpServletResponce之间有什么区别
@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
时,春消息写入响应的主体。 那么他们之间有什么区别?为什么第一种方法是错误的?
responseBody只是响应的主体,而'HttpServletResponce'包含整个响应,例如标题,Cookie,正文等。 –
发布的代码是否正确?两种方法都有@ResponseBody? –