无法读取文档:无法从 START_OBJECT 令牌中反序列化 java.lang.String 的实例
Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token
我正在通过 POSTMAN 调用我的 POST 方法,该方法应该是 return String 类型,但是当我进行调用时却出现错误"Could not read document: Can not deserialize instance of java. lang.String out of START_OBJECT token"
。为什么会这样?
实体中的属性:
@Column(name = "CIF_NIF")
@JsonView(Views.Buscador.class)
@JsonProperty("cifNif")
private String cifNif;
RestController:
@PostMapping(value = "/recuperaNombreLegalCliente")
public String recuperaNombreLegalCliente(@RequestBody String cifNif) {
return contratoService.getNombreLegalCliente(cifNif);
}
存储库
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif= ?1")
public String getNombreLegalCliente(String cifNif);
我调用方法时的输入:
{
"cifNif": "E85882355"
}
现在编辑 1 个堆栈跟踪错误(@sigur 回答后)
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [Contrato [id=null, tipoSolicitud=null, tipoContratoDiscriminator=null, cifNif=E85882355] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [Contrato [id=null, tipoSolicitud=null, tipoContratoDiscriminator=null, cifNif=E85882355] did not match expected type [java.lang.String (n/a)]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
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.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy174.getNombreLegalCliente(Unknown Source)
at com.xxx.xxxx.servicio.ContratoService.getNombreLegalCliente(ContratoService.java:24)
at com.xxxx.xxxx.acciones.rest.SolicitudAutorizacionPreciosRestController.recuperaNombreLegalCliente(SolicitudAutorizacionPreciosRestController.java:56)
@RequestBody
的映射器正在尝试读取您的输入并将其数据转换为所请求的 type/object。
在您的情况下,它试图将您的 json 文本转换为 String
。默认情况下不允许这样做,您应该更改 POJO class 中的输入,它反映了 json 输入的模型。
将 public String recuperaNombreLegalCliente(@RequestBody String cifNif)
更改为 String recuperaNombreLegalCliente(@RequestBody Cliente cifNif)
,其中 Cliente
是 class,例如:
class Cliente {
private String cifNif;
public void setCifNif(String cifNif) {this.cifNif = cifNif;}
public String getCifNif() {return cifNif;}
}
案例编辑 1 -> org.springframework.dao.InvalidDataAccessApiUsageException
该错误与在您的存储库中生成查询有关。当您将 String
更改为 Cliente
class 时,您还需要充分调用您的存储库或查询;因为现在您正在尝试将 Cliente
实例作为参数映射到您对字符串 field/column.
的查询中
如果要使用 Cliente
,则需要在查询中使用 cifNif
属性。示例:
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif=cliente.cifNif")
public String getNombreLegalCliente(@Param("cliente") Cliente cliente);
...
或者,如果您想使用字符串值,只需从您的控制器调用存储库函数:
String recuperaNombreLegalCliente(@RequestBody Cliente cifNif) {
repository.getNombreLegalCliente(cifNif.getCifNif());
...
}
存储库方法是:
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif= ?1")
public String getNombreLegalCliente(String cifNif);
...
我正在通过 POSTMAN 调用我的 POST 方法,该方法应该是 return String 类型,但是当我进行调用时却出现错误"Could not read document: Can not deserialize instance of java. lang.String out of START_OBJECT token"
。为什么会这样?
实体中的属性:
@Column(name = "CIF_NIF")
@JsonView(Views.Buscador.class)
@JsonProperty("cifNif")
private String cifNif;
RestController:
@PostMapping(value = "/recuperaNombreLegalCliente")
public String recuperaNombreLegalCliente(@RequestBody String cifNif) {
return contratoService.getNombreLegalCliente(cifNif);
}
存储库
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif= ?1")
public String getNombreLegalCliente(String cifNif);
我调用方法时的输入:
{
"cifNif": "E85882355"
}
现在编辑 1 个堆栈跟踪错误(@sigur 回答后)
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [Contrato [id=null, tipoSolicitud=null, tipoContratoDiscriminator=null, cifNif=E85882355] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [Contrato [id=null, tipoSolicitud=null, tipoContratoDiscriminator=null, cifNif=E85882355] did not match expected type [java.lang.String (n/a)]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
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.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy174.getNombreLegalCliente(Unknown Source)
at com.xxx.xxxx.servicio.ContratoService.getNombreLegalCliente(ContratoService.java:24)
at com.xxxx.xxxx.acciones.rest.SolicitudAutorizacionPreciosRestController.recuperaNombreLegalCliente(SolicitudAutorizacionPreciosRestController.java:56)
@RequestBody
的映射器正在尝试读取您的输入并将其数据转换为所请求的 type/object。
在您的情况下,它试图将您的 json 文本转换为 String
。默认情况下不允许这样做,您应该更改 POJO class 中的输入,它反映了 json 输入的模型。
将 public String recuperaNombreLegalCliente(@RequestBody String cifNif)
更改为 String recuperaNombreLegalCliente(@RequestBody Cliente cifNif)
,其中 Cliente
是 class,例如:
class Cliente {
private String cifNif;
public void setCifNif(String cifNif) {this.cifNif = cifNif;}
public String getCifNif() {return cifNif;}
}
案例编辑 1 -> org.springframework.dao.InvalidDataAccessApiUsageException
该错误与在您的存储库中生成查询有关。当您将 String
更改为 Cliente
class 时,您还需要充分调用您的存储库或查询;因为现在您正在尝试将 Cliente
实例作为参数映射到您对字符串 field/column.
如果要使用 Cliente
,则需要在查询中使用 cifNif
属性。示例:
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif=cliente.cifNif")
public String getNombreLegalCliente(@Param("cliente") Cliente cliente);
...
或者,如果您想使用字符串值,只需从您的控制器调用存储库函数:
String recuperaNombreLegalCliente(@RequestBody Cliente cifNif) {
repository.getNombreLegalCliente(cifNif.getCifNif());
...
}
存储库方法是:
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif= ?1")
public String getNombreLegalCliente(String cifNif);
...