Using @JsonComponent in Spring Boot – 在Spring Boot中使用@JsonComponent

最后修改: 2017年 4月 13日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

这篇快速文章主要介绍如何在Spring Boot中使用@JsonComponent注解。

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.

该注解允许我们将一个注解类暴露为Jackson的序列化器和/或反序列化器,而不需要手动将其添加到ObjectMapper

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

这是Spring Boot核心模块的一部分,所以在普通的Spring Boot应用程序中不需要额外的依赖。

2. Serialization

2.序列化

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

让我们从下面的User对象开始,该对象包含一个喜欢的颜色。

public class User {
    private Color favoriteColor;

    // standard getters/constructors
}

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

如果我们用Jackson的默认设置来序列化这个对象,我们会得到。

{
  "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.

我们可以通过直接打印RGB值来使JSON变得更加凝练和可读–例如,在CSS中使用。

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

在这种情况下,我们只需要创建一个实现JsonSerializer的类。

@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:

有了这个序列化器,产生的JSON已经被减少到。

{"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:

由于@JsonComponent注解,序列化器被注册在Spring Boot应用程序的Jackson ObjectMapper中。我们可以通过以下JUnit测试来测试。

@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

3.反序列化

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

继续同一个例子,我们可以写一个反序列化器,将网页颜色String变成一个JavaFX颜色对象。

@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:

让我们测试一下新的反序列化器,确保一切按预期工作。

@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

4.序列化器反序列化器在一个类中

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:

当需要时,我们可以通过使用两个内部类并在包围类上添加@JsonComponent,在一个类中连接串行器和反串行器。

@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

5.结论

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.

这个快速教程展示了如何通过利用带有@JsonComponent注解的组件扫描,在Spring Boot应用程序中快速添加Jackson序列化器/解串器。

The code snippets can be found over on GitHub.

这些代码片段可以在GitHub上找到over