Jackson Kotlin plugin not used by Spring Boot

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

//Exception 2018-02-23 13:29:09.046 ERROR 24730 --- [nio-9300-exec-1] oaccC[.[.[.[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 setting up Jackson, but that didn't help. What a weird method inside @Bean, where I configure ObjectMapper, everything works fine. In addition, when I added default values ​​for non-nullable fields, they were not 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 may be important is that the model is in a different project than the program itself.

Kotlin version - 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 up to this point:

All these answers

Famous issue, but maybe anyway anyway

And some others are not worth noting.

+5
source share
1 answer

I had this structure:

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

And to turn it on, I used

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

The problem was the lack of @RequestMapping in the derived function.

+4
source

Source: https://habr.com/ru/post/1275541/


All Articles