diff --git a/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxMenuController.java b/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxMenuController.java index 516b2cc..130956b 100644 --- a/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxMenuController.java +++ b/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxMenuController.java @@ -1,5 +1,6 @@ package com.github.binarywang.demo.wx.mp.controller; +import lombok.AllArgsConstructor; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.bean.menu.WxMenu; import me.chanjar.weixin.common.bean.menu.WxMenuButton; @@ -7,7 +8,6 @@ import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; import me.chanjar.weixin.mp.bean.menu.WxMpMenu; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -21,15 +21,11 @@ /** * @author Binary Wang(https://github.com/binarywang) */ +@AllArgsConstructor @RestController @RequestMapping("/wx/menu/{appid}") public class WxMenuController { - private WxMpService wxService; - - @Autowired - public WxMenuController(WxMpService wxService) { - this.wxService = wxService; - } + private final WxMpService wxService; /** *
diff --git a/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxPortalController.java b/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxPortalController.java index fad2d14..7c58a81 100644 --- a/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxPortalController.java +++ b/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxPortalController.java @@ -1,5 +1,8 @@ package com.github.binarywang.demo.wx.mp.controller; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j; +import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.mp.api.WxMpMessageRouter; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -20,20 +23,13 @@ /** * @author Binary Wang(https://github.com/binarywang) */ +@Slf4j +@AllArgsConstructor @RestController @RequestMapping("/wx/portal/{appid}") public class WxPortalController { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - private WxMpService wxService; - - private WxMpMessageRouter messageRouter; - - @Autowired - public WxPortalController(WxMpService wxService, WxMpMessageRouter messageRouter) { - this.wxService = wxService; - this.messageRouter = messageRouter; - } + private final WxMpService wxService; + private final WxMpMessageRouter messageRouter; @GetMapping(produces = "text/plain;charset=utf-8") public String authGet(@PathVariable String appid, @@ -42,7 +38,7 @@ public String authGet(@PathVariable String appid, @RequestParam(name = "nonce", required = false) String nonce, @RequestParam(name = "echostr", required = false) String echostr) { - this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, + log.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr); if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) { throw new IllegalArgumentException("请求参数非法,请核实!"); @@ -68,7 +64,7 @@ public String post(@PathVariable String appid, @RequestParam("openid") String openid, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", required = false) String msgSignature) { - this.logger.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}]," + log.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}]," + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", openid, signature, encType, msgSignature, timestamp, nonce, requestBody); @@ -94,7 +90,7 @@ public String post(@PathVariable String appid, // aes加密的消息 WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxService.getWxMpConfigStorage(), timestamp, nonce, msgSignature); - this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString()); + log.debug("\n消息解密后内容为:\n{} ", inMessage.toString()); WxMpXmlOutMessage outMessage = this.route(inMessage); if (outMessage == null) { return ""; @@ -103,7 +99,7 @@ public String post(@PathVariable String appid, out = outMessage.toEncryptedXml(wxService.getWxMpConfigStorage()); } - this.logger.debug("\n组装回复信息:{}", out); + log.debug("\n组装回复信息:{}", out); return out; } @@ -111,7 +107,7 @@ private WxMpXmlOutMessage route(WxMpXmlMessage message) { try { return this.messageRouter.route(message); } catch (Exception e) { - this.logger.error("路由消息时出现异常!", e); + log.error("路由消息时出现异常!", e); } return null; diff --git a/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxRedirectController.java b/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxRedirectController.java index fa4e914..6624e6f 100644 --- a/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxRedirectController.java +++ b/src/main/java/com/github/binarywang/demo/wx/mp/controller/WxRedirectController.java @@ -1,6 +1,7 @@ package com.github.binarywang.demo.wx.mp.controller; import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration; +import lombok.AllArgsConstructor; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; @@ -17,15 +18,11 @@ /** * @author Edward */ +@AllArgsConstructor @Controller @RequestMapping("/wx/redirect/{appid}") public class WxRedirectController { - private WxMpService wxService; - - @Autowired - public WxRedirectController(WxMpService wxService) { - this.wxService = wxService; - } + private final WxMpService wxService; @RequestMapping("/greet") public String greetUser(@PathVariable String appid, @RequestParam String code, ModelMap map) {