Java 为什么Spring引导不使用自定义ObjectMapperbean?

Java 为什么Spring引导不使用自定义ObjectMapperbean?,java,spring,spring-boot,Java,Spring,Spring Boot,我将自定义ObjectMapperbean注释为@Primary @Configuration public class BeanConfig { @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new Jdk8Module());

我将自定义ObjectMapperbean注释为@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;
    }

}
然而,当我试图用不同的大小写枚举解析表单时,它会给我错误

@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();
    }

}
这是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()
}
我已尝试声明Jackson2ObjectMapperBuilderbean。这没用

你知道可能会发生什么吗,或者我应该去哪里查找问题?

检查让spring引导自动配置你的应用程序。在spring boot应用程序配置中,越少越好。如果添加了@EnableWebMvc,则会阻止spring boot自动配置序列化和反序列化

  • 创建一个
    Jackson2ObjectMapperBuilderCustomizer
    bean,例如。
    @Bean
    公共Jackson2ObjectMapperBuilderCustomizer Jackson2ObjectMapperBuilderCustomizer(){
    返回生成器->{
    serializationinclude(JsonInclude.Include.NON_EMPTY);
    builder.features禁用(
    SerializationFeature.WRITE_DATES_作为时间戳,
    SerializationFeature.FAIL\u在\u空\u bean上,
    已忽略属性上的反序列化Feature.FAIL\u,
    反序列化功能。在未知属性上失败);
    featuresToEnable(反序列化功能。接受单个值作为数组);
    };
    }
    
  • 或者在spring boot应用程序属性中对其进行自定义。如。
    spring:
    杰克逊:
    制图员:
    默认视图包含:true
    序列化:
    缩进输出:真
    将日期写入时间戳:false
    反序列化:
    在\u上失败\u忽略\u属性:false
    在未知属性上失败:false
    接受\u单个\u值\u作为\u数组:true
    默认属性包含:非空
    

  • 请添加您的jsonpayload@VovaBilyachat我该怎么做?它是来自网页的表单。问题是当我以正确的方式发送相同的枚举时,它不会给我错误。您不需要重新定义bean,只需编辑
    application.properties
    即可启用所需的功能。请参阅@andreoss
    spring.jackson.mapper.accept不区分大小写的enums=true
    属性也不会给出任何结果,以便检查jackson中的问题mb。因为我只在
    application.properties
    中添加了特性,而没有添加ObjectMapperbean。和loggin启用的自动连线对象映射器的映射器功能feaure已启用,但仍然存在相同的问题。不适用于me@north32请删除您的Jackson deps,网络初学者将包括它们。杰克逊2.12API中有一些突破性的变化。我认为SpringBoot2.4.x将保持2.11.x不工作。我通过使用json数据+找到了解决方案@RequestBody@north32你的问题根本与Jackson无关。我在任何配置中都没有@EnableWebMvc注释。
    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()
    }