Spring Boot没有使用Jackson Kotlin插件

Ela*_*las 5 spring jackson kotlin spring-boot

我不能强迫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() {
        (...)
}
Run Code Online (Sandbox Code Playgroud)

我试图配置杰克逊,但它没有多大帮助.有什么奇怪的,在我配置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)
    }
}
Run Code Online (Sandbox Code Playgroud)

可能重要的是模型与应用程序本身在不同的项目中.

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>
Run Code Online (Sandbox Code Playgroud)

我现在尝试的是:

所有这些答案

类似的问题,但无论如何可能是不同的情况

还有一些其他的,不值得注意的.

Ela*_*las 4

我的结构如下:

abstract class AbstractController (...) {
    fun save(@RequestMapping entity: T) {
    (...)
}
Run Code Online (Sandbox Code Playgroud)

为了启用它,我使用了

class DeriveredController (...) {
    @PostMapping
    override fun save(entity: Derivered) {
    (...)
    }
}
Run Code Online (Sandbox Code Playgroud)

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