基于此Jira
我有这个方法:
val innerResult: DeferredResult[Object] = new DeferredResult[Object]()
override def setResult(result: T): Boolean = {
val beanPropertyFilter: SimpleBeanPropertyFilter = filter.size match {
case 0 => SimpleBeanPropertyFilter.serializeAll()
case _ => SimpleBeanPropertyFilter.filterOutAllExcept("id")
}
val filterProvider = new SimpleFilterProvider()
.addFilter("propertiesFilter", beanPropertyFilter)
val wrapper = new MappingJacksonValue(result)
wrapper.setFilters(filterProvider)
innerResult.setResult(wrapper)
}
在回复中我看到了:
{"headers":{}, "body":[{"id":"573080B50CCDED33E08DA678"}], "statusCode":"OK"}
虽然我想看到:
[{"id":"573080B50CCDED33E08DA678"}]
我做错了什么?
答案 0 :(得分:2)
我不知道Scala并且无法关注您的代码(result
定义在哪里?)。也许添加方法声明并修复缩进?
在任何情况下,您似乎都获得了整个响应的JSON表示,而不仅仅是响应主体。在Java中,ResponseEntity
类具有以下属性:
headers
:响应标头(Cache-Control等)statusCode
:响应状态(200 OK,404 Not Found等)body
:响应正文(一个包含单个对象且在您的情况下带有id字段的数组)这部分看起来很可疑:
val wrapper = new MappingJacksonValue(result)
innerResult.setResult(wrapper)
看起来你正在取结果(body,header,statusCode)并将其存储在innerResult中。仅基于变量名称,这似乎是倒退。
答案 1 :(得分:0)
基于@bernie方向,完整代码应该看起来像
case class FilterDeferredResult[T <: ResponseEntity[_]](properties: Option[Set[String]] = Some(Set.empty), innerResult: DeferredResult[Object]) extends DeferredResult[T] {
override def setErrorResult(result: scala.Any): Boolean = {
super.setErrorResult(result)
}
override def setResult(result: T): Boolean = {
val beanPropertyFilter: SimpleBeanPropertyFilter = properties match {
case None => SimpleBeanPropertyFilter.serializeAll()
case Some(p) => p.size match {
case 0 => SimpleBeanPropertyFilter.serializeAll()
case _ => SimpleBeanPropertyFilter.filterOutAllExcept(p)
}
}
val filterProvider = new SimpleFilterProvider().addFilter("propertiesFilter", beanPropertyFilter)
val wrapper = new MappingJacksonValue(result.getBody)
wrapper.setFilters(filterProvider)
val bodyBuilder = new ResponseEntity(wrapper,result.getHeaders, result.getStatusCode)
innerResult.setResult(bodyBuilder)
}
}
所以我需要根据第一个创建新的ResponseEntity,并将正文包裹在MappingJacksonValue