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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With