开发者

How to instantiate a Home interface in a Seam Rest service if the Home interface has an User reference?

I am developing my first Seam + Android application and I am facing some new scenarios as expected.

Now specifically for this app, I decided not to keep any state because probably 80% to 90% of the server calls on the REST service will be specifically to do a simple synchronization and the client is gone for a while, so there is(probably) no point on keeping any state on the server side for these calls.

Well there is a specific routine where I need to persist an Object sent by device to the server side and this object is bind to a specific user(a very common scenario in my opinion).

So there goes some code to ilustrate.

The Home interface has a user injected by Seam:

@Name("pesoHome")
public class PesoHome extends EntityHome<Peso> {

    private static final long serialVersionUID = 6087763635317971234L;

    @In
    User user;

Than we need a rest service wich uses this home interface:

public class PesoRest{

    @Logger
    Log log;

    开发者_如何学运维@In(create = true)
    UserPesquisa userPesquisa;

    @In(create = true)
    PesoHome pesoHome;

    @POST
    @Path("/cadastrar/{userEmail}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response cadastraPeso(@PathParam("userEmail") String email, String jsonString)
    {
            String json = jsonString;
            System.out.println("String jSon no servidor ==>> " + json);

And than because of the User injection in the home interface the following error triggers at runtime:

Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: pesoHome
    at org.jboss.seam.Component.newInstance(Component.java:2144)
    ... 46 more
Caused by: org.jboss.seam.RequiredException: @In attribute requires non-null value: pesoHome.user

Notice that in the body of the request the JSon object brings information about this user wich allows to recover this user using the also injected userPesquisa reference. I know I could have a different approach where I would build a seam component with no references to user and than in it build the user and place it in a new conversation and than inject the home reference I need, but...

I was wondering that probably there should be some more standard approach for this as it is a common scenario. Any suggestions? :)

[]s


as suggested by Arthur in this thread comments I have changed a bit the code and now it works:

PesoHome:

    @In(required=false)
    User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

and than in PesoRest:

User usuario = userPesquisa.pesquisaUsuarioPorEmail(email);
pesoHome.setUser(usuario);

And than because now user is not required on creation anymore, the error is not triggered and I can do a late bind after searching DB to recover the correct user. Quite simple solution. Thanks Arthur.

[]s

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜