我在用nutzwk的apitoken的时候,每次生成token调用getKey方法的时候,总是会报NullPointerException,说redisService是null,但是我Inject过了啊。搞不懂了。
2019-04-26 00:03:35,811 org.nutz.mvc.impl.processor.FailProcessor.process(FailPr
ocessor.java:28) WARN - Error@/public/wx/wechat/8b5e78c086a9426db738094a7b43d47
f/back :
java.lang.NullPointerException
at cn.wizzer.app.web.commons.utils.TokenUtil.getKey(TokenUtil.java:43)
at cn.wizzer.app.web.commons.utils.TokenUtil.generateToken(TokenUtil.jav
a:71)
at cn.wizzer.app.web.modules.controllers.front.wx.WechatController.back(
WechatController.java:125)
@Inject
private RedisService redisService;
/**
* 获取KEY
*
* @param appid appid
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
private Key getKey(String appid) throws IOException, ClassNotFoundException {
Key key;
43 byte[] obj = redisService.get(("api_token:" + appid).getBytes());
if (obj != null) {
ObjectInputStream keyIn = new ObjectInputStream(new ByteArrayInputStream(obj));
key = (Key) keyIn.readObject();
keyIn.close();
} else {
key = MacProvider.generateKey();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bao);
oos.writeObject(key);
obj = bao.toByteArray();
redisService.set(("api_token:" + appid).getBytes(), obj);
redisService.expire(("api_token:" + appid).getBytes(), 7202);//2小时零2秒后自动删除
}
return key;
}