我尝试开发服务器端 API。我正在使用 GraphQL 并想从服务器端设置一个 cookie,但是当我尝试使用HttpServletResponse
它设置 cookie 时,我得到了一个NullPointerException
?
这是我的响应视图类:
package org.jembi.appstore.service.graphql.api.response.view;
public class ResponseView {
private int code;
private String message;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我在这里设置cookie:
@GraphQLMutation(name="login")
public ResponseView login(@GraphQLArgument(name="msisdn") String msisdn, @GraphQLArgument(name="password")String password, HttpServletResponse response) {
ResponseView responseView = new ResponseView();
User user = new User();
System.out.println(msisdn.length());
Cookie cookie = null;
if(msisdn.length()==10) {
user.setPassword(password);
user.setMsisdn(msisdn);
int code = userDao.getUser(user);
responseView.setCode(code);
if(code==100) {
responseView.setMessage("User Logged In Successfully");
cookie = new Cookie("token", UUID.randomUUID().toString());
//responseView.doPost(req, resp);
int hour = 3600000;
int exp = 14 * 24 * hour; //2 weeks
cookie.setMaxAge(exp);
//cookie.setPath("/");
cookie.setHttpOnly(true);
response.addCookie(cookie);
//responseView.setCookie(cookie);
}
else
responseView.setMessage("Invalid credentials");
}else {
responseView.setCode(400);
responseView.setMessage("Bad Request");
}
return responseView;
}