如果 Home 界面有 User 引用,如何在 Seam Rest 服务中实例化 Home 界面?

发布于 2024-09-25 13:41:17 字数 1544 浏览 5 评论 0原文

我正在开发我的第一个 Seam + Android 应用程序,并且我正面临着一些预期的新场景。

现在专门针对这个应用程序,我决定不保留任何状态,因为 REST 服务上的服务器调用可能有 80% 到 90% 将专门用于执行简单的同步,并且客户端会消失一段时间,所以有(可能)没有必要在服务器端为这些调用保留任何状态。

好吧,有一个特定的例程,我需要持久保存设备发送到服务器端的对象,并且该对象绑定到特定用户(我认为这是一个非常常见的场景)。

所以有一些代码可以说明。

Home 接口有一个由 Seam 注入的用户:

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

    private static final long serialVersionUID = 6087763635317971234L;

    @In
    User user;

我们需要一个使用此 home 接口的休息服务:

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

并且由于 home 接口中的用户注入,在运行时会触发以下错误:

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

请注意,在请求正文中,JSon对象带来有关此用户的信息,允许使用还注入的 userPesquisa 引用来恢复此用户。 我知道我可以有一种不同的方法,我可以构建一个不引用用户的接缝组件,而不是在其中构建用户并将其放置在新的对话中,然后注入我需要的家庭引用,但是...
我想知道可能应该有一些更标准的方法,因为这是一种常见的情况。有什么建议吗? :)

[]s

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2024-10-02 13:41:17

正如 Arthur 在该线程评论中所建议的那样,我对代码做了一些更改,现在它可以工作了:

PesoHome:

    @In(required=false)
    User user;

    public User getUser() {
        return user;
    }

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

和 PesoRest: 相比

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

,因为现在不再需要用户创建,所以不会触发错误,我可以稍后做搜索数据库后绑定以恢复正确的用户。非常简单的解决方案。谢谢亚瑟。

[]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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文