\n
case edit 1 -> org.springframework.dao.InvalidDataAccessApiUsageException
\nThe error is related to the query generation into your repository. When you changed your String
into the Cliente
class, you need also to adequate your call for your repository, or the query; because now you are trying to map a Cliente
instance as a parameter into your query for a string field/column.
If you want to use the Cliente
you need to use the cifNif
property into your query. Example:
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif=cliente.cifNif")\npublic String getNombreLegalCliente(@Param("cliente") Cliente cliente);\n...\n
\nOr if you want to use the string value just callthe repository function from your controller:
\nString recuperaNombreLegalCliente(@RequestBody Cliente cifNif) {\n repository.getNombreLegalCliente(cifNif.getCifNif());\n...\n}\n
\nwhere the repository method is:
\n@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif= ?1")\npublic String getNombreLegalCliente(String cifNif);\n...\n
\n","author":{"@type":"Person","name":"sigur"},"upvoteCount":3}}}Reputation: 135
I'm doing a call via POSTMAN to my POST method that should return a String type, but when I'm doing the call it's giving me the error "Could not read document: Can not deserialize instance of java. lang.String out of START_OBJECT token"
. Why is this happening?
Attribute in the entity:
@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);
}
Repository
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif= ?1")
public String getNombreLegalCliente(String cifNif);
The input when I'm doing the method call:
{
"cifNif": "E85882355"
}
EDIT 1 Stack trace error now (after @sigur answer)
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)
Upvotes: 0
Views: 1089
Reputation: 690
The mapper for the @RequestBody
is trying to read your input and transform its data for the requested type/object.
In your case it's trying to transform your json text into String
. This is not allowed by default, you should change your input in POJO class which reflects the model of the json input.
Change public String recuperaNombreLegalCliente(@RequestBody String cifNif)
to something like String recuperaNombreLegalCliente(@RequestBody Cliente cifNif)
, where Cliente
is a class like:
class Cliente {
private String cifNif;
public void setCifNif(String cifNif) {this.cifNif = cifNif;}
public String getCifNif() {return cifNif;}
}
case edit 1 -> org.springframework.dao.InvalidDataAccessApiUsageException
The error is related to the query generation into your repository. When you changed your String
into the Cliente
class, you need also to adequate your call for your repository, or the query; because now you are trying to map a Cliente
instance as a parameter into your query for a string field/column.
If you want to use the Cliente
you need to use the cifNif
property into your query. Example:
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif=cliente.cifNif")
public String getNombreLegalCliente(@Param("cliente") Cliente cliente);
...
Or if you want to use the string value just callthe repository function from your controller:
String recuperaNombreLegalCliente(@RequestBody Cliente cifNif) {
repository.getNombreLegalCliente(cifNif.getCifNif());
...
}
where the repository method is:
@Query(value="SELECT c.nombreSociedadPersona FROM Contrato c WHERE c.cifNif= ?1")
public String getNombreLegalCliente(String cifNif);
...
Upvotes: 3