Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Kotlin plugin not used by Spring Boot

Tags:

I can't force Spring to use Kotlin module for Jackson. The problem is that Jackson can't parse JSON into data class.

//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() {
        (...)
}

I tried to config Jackson, but it didn't help much. What's strange, inside @Bean method where I configure ObjectMapper, everything works fine. Also, when I added default values for non-nullable fields, they weren't overwritten.

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

What might be important is that model is in the different project than the application itself.

Kotlin version is 1.20 Jackson dependencies:

    <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>

And what I tried to this moment:

All of these answers

Similiar problem but probably different case anyway

And some other, not worth noticing.

like image 618
Elas Avatar asked Feb 23 '18 14:02

Elas


1 Answers

I had structure like:

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

And to enable it I used

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

Problem was with lack of @RequestMapping in derivered function.

like image 177
Elas Avatar answered Sep 19 '22 13:09

Elas