简体   繁体   English

为什么 Spring 引导不使用自定义 ObjectMapper bean?

[英]Why Spring Boot not using custom ObjectMapper bean?

I have custom ObjectMapper bean annotated as @Primary.我有自定义 ObjectMapper bean 注释为@Primary。

@Configuration
public class BeanConfig {

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new Jdk8Module());
        objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
        return objectMapper;
    }

}

However when I try to parse form with not same case written enum it gives me error.但是,当我尝试使用不同大小写的枚举解析表单时,它给了我错误。

@RestController
@RequestMapping("/game")
public class GameController {

    private final GameService gameService;

    public GameController(GameService gameService) {
        this.gameService = gameService;
    }

    @PostMapping("/create")
    public ResponseEntity<?> createGame(
            @AuthenticationPrincipal String id,
            GameParametersForm gameParametersForm
    ) {
        Result<String> result = gameService.createWebGameSession(id, gameParametersForm);
        return result.toResponseEntity();
    }

}
public class GameParametersForm {

    private Mode mode;

    public GameParametersForm(
            Mode mode
    ) {
        this.mode = mode;
    }

    public Mode getMode() {
        return mode;
    }

}

Here is build.gradle这是 build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.fasterxml.jackson.core:jackson-annotations:2.12.2'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.2'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.12.2'
    implementation 'javax.validation:validation-api:2.0.1.Final'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-websocket'
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.security:spring-security-test'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

I have tried to declare Jackson2ObjectMapperBuilder bean.我试图声明 Jackson2ObjectMapperBuilder bean。 It didn't help.它没有帮助。
Any idea what might be happening, or where else I should be looking to track down the issue?知道可能会发生什么,或者我应该在哪里寻找问题?

  1. Create a Jackson2ObjectMapperBuilderCustomizer bean, eg.创建一个Jackson2ObjectMapperBuilderCustomizer bean,例如。 hantsy/spring-r2dbc-sample DemoApplication.java#L152 hantsy/spring-r2dbc-sample DemoApplication.java#L152
     @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> { builder.serializationInclusion(JsonInclude.Include.NON_EMPTY); builder.featuresToDisable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, SerializationFeature.FAIL_ON_EMPTY_BEANS, DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); builder.featuresToEnable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); }; }
  2. Or customize it in spring boot application properties.或者在 spring 启动应用程序属性中对其进行自定义。 eg.例如。 hantsy/spring-webmvc-jwt-sample application.yml#L4 hantsy/spring-webmvc-jwt-sample application.yml#L4
     spring: jackson: mapper: DEFAULT_VIEW_INCLUSION: true serialization: indent_output: true WRITE_DATES_AS_TIMESTAMPS: false deserialization: FAIL_ON_IGNORED_PROPERTIES: false FAIL_ON_UNKNOWN_PROPERTIES: false ACCEPT_SINGLE_VALUE_AS_ARRAY: true default-property-inclusion: non_empty

Check that your letting spring boot auto configure your application.检查您是否让 spring 引导自动配置您的应用程序。 Less is more in your spring boot application configuration. spring 引导应用程序配置中的少即是多。 If you have added @EnableWebMvc you prevent spring boot from auto configuring your serialization and deserialization.如果您添加了@EnableWebMvc,您会阻止 spring 引导自动配置您的序列化和反序列化。

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-spring-mvc-auto-configuration https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-spring-mvc-auto-configuration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是 Spring 启动 ObjectMapper Bean 范围? - What is the Spring boot ObjectMapper Bean scope? 如何在不覆盖 Spring Boot 使用的 bean 的情况下定义自定义 ObjectMapper bean - How can I define a custom ObjectMapper bean without overriding the one used by Spring Boot 自定义ObjectMapper迁移到Spring Boot 2后被忽略 - Custom ObjectMapper ignored after migration to Spring Boot 2 Spring 启动自定义 Bean Loader - Spring Boot Custom Bean Loader Spring Boot 和 ObjectMapper 配置 - Spring Boot and ObjectMapper configuration 在 Spring Boot 存储库中使用自动装配 bean 的自定义删除方法 - custom delete method using autowired bean in spring boot repository 在 Spring Boot 2 中使用自定义 ObjectMapper 返回 ISO-8601 日期 - Return ISO-8601 dates with custom ObjectMapper in Spring Boot 2 如何将 Spring 启动环境 bean 注入自定义 Spring xml bean? - How to inject Spring boot Environment bean to Custom Spring xml bean? Spring Boot/@JDBCTest - 没有可用的“com.fasterxml.jackson.databind.ObjectMapper”类型的合格 bean - Spring Boot/@JDBCTest - No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available 使用Jackson框架的@RequestBody注释的Spring Boot ObjectMapper - Spring Boot ObjectMapper for @RequestBody annotation using Jackson Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM