8

在使用 Micronaut 时,我尝试将 jackson 配置为使用 SNAKE_CASE 接受 JSON,但是,它无法识别 property jackson.property-naming-strategy: SNAKE_CASE

4

2 回答 2

11

从 Micronaut 1.1.1

应用程序.yml

jackson:
    property-naming-strategy: SNAKE_CASE

在 Micronaut 1.1.1 之前

MicronautObjectMapper使用ObjectMapperFactory不设置属性命名策略的构造(至少在 Micronaut 1.0 GA 版本中,这可能会在未来的版本中改变)。配置类不支持您在问题中提到的配置选项,因此使用它根本没有任何作用。但是,您可以ObjectMapperFactory用您自己的自定义实现替换类,该实现ObjectMapper以默认方式构建 + 它设置属性命名策略。考虑以下示例:

package com.github.wololock.micronaut;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Replaces;
import io.micronaut.jackson.JacksonConfiguration;
import io.micronaut.jackson.ObjectMapperFactory;
import io.micronaut.runtime.Micronaut;

import javax.inject.Singleton;
import java.util.Optional;

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }

    @Factory
    @Replaces(ObjectMapperFactory.class)
    static class CustomObjectMapperFactory extends ObjectMapperFactory {

        @Override
        @Singleton
        @Replaces(ObjectMapper.class)
        public ObjectMapper objectMapper(Optional<JacksonConfiguration> jacksonConfiguration, Optional<JsonFactory> jsonFactory) {
            final ObjectMapper mapper = super.objectMapper(jacksonConfiguration, jsonFactory);
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            return mapper;
        }
    }
}

CustomObjectMapperFactory在这个例子中,我在主类中添加了一个静态类,Application并使用@Replaces注解来指示 Micronaut 使用这个工厂类和objectMapper()这个自定义工厂类提供的方法。我们从这个工厂返回的ObjectMapper实例是基于默认的工厂方法 + 它添加:

mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

设置预期的属性命名策略。

这是添加此自定义工厂类后我在响应中返回的示例输出:

HTTP/1.1 200 OK
Date: Wed, 7 Nov 2018 19:15:10 GMT
connection: keep-alive
content-length: 38
content-type: application/json

{
    "first_name": "Joe",
    "last_name": "Doe"
}

默认情况下(没有此自定义工厂类)响应如下所示:

HTTP/1.1 200 OK
Date: Wed, 7 Nov 2018 19:04:14 GMT
connection: keep-alive
content-length: 36
content-type: application/json

{
    "firstName": "Joe",
    "lastName": "Doe"
}

更新:BeanCreatedEventListener<ObjectMapper>改为使用

有一种替代方法可以实现相同的效果,需要更少的代码行。归功于Micronaut框架 Twitter 帐户:)

我们可以使用BeanCreatedEventListener<T>它来响应 bean 创建事件并允许我们扩展刚刚创建的 bean。在这种情况下,这意味着添加一个实现BeanCreatedEventListener<ObjectMapper>和设置属性命名策略的类:

package com.github.wololock.micronaut;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.runtime.Micronaut;

import javax.inject.Singleton;

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }

    @Singleton
    static class ObjectMapperBeanEventListener implements BeanCreatedEventListener<ObjectMapper> {

        @Override
        public ObjectMapper onCreated(BeanCreatedEvent<ObjectMapper> event) {
            final ObjectMapper mapper = event.getBean();
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            return mapper;
        }
    }
}

该解决方案的优点:

  • 更少的代码行
  • 减少一个抽象层(我们不必费心ObjectMapperFactory,我们只关心配置现有的ObjectMapperbean)。
于 2018-11-07T18:21:30.673 回答
7

这在本期 Micronaut 1.1.1 中已修复:https ://github.com/micronaut-projects/micronaut-core/issues/1599

将此添加到您的application.yml

jackson:
    property-naming-strategy: SNAKE_CASE

您可以使用以下方法对其进行测试:

    @Test
    fun testJackson() {
        val applicationContext = ApplicationContext.run()

        assertThat(applicationContext.getBean(JacksonConfiguration::class.java).propertyNamingStrategy).isEqualTo(PropertyNamingStrategy.SNAKE_CASE)
    }
于 2019-05-14T18:59:36.560 回答