我一直在'No class was registered (through reference chain: ... )',但我注册了我的enties



我正在谷歌应用引擎上编写我的送餐应用程序。我有两个工作实体(类):MenuProfile及其端点类。它工作得很好。我添加了第三个实体:订单,看起来像这样:

@Entity
public class Order {
@Id
public Long id;
@Load
public Ref<Profile> profile;
@Load
public Ref<Menu> menu;
public Order() {}
public Long getId() { return id; }
public void setId(Long id) {this.id = id;}
public Profile getProfile() { return profile.get(); }
public void setProfile(Profile profile) { this.profile = Ref.create(profile);}
public Menu getMenu() {return menu.get();}
public void setMenu(Menu menu) { this.menu = Ref.create(menu); }
}

我有一个OfyService类,我的所有实体都在其中注册:

public class OfyService {
static {
ObjectifyService.register(Menu.class);
ObjectifyService.register(Profile.class);
ObjectifyService.register(Order.class);
}
public static Objectify ofy() {
return ObjectifyService.ofy();
}
public static ObjectifyFactory factory() {
return ObjectifyService.factory();
}
}

我在每个端点类中都调用这个静态的of()方法,但在Api Explorer上仍然会出现错误。上面写着:

 "com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException:  No class 'hu.iit.bme.api.model.Menu' was registered (through reference chain: hu.iit.bme.api.model.Order["menu"])

知道吗?我该怎么办?

非常感谢!

@jirungaray的答案可能是问题的根源——很容易意外导入错误的ofy()方法并错过静态初始化程序块。

我正在改变我对以这种方式注册课程是否是最佳实践的看法。这个问题提得太多了。

重要的是,在应用程序启动之前,类应该在一个线程中注册。有很多方法可以保证这一点,而不必求助于静态初始化器块。最可靠的方法是将您的注册转移到在web.xml中注册的ServletContextListener中。

我将很快用这个建议更新Objectify文档。

@stickfigure是对的。尽管我已经注册为@Sappy,但我还是收到了错误。

我所做的是创建一个类OfyHelper并在web.xml 中注册

public class OfyHelper implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
    // This will be invoked as part of a warmup request, or the first user request if no warmup
    // request.
    ObjectifyService.register(User.class);
    ObjectifyService.register(BeerBrand.class);
    ObjectifyService.register(BeerStyle.class);
    ObjectifyService.register(BeerBottle.class);
    ObjectifyService.register(BeerCategory.class);
    ObjectifyService.register(Beer.class);
}
public void contextDestroyed(ServletContextEvent event) {
    // App Engine does not currently invoke this method.
}

并在web.xml中插入:

<listener>
    <listener-class>br.com.jluiz.myPackage.OfyHelper</listener-class>
</listener>

您可以在此处获得更多帮助:https://cloud.google.com/appengine/docs/java/gettingstarted/usingdatastore

希望它能帮助

确保在端点上使用import static com.yourcode.OfyService.ofy;而不是import static com.googlecode.objectify.ObjectifyService.ofy

编辑:根据Objectify文档,您应该这样注册您的实体:

static {
        factory().register(Thing.class);
        factory().register(OtherThing.class);
    }

看起来你没有在工厂注册你的实体,这将在以后产生你的服务。

最新更新