Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

jackson
java
annotation
communitycreator
disable()

How to disable all Jackson annotations in Java

abhilash

Overview

When working with the Jackson framework, there might be a need to disable all the annotations used from the framework. This shot explores how to disable all the Jackson annotations.


To use the Jackson library, add the Jackson databind dependency from the Maven Repository.

How to use the disable() method

The disable() method of the JsonMapper class disables a list of features.

Parameters

The method takes in the different features to disable as parameters. We use the USE_ANNOTATIONS of the MapperFeature enumenumeration - a special “class” that represents a group of constants (unchangeable variables, like final variables). to disable all the annotations used.


ObjectMapper objectMapper = JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build();

In the code above, we build an object of the ObjectMapper class with all the annotations disabled.

Code

In the code below, we have used the JsonPropertyOrder annotation to order the elements of JSON in particular.

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

public class Main{

    @JsonPropertyOrder({"firstName", "middleName", "lastName"})
    static class Student{

        private int id;
        private int age;
        private String firstName;
        private String middleName;
        private String lastName;

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getMiddleName() {
            return middleName;
        }

        public void setMiddleName(String middleName) {
            this.middleName = middleName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student();
        student.setFirstName("firstName");
        student.setMiddleName("middleName");
        student.setLastName("lastName");
        student.setId(123);
        student.setAge(20);

        ObjectMapper objectMapper = JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build();
        String studentJson = objectMapper.writeValueAsString(student);
        System.out.println(studentJson);
    }
}

Explanation

We use the disable() method while we create the ObjectMapper object to disable all the annotations used. The output of the code is as follows.


{"id":123,"age":20,"firstName":"firstName","middleName":"middleName","lastName":"lastName"}

We can see that the JsonPropertyOrder annotation is not applied based on the output above.

If we remove the disable method and run the code, the output will be as follows.


{"firstName":"firstName","middleName":"middleName","lastName":"lastName","id":123,"age":20}

RELATED TAGS

jackson
java
annotation
communitycreator
disable()
RELATED COURSES

View all Courses

Keep Exploring