0

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 :)

Benjamin Bini
  • 311
  • 4
  • 15
  • WildFly 8.1 by default uses the Jackson2 library for JSON serialization. I think `JsonAutoDetect` is only available in Jackson 1, hence it's not being registered. Actually more importantly - why are you registering an EJB to contain this data? `SessionScoped` should be enough, no? JAX-RS really doesn't define a session though. – John Ament Nov 02 '14 at 15:02
  • I just looked at Jackson documentation, it works with Jackson 2. And the problem should occur before JSON serialization, shouldn't it ? – Benjamin Bini Nov 02 '14 at 15:07
  • To be a bit more clear, here is what I got before login : { loggedIn: false, userId: null } and after : { loggedIn: true, userId: null } – Benjamin Bini Nov 02 '14 at 15:26
  • @JohnAment You're right, I removed "Stateless" and "Stateful", but it doesn't change anything. What I don't understand is why it works well with my Boolean and not with my Integer. This is nonsense. – Benjamin Bini Nov 02 '14 at 16:41
  • Can you add details about what `User user = userService.getUserByMailAddress(userMail.trim());` does? Can you also add your import statements - you didnt' clarify if you were using Jackson 1 or Jackson 2. – John Ament Nov 02 '14 at 20:08
  • I updated my post with imports. UserService is a business object accessing DB through ORMLite DAO. User is correctly instantiated by ORMLite if a user exists with the given mail address. – Benjamin Bini Nov 02 '14 at 23:34
  • @JohnAment And I use Jackson 2 :) – Benjamin Bini Nov 03 '14 at 12:48

0 Answers0