Spring 如何调试HttpMessageTreadableException?
我有这样一个DTO对象: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 {
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的数据
和值
注释中遇到了类似的问题