spring - 如何调试 HttpMessageNotReadableException?

标签 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())
}

...和这样的 Controller :

@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- 您的 DTO 需要提供一个默认构造函数(没有参数的空构造函数)。这就是错误所说的

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

注释的方法上放置一个断点

关于spring - 如何调试 HttpMessageNotReadableException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48978072/

相关文章:

java - 在 Spring Boot 应用程序中创建自定义连接池

java - Spring Boot 和 mongodb 中的连接池

mysql - {"timestamp": 1567422726395 ,"status": 404 ,"error": "Not Found" ,"message": "No message available" ,"path": "/SpringRestfulWebServiceHibernate/add/" }

spring-boot - Spring Boot 使用 Hikari 连接池

spring-boot - 使用作业存储 'org.quartz.simpl.RAMJobStore' - 不支持持久性。并且不聚集

java - 使用 WEB-INF/views/index.jsp 获取 404 http 错误

spring - BeanInstantiationException - 构造函数抛出异常;嵌套异常是 java.lang.NullPointerException

java - Spring @Controller rsp. @RestController 与 spring-data-jpa 存储库进行事务处理?看起来像这样

java - 如何关闭通过 Spring 创建的 jdbc 连接?

java - 更改 Spring Boot 2.1.2 上文件上传的大小限制