Image of How to solve org.codehaus.jackson.map.JsonMappingException: Unrecognized field

ADVERTISEMENT

Table of Contents

Introduction

Jackson library is used with most REST/JSON applications in order to dynamically convert JSON to POJO and POJO to JSON. This tutorial explains how to solve the common exception raised by Jackson while doing the JSON/POJO conversion: org.codehaus.jackson.map.JsonMappingException: Unrecognized field

This exception occurs when Jackson tries to map a JSON attribute to a POJO field and couldn’t find a setter method for this attribute or a copy constructor.

Here below we explain the solutions to this issue.

1- Copy constructor/ Setter method

The first thing to check when facing such an issue is to make sure that the unrecognized field is set through either a copy constructor or a setter method.

If the field neither exists in a copy constructor nor it has a setter method, then Jackson won’t be able to map it hence throws an exception.

2- Double check the name of POJO field

If you’re sure that you set the field correctly in the POJO, then double check the name of the field. It should exactly match the name of the JSON attribute.

If you set the field through a setter method, then the name of your setter method should be like set(). (Knowing that the field name is CamelCase).

If you set the field through a copy constructor, then the name of the parameter should match the name of the JSON attribute.

Summary

Jackson library is used with most REST/JSON applications in order to dynamically convert JSON to POJO and POJO to JSON. This tutorial explains how to solve the common exception raised by Jackson while doing the JSON/POJO conversion: org.codehaus.jackson.map.JsonMappingException: Unrecognized field

Next Steps

If you're interested in learning more about the basics of Java, coding, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.

Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to jacob@initialcommit.io.

Final Notes