Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot deserialize from Object value (no delegate- or property-based Creator) using Jackson

I'm trying to deserialise below JSON payload with Jackson:

{"code":null,"reason":"subscription yet available","message":"{ Message:\"subscription yet available\", SubscriptionUID:\"46b62920-c519-4555-8973-3b28a7a29463\" }"}

but I'm getting this JsonMappingException:

Cannot construct instance of `com.ids.utilities.DeserializeSubscription` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{"code":null,"reason":"subscription yet available","message":"{ Message:\"subscription yet available\", SubscriptionUID:\"46b62920-c519-4555-8973-3b28a7a29463\" }"}"; line: 1, column: 2]

I've created two classes. The first class:

import lombok.Data;

@Data
public class DeserializeSubscription {

    private String code;
    private String reason;
    private MessageSubscription message;


    public DeserializeSubscription(String code, String reason, MessageSubscription message) {
        super();
        this.code = code;
        this.reason = reason;
        this.message = message;
    }

and the second class

import lombok.Data;

@Data
public class MessageSubscription {

    private String message;
    private String subscriptionUID;


    public MessageSubscription(String message, String subscriptionUID) {
        super();
        this.message = message;
        this.subscriptionUID = subscriptionUID;
    }

In the main class:

                 try 
                 {

                    ObjectMapper mapper = new ObjectMapper();
                    mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
                    DeserializeSubscription desSub=null;

                    desSub=mapper.readValue(e.getResponseBody(), DeserializeSubscription.class);

                    System.out.println(desSub.getMessage().getSubscriptionUID());
                 }
                 catch (JsonParseException e1) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                 }
                 catch (JsonMappingException e1) {
                     System.out.println(e1.getMessage());
                        e.printStackTrace();
                 }
                 catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                 }

I've found this solution but I didn't work it https://facingissuesonit.com/2019/07/17/com-fasterxml-jackson-databind-exc-invaliddefinitionexception-cannot-construct-instance-of-xyz-no-creators-like-default-construct-exist-cannot-deserialize-from-object-value-no-delega/

The jackson maven I'm using in my application

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.10.2</version>
    </dependency>
like image 825
Scripta14 Avatar asked Dec 14 '22 09:12

Scripta14


1 Answers

The message is pretty clear: (no Creators, like default construct, exist)

you need to add a no argument constructor to the class or the NoArgsConstructor annotation:

@Data
public class DeserializeSubscription {
  public DeserializeSubscription (){}

or

@NoArgsConstructor
@Data
public class DeserializeSubscription {
like image 116
Jens Avatar answered Jan 01 '23 04:01

Jens