Using @JsonComponent in Spring Boot

https://www.baeldung.com/spring-boot-jsoncomponent

 

1. Overview

This quick article is focused on how to use the @JsonComponent annotation in Spring Boot.

The annotation allows us to expose an annotated class to be a Jackson serializer and/or deserializer without the need to add it to the ObjectMapper manually.

This is part of the core Spring Boot module, so there are no additional dependencies required in a plain Spring Boot application.

2. Serialization

Let’s start with the following User object containing a favorite color:

1

2

3

4

5

public class User {

    private Color favoriteColor;

 

    // standard getters/constructors

}

If we serialize this object using Jackson with default settings we get:

{
  "favoriteColor": {
    "red": 0.9411764740943909,
    "green": 0.9725490212440491,
    "blue": 1.0,
    "opacity": 1.0,
    "opaque": true,
    "hue": 208.00000000000003,
    "saturation": 0.05882352590560913,
    "brightness": 1.0
  }
}

We can make the JSON a lot more condensed and readable by just printing the RGB values – for example, to be used in CSS.

To this extent, we just have to create a class that implements JsonSerializer:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@JsonComponent

public class UserJsonSerializer extends JsonSerializer<User> {

 

    @Override

    public void serialize(User user, JsonGenerator jsonGenerator,

      SerializerProvider serializerProvider) throws IOException,

      JsonProcessingException {

  

        jsonGenerator.writeStartObject();

        jsonGenerator.writeStringField(

          "favoriteColor",

          getColorAsWebColor(user.getFavoriteColor()));

        jsonGenerator.writeEndObject();

    }

 

    private static String getColorAsWebColor(Color color) {

        int r = (int) Math.round(color.getRed() * 255.0);

        int g = (int) Math.round(color.getGreen() * 255.0);

        int b = (int) Math.round(color.getBlue() * 255.0);

        return String.format("#%02x%02x%02x", r, g, b);

    }

}

With this serializer, the resulting JSON has been reduced to:

{"favoriteColor":"#f0f8ff"}

Due to the @JsonComponent annotation, the serializer is registered in the Jackson ObjectMapper in the Spring Boot application. We can test this with the following JUnit test:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@JsonTest

@RunWith(SpringRunner.class)

public class UserJsonSerializerTest {

 

    @Autowired

    private ObjectMapper objectMapper;

 

    @Test

    public void testSerialization() throws JsonProcessingException {

        User user = new User(Color.ALICEBLUE);

        String json = objectMapper.writeValueAsString(user);

  

        assertEquals("{\"favoriteColor\":\"#f0f8ff\"}", json);

    }

}

3. Deserialization

Continuing with the same example, we can write a deserializer that will turn the web color String into a JavaFX Color object:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@JsonComponent

public class UserJsonDeserializer extends JsonDeserializer<User> {

  

    @Override

    public User deserialize(JsonParser jsonParser,

      DeserializationContext deserializationContext) throws IOException,

      JsonProcessingException {

  

        TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);

        TextNode favoriteColor

          = (TextNode) treeNode.get("favoriteColor");

        return new User(Color.web(favoriteColor.asText()));

    }

}

Let’s test the new deserializer and make sure everything works as expected:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@JsonTest

@RunWith(SpringRunner.class)

public class UserJsonDeserializerTest {

 

    @Autowired

    private ObjectMapper objectMapper;

 

    @Test

    public void testDeserialize() throws IOException {

        String json = "{\"favoriteColor\":\"#f0f8ff\"}"

        User user = objectMapper.readValue(json, User.class);

  

        assertEquals(Color.ALICEBLUE, user.getFavoriteColor());

    }

}

4. Serializer and Deserializer in one Class

When desired, we can connect the serializer and the deserializer in one class by using two inner classes and adding the @JsonComponent on the enclosing class:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

@JsonComponent

public class UserCombinedSerializer {

  

    public static class UserJsonSerializer

      extends JsonSerializer<User> {

 

        @Override

        public void serialize(User user, JsonGenerator jsonGenerator,

          SerializerProvider serializerProvider) throws IOException,

          JsonProcessingException {

  

            jsonGenerator.writeStartObject();

            jsonGenerator.writeStringField(

              "favoriteColor", getColorAsWebColor(user.getFavoriteColor()));

            jsonGenerator.writeEndObject();

        }

 

        private static String getColorAsWebColor(Color color) {

            int r = (int) Math.round(color.getRed() * 255.0);

            int g = (int) Math.round(color.getGreen() * 255.0);

            int b = (int) Math.round(color.getBlue() * 255.0);

            return String.format("#%02x%02x%02x", r, g, b);

        }

    }

 

    public static class UserJsonDeserializer

      extends JsonDeserializer<User> {

  

        @Override

        public User deserialize(JsonParser jsonParser,

          DeserializationContext deserializationContext)

          throws IOException, JsonProcessingException {

  

            TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);

            TextNode favoriteColor = (TextNode) treeNode.get(

              "favoriteColor");

            return new User(Color.web(favoriteColor.asText()));

        }

    }

}

5. Conclusion

This quick tutorial showed how to quickly add a Jackson serializer/deserializer in a Spring Boot application by leveraging component scanning with the @JsonComponent annotation.

The code snippets can be found over on GitHub.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值