【发布时间】:2019-12-04 12:27:48
【问题描述】:
我在 Kotlin 中使用 Spring Boot。
我正在接收一些 JSON 字符串,并使用 ObjectMapper 对其进行解析,但我想验证它是否具有模型中的所有内容 - 即 id 和 s3FilePath 不是空白或缺失。
所以这是我要验证的模型:
@JsonIgnoreProperties(ignoreUnknown = true)
class MyModel {
var id : String = ""
var s3FilePath : String = ""
}
这是我使用该模型的地方:
class FirstMessage {
fun create(newMessage: String) : String {
val objectMapper = ObjectMapper()
val parsedMap : MyModel = objectMapper.readValue(newMessage, MyModel::class.java)
val result = MyModel()
result.id = parsedMap.id
result.s3FilePath = parsedMap.s3FilePath
return objectMapper.writeValueAsString(result)
}
}
最后我有这个测试,我想验证一个异常:
@Test
fun incompleteDataReturnsException() {
var input = """{"missing": "parts"}"""
// FirstMessage().create(input) // Will make some assertion here here
}
任何帮助将不胜感激。我刚刚开始使用 Spring 及其相当“强烈”。
谢谢。
附言如果创建该模型错误/有更好的方法,请告诉我。我有点不确定这是否正确。
【问题讨论】:
标签: spring spring-boot kotlin