如何调试HttpMessageNotReadableException?

时间:2018-02-25 20:22:38

标签: spring spring-boot

我有一个像这样的DTO对象:

data class ClientDto(
        var uuid: String,
        var ip: String,
        var lastSeen: Date? = Date()
) {
    internal fun toEntity() = Client(uuid=uuid, ip=ip, lastSeen=Date())
}

......和这样的控制器:

@RestController
internal class ClientController {

    @Autowired
    private lateinit var service : ClientService

    @GetMapping("/clients")
    internal fun getClients() =  service.findAll()

    @PostMapping("/clients")
    internal fun postClient(@RequestBody client: ClientDto) = service.add(client)
}

现在我用httpie发布这样的内容:

http POST localhost:8080/clients uuid=my-uuid ip=192.123.31:8080

得到:

{
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "JSON parse error: Can not construct instance of awesome.discovery.domain.dto.ClientDto: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of awesome.discovery.domain.dto.ClientDto: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@71c6ae97; line: 1, column: 2]",
    "path": "/clients",
    "status": 400,
    "timestamp": 1519587678605
}
  1. 这篇文章有什么问题?
  2. 更重要的是我如何调试它,例如知道它的尝试以及如何理解它出错的地方?

1 个答案:

答案 0 :(得分:3)

1-您的DTO需要提供默认构造函数(没有参数的空构造函数)。这是错误所说的

2- Spring根据RequestParam的类型配置Jackson使用反射来自动反序列化JSON。您可以自定义实现JsonDeserializer的此行为。如果要调试,可以在使用PostMapping

注释的方法上设置断点