0

I have two classes Promotion and PromotionZona and hava this error.

Here is my json request which I want to save in the database.

"promotionZona":{"localidad":{"latitud":0.0,"longitud":0.0},"provincia":{},"todoElPais":true,"promotion":0,"promoZonaId":0}

You can see promotion, but I can't save it to my database, I've got such an error:

2020-10-25 20:06:50.113  INFO 5550 --- [           main] o.s.s.quartz.SchedulerFactoryBean        : Starting Quartz Scheduler now
2020-10-25 20:06:50.113  INFO 5550 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2020-10-25 20:06:50.133  INFO 5550 --- [           main] a.c.b.s.s.SuipBackofficeApplication      : Started SuipBackofficeApplication in 31.486 seconds (JVM running for 57.573)
2020-10-25 20:06:50.323  INFO 5550 --- [           main] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2020-10-25 20:06:50.419  INFO 5550 --- [           main] org.apache.coyote.ajp.AjpNioProtocol     : Starting ProtocolHandler ["ajp-nio-8109"]
2020-10-25 20:06:50.430  INFO 5550 --- [           main] org.apache.catalina.startup.Catalina     : Server startup in [47,370] milliseconds
2020-10-25 20:07:21.325  INFO 5550 --- [nio-8180-exec-3] o.a.c.c.C.[.[.[/suip-backoffice]         : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-10-25 20:07:21.327  INFO 5550 --- [nio-8180-exec-3] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-10-25 20:07:21.413  INFO 5550 --- [nio-8180-exec-3] o.s.web.servlet.DispatcherServlet        : Completed initialization in 86 ms
2020-10-25 20:07:21.759  WARN 5550 --- [nio-8180-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `us.com.model.Promocion` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (0); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `us.com.model.suipportalmodel.model.Promotion` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (0)
 at [Source: (PushbackInputStream); line: 1, column: 2650] (through reference chain: ar.com.model.suipportalmodel.dto.PromotionDTO["promotionZona"]->java.lang.Object[][0]->.PromotionZona["promotion"])]

My Entity PromotionZona:

@Table(name="promotion_zona")
public class PromotionZona implements Serializable {


   @Id 
   @GeneratedValue(strategy = GenerationType.IDENTITY)//9
   @Column(name = "id_promotion_zona", unique = true, nullable= false)
   private Long idPromotionZona;
   
   
   @Column(name="todo_el_pais")
   private Boolean todoElPais;
   
   @Column(name="fecha_desde_solicitada")
   private Date fechaDesdeSolicitada;
   
   @Column(name="fecha_hasta_solicitada")
   private Date fechaHastaSolicitada;
   
   @Column(name="fecha_desde_confirmada")
   private Date fechaDesdeConfirmada;
   
   @Column(name="fecha_hasta_confirmada")
   private Date fechaHastaConfirmada;
   
   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinColumn(name="id_localidad",nullable=false)
   private Localidad localidad;
   
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "id_provincia", nullable = true)
   private Provincia provincia;
   
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name="id_promocion",nullable=false)
   private Promotion promotion; ```
3
  • 1
    Not sure if related, but you have Promocion in your entity and promotion in your request.
    – Fullslack
    Commented Oct 25, 2020 at 23:31
  • It is not relevant, I change now, but the same error
    – Gonzalo
    Commented Oct 26, 2020 at 0:43
  • You’re json isn’t actually valid json. You’ll need to wrap it in {} to make it valid. Commented Oct 26, 2020 at 2:44

1 Answer 1

6

The error happens when spring mvc fails to deserialize json data to your class Promotion, to be specific, it fails at "promotion":0 -> instance of class Promotion, since Promotion is a class, it must look something like this:

public class Promotion {

    private int field1;
    
    //getters and setters
}

then in your json data, it should be:

"promotion":{"filed1": 0}

So there are several ways to solve this problem:

  1. apprently you can change your request to:
"promotionZona":{"localidad":{"latitud":0.0,"longitud":0.0},"provincia":{},"todoElPais":true,"promotion":{"field1":0},"promoZonaId":0}

  1. add an int-argument constructor to the class Promotion:
public class Promotion {

    private int field1;

    public Promotion(int field1) {
        this.field1 = field1;
    }
    
    //getters and setters
}
  1. add a custom JsonDeserializer:
public class PromotionJsonDeserializer extends JsonDeserializer<Promotion> {
    @Override
    public Promotion deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        if(jsonParser == null)return null;

        Promotion promotion = new Promotion();
        promotion.setField1(Integer.valueOf(jsonParser.getText()));
        return promotion;
    }
}

then in PromotionZona, annotate the field promotion with PromotionJsonDeserializer:


@Table(name="promotion_zona")
public class PromotionZona implements Serializable {

   ...
 
   @JsonDeserialize(using = PromotionJsonDeserializer.class)
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name="id_promocion",nullable=false)
   private Promotion promotion;

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.