这与Hibernate: Could not deserialize - invalid stream header有关。
我无法真正使用这些答案,所以我再次创造了这个(也许情况有所改变)。
我正在构建一个SpringBoot应用程序,这里是我使用的库。
org.springframework.boot:1.5.4.RELEASE
hibernate-core:5.0.12
hibernate-spatial
hibernate-jpa-2.1-api:1.0.0
postgis-jdbc:2.2.1
postgresql:42.1.4
声明我的实体的代码
package be.naturalsciences.tiles.model;
/**
*
* @author michel
*/
import org.postgis.Point;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@Entity
@Table(schema = "idod", name = "coordinates")
@XmlRootElement
public class SCoordinates {
@Id
@Basic(optional = false)
@Column(name = "theid")
public Integer id;
@Basic(optional = false)
@Column(name = "latitude")
public BigDecimal latitude;
@Basic(optional = false)
@Column(name = "longitude")
public BigDecimal longitude;
@Basic(optional = false)
@Column(columnDefinition = "geometry", name="geometry")
public Point point;
}
我通过一个足够简单的Repository对象访问我的实体。
package be.naturalsciences.tiles.services;
/**
*
* @author michel
*/
import be.naturalsciences.tiles.model.SCoordinates;
import java.io.Serializable;
import org.springframework.data.repository.Repository;
public interface SCoordinatesRepository extends Repository<SCoordinates, Integer>{
SCoordinates findById(Integer id);
}
但我确实得到了这个java.io.StreamCorruptedException
例外(我确实缩短了它)
2018-02-23 09:59:13,411 INFO [be.naturalsciences.tiles.controllers.SCoordinatesController] (default task-2) Entering SCoordinatesController.findById()
2018-02-23 09:59:13,413 INFO [be.naturalsciences.tiles.services.SCoordinatesServiceImpl] (default task-2) Start of SCoordinatesServiceImpl.findById(), parameter is : 2282
2018-02-23 09:59:13,436 DEBUG [org.hibernate.SQL] (default task-2)
select
scoordinat0_.theid as theid1_0_,
scoordinat0_.latitude as latitude4_0_,
scoordinat0_.longitude as longitud5_0_,
scoordinat0_.geometry as geometry7_0_
from
idod.coordinates scoordinat0_
where
scoordinat0_.theid=?
2018-02-23 09:59:13,608 ERROR [org.springframework.boot.web.support.ErrorPageFilter] (default task-2) Forwarding to error page from request [/scoordinates/2282] due to exception [could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize]: org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:488)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy134.findById(Unknown Source)
at be.naturalsciences.tiles.services.SCoordinatesServiceImpl.findById(SCoordinatesServiceImpl.java:25)
at be.naturalsciences.tiles.controllers.SCoordinatesController.findById(SCoordinatesController.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115)
at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59)
at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:243)
at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:287)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:138)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:27)
at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:60)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2790)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1729)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1655)
at org.hibernate.loader.Loader.getRow(Loader.java:1544)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:930)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doList(Loader.java:2617)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429)
at org.hibernate.loader.Loader.list(Loader.java:2424)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1326)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:529)
at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:54)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:206)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:85)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 103 more
Caused by: java.io.StreamCorruptedException: invalid stream header: 30313031
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:863)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:355)
at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:309)
at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:299)
at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:218)
... 147 more
任何帮助,建议,指示将不胜感激。
哦,顺便说一句,我确实尝试过使用
com.vividsolutions.jts.geom.Geometry;
或
org.geolatte.geom.Point;
作为替代方案,但没有任何成功,我仍然得到同样的错误。
答案 0 :(得分:2)
当我从spring-boot-2.1.x-> spring-boot-2.2.x迁移spring-boot应用程序时,抛出了相同的异常
在spring-boot-2.2.x中,休眠版本已更改为5.3.x-> 5.4.x,hibernate-5.4 migration guide表示休眠空间依赖的JTS也已迁移,这也暗示了更改在软件包命名中,即com.vividsolutions.jts.*
已更改为org.locationtech.jts.*
。
在我的特殊情况下,从数据库中获取的DAO实体使用以下导入
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.PrecisionModel;
我必须更改为
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.PrecisionModel;
从
更改h2gis
依赖项的版本之后,这是有可能的
<dependency>
<groupId>org.orbisgis</groupId>
<artifactId>h2gis</artifactId>
<version>1.4.0</version>
</dependency>
到
<dependency>
<groupId>org.orbisgis</groupId>
<artifactId>h2gis</artifactId>
<version>1.5.0</version>
</dependency>
答案 1 :(得分:2)
我遇到了同样的问题,并在application.properties的下面添加了行,然后它起作用了。
spring.jpa.database-platform=org.hibernate.spatial.dialect.postgis.PostgisDialect
您还可以在此处看到类似的问题:How to deserialize / serialize type Geometry in spring boot?
答案 2 :(得分:1)
30313031
是十六进制。这不是序列化数据,而是ASCII文本。你没有序列化它,所以你也不能反序列化它。
答案 3 :(得分:1)
原因之一可能是您没有包括 spring-boot-starter-data-jpa 的所有相关依赖项,请确保对于以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
您已包括以下依赖项:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-digester3</artifactId>
<version>3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-java8 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
答案 4 :(得分:0)
好吧,看起来我还没有完成我的功课。 : - $ 击>
这确实是一个序列化问题。
只需更改
public class SCoordinates implements Serializable {
private static final long serialVersionUID = 1L;
成功了。
: - $太快就对那个人大喊大叫。 它不会那么简单......
感谢指点
答案 5 :(得分:0)
这是一个依赖地狱的问题,类加载器会回复误导信息。
感谢
的一些帖子https://stackoverflow.com/users/1126380/arturo-volpe
和
https://stackoverflow.com/users/4492890/maverick-java
我终于找到了出路 看看他们的帖子,他们帮了很多忙。
顺便说一句,如果我感兴趣的是我的新gradle构建能够摆脱这种地狱般的感觉
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '1.5.9.RELEASE' // Yeah I changed Spring version too ...
id 'net.ltgt.apt' version '0.14'
}
description="TILES project"
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal() // I had to put the oracle jar in there
mavenCentral()
jcenter()
}
dependencies {
implementation('dom4j:dom4j:1.6') {
force = true
}
implementation('org.postgresql:postgresql:42.2.1') {
force = true
}
implementation 'ch.qos.logback:logback-classic:1.1.11',
'org.slf4j:slf4j-api:1.7.25',
'org.slf4j:log4j-over-slf4j:1.7.2',
'org.springframework.boot:spring-boot-starter-data-jpa:1.5.1.RELEASE',
'org.springframework.boot:spring-boot-starter-actuator',
'org.hibernate.common:hibernate-commons-annotations:5.0.2.Final',
'org.apache.commons:commons-dbcp2:2.2.0',
'com.vividsolutions:jts-core:1.14.0'
implementation(
'org.springframework.boot:spring-boot-starter-thymeleaf:1.4.5.RELEASE'
) {
exclude module: 'org.springframework.boot:spring-boot-starter-tomcat'
}
implementation ('org.springframework.boot:spring-boot-starter-web') {
exclude module: 'org.springframework.boot:spring-boot-starter-tomcat'
}
implementation ('org.hibernate:hibernate-core:5.1.10.Final') {
exclude module: 'dom4j:dom4j'
}
implementation ('org.hibernate:hibernate-spatial:5.0.12.Final') {
exclude module: 'postgresql'
}
providedCompile 'org.springframework.boot:spring-boot-starter-tomcat',
'org.postgresql:postgresql',
'javax.servlet:javax.servlet-api',
'dom4j:dom4j',
'javax.servlet:jstl'
}
希望这可以帮助任何人...
答案 6 :(得分:0)
我今天遇到过类似的问题。我也得到了"java.io.StreamCorruptedException: invalid stream header: 54656D70"
,但就我而言,它是String变量。我花了很多时间来弄清楚IntelliJ IDEA导入的类:
import com.sun.org.apache.xpath.internal.operations.String;
而不是普通java.lang.String
,这就是为什么应用程序无法反序列化它。
希望它会对某人有所帮助。
答案 7 :(得分:0)
您是否已将hibernate-spatial
依赖项添加到项目中,并且已将PostgisPG95Dialect
指定为休眠方言(在Spring Boot中,它是spring.jpa.database-platform
属性)?应该会有帮助。