Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何调试HttpMessageTreadableException?_Spring_Spring Boot - Fatal编程技术网

Spring 如何调试HttpMessageTreadableException?

Spring 如何调试HttpMessageTreadableException?,spring,spring-boot,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 {

我有这样一个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)
}
。。。还有这样一个控制器:

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


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

    注释的方法上设置断点。我知道kotlins默认构造函数的设置与我在数据类中所做的相同。啊,您是对的。这里有一个关于Java中默认构造函数的可视性的答案:没错,我没有使用kotlin,但我在ProjectLombok的
    数据
    注释中遇到了类似的问题