Hey everyone, maybe there's someone who can guide ...
# spring
p
Hey everyone, maybe there's someone who can guide me to the solution of the following problem: Let's say I have the following dto defined:
Copy code
@Serializable
class RouteDto(
        val id: Int? = null,
        val template: Template?,
        val server: Server?,
        val variables: List<Variable>,
        val name: String,
        val active: Boolean,
        val profile: Profile,
        val description: String
)
And I have the following endpoint:
Copy code
@RestController
@RequestMapping("/routes")
class RouteController(
    @PostMapping("/new")
    fun createRoute(routeDto: RouteDto): ResponseEntity<RouteDto?> {
        val route = saveRouteService.saveRouteFrom(routeDto)
        val dto: RouteDto = route.id?.toDto() ?: return ResponseEntity.unprocessableEntity().build()

        return ResponseEntity.ok(dto)
    }
Now I just wanted to call it like that with mockMvc:
Copy code
@Test
    fun `should create a route`() {
        val dto = RouteDto(null, null, null, listOf(),"testDto", true, Profile.DEV, "should create a route test")
        <http://mockMvc.post|mockMvc.post>("/routes/new") {
            contentType = MediaType.APPLICATION_JSON
            content = Json.encodeToString(dto)
        }.andExpect {
            status { is2xxSuccessful() }
        }
    }
However, I get the following Warning:
Copy code
DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'routeDto' on field 'active': rejected value [null]; codes [typeMismatch.routeDto.active,typeMismatch.active,typeMismatch.boolean,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [routeDto.active,active]; arguments []; default message [active]]; default message [Failed to convert value of type 'null' to required type 'boolean'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [boolean] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]]
Spring seems to fail at reading the Boolean value
active
, which is actually correctly serialized:
Copy code
MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /routes/new
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"136"]
             Body = {"template":null,"server":null,"variables":[],"name":"testDto","active":true,"profile":"DEV","description":"should create a route test"}
    Session Attrs = {}
It would be great if someone could point me in a helpful direction. Thanks in advance!
k
out of curiosity which Boolean is it?
j
Does it work if you add @RequestBody for route: RouteDto parameter?
👆🏻 1
m
I would temporarily remove
active
from the mix here and see if that solves the problem or if that's just the first one it hits.
...but even more I think Jukka is on to something 🙂
p
I knew it had to be such a blatant error.. Thanks guys, it was really the missing request body annotation. Got completely distracted by the error message.. Have a good weekend!
k
hah I thought so but on the other hand what was spring doing without that annotation? it looks like it understood the object no?
p
Exactly, that's why I didn't even think about the RequestBody annotation.