【问题标题】:Jackson Kotlin plugin not used by Spring BootSpring Boot 未使用 Jackson Kotlin 插件
【发布时间】:2018-08-03 16:02:16
【问题描述】:

我不能强制 Spring 为 Jackson 使用 Kotlin 模块。 问题是 Jackson 无法将 JSON 解析为数据类。

//Exception
2018-02-23 13:29:09.046 ERROR 24730 --- [nio-9300-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/services] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [*.model.User]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Parameter specified as non-null is null: method *.model.User.<init>, parameter name] with root cause
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method *.User.<init>, parameter name

//JSON
{
    "name": "name",
    "surname": "surname",
    "email": "email",
    "password": "pswd"
}

//Model
@Entity
@Table
data class User(
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        var userId: Long?,
        var name: String,
        var surname: String,
        var email: String,
        var password: String,
        (...)
        ): Resource() {
        (...)
}

我尝试配置 Jackson,但没有太大帮助。奇怪的是,在我配置 ObjectMapper 的 @Bean 方法中,一切正常。 此外,当我为不可为空的字段添加默认值时,它们并没有被覆盖。

@Configuration
class JacksonConfig {
    @Bean
    fun mappingJackson2HttpMessageConverter(): MappingJackson2HttpMessageConverter {
        val mapper = ObjectMapper().registerKotlinModule()
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
        var user = mapper.readValue<User>("{\n" +
                "\t\"name\": \"name\",\n" +
                "\t\"surname\": \"surname\",\n" +
                "\t\"email\": \"email\",\n" +
                "\t\"password\": \"pswd\"\n" +
                "}")
        return MappingJackson2HttpMessageConverter(mapper)
    }
}

重要的是模型与应用程序本身位于不同的项目中。

Kotlin 版本是 1.20 杰克逊依赖:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version> <!--2.9.4-->
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-kotlin</artifactId>
        <version>${jackson.version}</version>
    </dependency>

以及我到现在为止的尝试:

All of these answers

Similiar problem but probably different case anyway

还有一些其他的,不值得关注。

【问题讨论】:

  • 看来您的映射器配置是正确的。一切都应该工作。您能否为输入请求添加日志记录,您能否添加示例如何向控制器发送请求?
  • 发现错误,我之前对问题的分析是错误的。

标签: spring spring-boot jackson kotlin


【解决方案1】:

我的结构如下:

abstract class AbstractController (...) {
    fun save(@RequestMapping entity: T) {
    (...)
}

为了启用它,我使用了

class DeriveredController (...) {
    @PostMapping
    override fun save(entity: Derivered) {
    (...)
    }
}

问题在于派生函数中缺少@RequestMapping。

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2016-05-07
    • 2018-10-12
    • 1970-01-01
    • 2022-12-20
    • 2017-05-28
    • 2018-05-21
    • 2021-03-31
    • 2019-07-29
    相关资源
    最近更新 更多