将 Resteasy 与 javassist 一起使用?

发布于 2024-08-12 14:36:36 字数 971 浏览 3 评论 0原文

我正在尝试使用 Resteasy 来提供 spring-hibernate 获取的一些实体。

我配置了一种返回 POJO 并按预期工作的方法:

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_XML)
public Episode getTestEpisode() {
  Episode e = new Episode();
  e.setEpisodename("test");
  return e;
}

Produces:

<episode episodeId="0">
 <combinedEpisodenumber>0.0</combinedEpisodenumber>
 <combinedSeason>0</combinedSeason>
 <episodename>test</episodename>
 <episodenumber>0</episodenumber>
 <seasonId>0</seasonId>
 <seasonnumber>0</seasonnumber>
</episode>

但是,如果我尝试从 spring/hibernate 返回某些内容,则会收到错误:

无法找到类型为 com.company.domain.Episode_$$_javassist_27 媒体类型为 application/xml 的响应对象的 MessageBodyWriter

我想这对 javassist 来说是一种魔力,但是我认为它不是预期的类,这让 JAX-B 感到困惑。我可以告诉 JAX-B 在哪里查找注释,或者我可以从该对象获取 POJO 吗?

从未直接使用过javassist,所以不确定它是如何工作的。

I'm trying to use resteasy to serve out some entities fetched by spring-hibernate.

I've configured one method which returns a POJO and works as expected:

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_XML)
public Episode getTestEpisode() {
  Episode e = new Episode();
  e.setEpisodename("test");
  return e;
}

Produces:

<episode episodeId="0">
 <combinedEpisodenumber>0.0</combinedEpisodenumber>
 <combinedSeason>0</combinedSeason>
 <episodename>test</episodename>
 <episodenumber>0</episodenumber>
 <seasonId>0</seasonId>
 <seasonnumber>0</seasonnumber>
</episode>

However, if I try and return something from spring/hibernate I get an error:

Could not find MessageBodyWriter for response object of type: com.company.domain.Episode_$$_javassist_27 of media type: application/xml

I imagine this is some magic with javassist, however I think it's confusing JAX-B by not being the expected class. Can I tell JAX-B where to look for the annotations, or can I get a POJO from this object?

Never used javassist directly, so not sure how it works.

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

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

发布评论

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

评论(1

余厌 2024-08-19 14:36:37

您必须对对象进行去代理..找到一个有效的实用方法..

转换 Hibernate真实对象的代理

public static <T> T initializeAndUnproxy(T entity) {
if (entity == null) {
    throw new 
       NullPointerException("Entity passed for initialization is null");
}

Hibernate.initialize(entity);
if (entity instanceof HibernateProxy) {
    entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer()
            .getImplementation();
}
return entity;

}

You have to de-proxy the object.. found a utility method that works..

Converting Hibernate proxy to real object

public static <T> T initializeAndUnproxy(T entity) {
if (entity == null) {
    throw new 
       NullPointerException("Entity passed for initialization is null");
}

Hibernate.initialize(entity);
if (entity instanceof HibernateProxy) {
    entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer()
            .getImplementation();
}
return entity;

}

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