我从服务器收到以下错误响应.
HTTP状态500 -
类型异常报告
信息
description服务器遇到内部错误(),导致无法完成此请求.
例外
javax.servlet.ServletException:java.lang.UnsupportedOperationException:尝试序列化java.lang.Class:org.hibernate.proxy.HibernateProxy.忘了注册一个类型适配器?
根本原因
java.lang.UnsupportedOperationException:尝试序列化java.lang.Class:org.hibernate.proxy.HibernateProxy.忘了注册一个类型适配器?
从Java调试器:
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer@7632012e
Run Code Online (Sandbox Code Playgroud)
我正在使用Gson将我的Java对象转换为JSON.下面我贴了一些代码.
这是我的资源:
@Stateless
@LocalBean
@Path("/autos")
@Produces(MediaType.APPLICATION_JSON)
public class AutoResource {
@EJB
private CarAssembler warehouse;
@Context
private UriInfo uriInfo;
@GET
public Response allAutos() {
// Building a context, lots of code...
// Creating a Gson instance and configures it...
final Auto auto = warehouse.list(context);
final String autoJson = gson.toJson(auto);
return Response.ok(autoJson).build();
}
}
Run Code Online (Sandbox Code Playgroud)
CarAssembler只是一个调用存储库的服务.我没有在这里粘贴服务代码.
库:
@Override
public Question findById(final int id, final FetchType fetchType) {
final Auto question = …
Run Code Online (Sandbox Code Playgroud) 我试图将一个集合(从Hibernate获得)转换为使用Gson的json字符串,用于我的restful服务.但是,该服务给了我http 500错误.
我的代码如下:
@GET
@Path("/getScheduleLookUp")
@Produces(MediaType.APPLICATION_JSON)
public String getScheduleLookUp() throws IOException {
ScheduleLookupDAO lookupDAO = new ScheduleLookupDAO();
return new Gson().toJson(lookupDAO.getAllScheduleLookUps());
}
Run Code Online (Sandbox Code Playgroud)
查询数据库后返回的hibernate列表返回带有一些垃圾值的classname,可能是因为Hibernate的延迟加载.我尝试通过关闭延迟加载,但最终得到了相同的结果.我的班级结构有点像这样
public class ScheduleLookup extends BaseModel {
private static final long serialVersionUID = -2561410864465885712L;
private Integer scheduleId;
private String scheduleName;
private ClientLookup clientLookup;
private String description;
private Route route;
private String routeName;
private Double estimatedDuration;
private Double calculatedDuration;
private UserLookup createdBy;
private Date createdAt;
private UserLookup lastUpdatedBy;
private Date lastUpdatedAt;
private Boolean delFlag;
private UserLookup deletedBy;
private Date deletedAt; …
Run Code Online (Sandbox Code Playgroud)