I am working on a webservice using RESTEasy.
I have an "authentication" webservice with two methods : "login" and "logout".
I have a stateful session scoped bean, UserData with two attributes : "loggedIn", boolean, and "userId", Integer.
I am injecting UserData in my Authentication class. It is working well for the "loggedIn" attribute, when I call "login" method, it is set to true and then it remains true until the session ends.
But strangely it is not working with "userId" attribute. When I call "login" method, I set userId to my user id but right after the "setUserId" method is called, userId is still null.
Here is Authentication class code (without logOut method, unused for now ) :
package com.bini.dev.dilemme.web.api;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import com.bini.dev.dilemme.business.service.UserService;
import com.bini.dev.dilemme.persistence.model.User;
import com.bini.dev.dilemme.web.UserData;
@Path("/auth")
public class AuthenticationApi {
@Inject
private UserData userData;
@Inject
private UserService userService;
@GET
@Path("login")
@Produces("application/json")
public UserData logIn(@QueryParam("mail") String userMail, @QueryParam("password") String userHashedPassword) {
try {
this.logOut();
User user = userService.getUserByMailAddress(userMail.trim());
if (user == null)
throw new Exception("User does not exists");
boolean loggedIn = userService.checkUserPassword(user, userHashedPassword.trim());
if (loggedIn) {
userData.setLoggedIn(true);
userData.setUserId(user.getUserId());
}
return userData;
} catch (Exception e) {
e.printStackTrace();
return userData;
}
}
}
And here is the code of "UserData" class :
package com.bini.dev.dilemme.web;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
@SessionScoped
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC,
getterVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.NONE)
public class UserData implements Serializable {
protected Boolean loggedIn;
protected Integer userId;
public UserData() {
this.loggedIn = false;
this.userId = null;
}
public Boolean isLoggedIn() {
return loggedIn;
}
public void setLoggedIn(Boolean loggedIn) {
this.loggedIn = loggedIn;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
}
I tried with or without "Stateful" and "Stateless" annotation. Doesn't change anything.
I really have not idea what to do.
I thought it might be a "setter" syntax error, but I really don't see where.
EDIT : BTW, I am using Weld and RestEASY with WildFly server. Thanks :)