Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Jackson deserialization error: MismatchedInputException

General Tech Bugs & Fixes

Max. 2000 characters
Replies

usr_profile.png

User

( 6 months ago )

 

I have the following class

public class Cart {
    private final String id;

    public Cart(String id) { this.id = id;}

    public String getId() { return id; }
}

And the following test:

    String jsonString = "{\"id\":\"56c7b5f7-115b-4cb9-9658-acb7b849d5d5\"}";
    Cart cart = mapper.readValue(jsonString, Cart.class);
    assertThat(cart.getId()).isEqualTo("56c7b5f7-115b-4cb9-9658-acb7b849d5d5");

And I'm getting the following error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofcom.store.domain.model.Cart (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"id":"56c7b5f7-115b-4cb9-9658-acb7b849d5d5"}"; line: 1, column: 2]

I can't figure out what's wrong here. Any help please?

usr_profile.png

User

( 6 months ago )

You need to define your bean as:

public class Cart {
    private final String id;

    @JsonCreator
    public Cart(@JsonProperty("id") String id) { this.id = id;}

    public String getId() { return id; }
}

public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    String jsonString = "{\"id\":\"56c7b5f7-115b-4cb9-9658-acb7b849d5d5\"}";
    Cart cart = mapper.readValue(jsonString, Cart.class);
    System.out.println(mapper.writeValueAsString(cart));
}

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif