如何在 finatra 中使用 camelCase 设置响应
how to set response with camelCase in finatra
我需要 return 一个 json 响应以 camelCase 和 finatra 但默认情况下它是 snake_case。从我目前发现的情况来看,我需要使用 ObjectMapper,但我无法理解创建它后将它传递到哪里。一个例子会很有帮助。这是我拥有的:
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
class myTargetingController extends Controller {
val endpoint = "http://....."
get(s"$endpoint/?") { request: Request =>
// what do I do with it?
// val objectMapper = ScalaObjectMapper.builder.camelCaseObjectMapper
response.ok.json(myObject)
}
}
============================================= =====================
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.finagle.http.{ Request, Response}
import com.twitter.finatra.http.routing.HttpRouter
import com.twitter.finatra.http.{HttpServer}
import com.twitter.finatra.http.filters.CommonFilters
import com.twitter.util.Future
object MyServerApp extends MyServer
class MyServer extends HttpServer {
override protected def configureHttp(router: HttpRouter) {
router
.filter[CommonFilters]
.add[CorsFilter, MyController]
}
}
P.S。我对 Scala 非常非常陌生
根据评论跟进
定义自定义 ObjectMapperModule
class CamelCaseModule extends ScalaObjectMapperModule {
override val propertyNamingStrategy: PropertyNamingStrategy =
new PropertyNamingStrategy.UpperCamelCaseStrategy
override def additionalMapperConfiguration(mapper: ObjectMapper): Unit = {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
}
}
覆盖服务器的默认 Jackson 模块
override def jacksonModule = new CamelCaseModule
确保你有
"com.twitter" %% "finatra-jackson" % yourFinatraVersion % "test"
在你的 build.sbt
并且您导入
import com.fasterxml.jackson.databind.{DeserializationFeature, Module, ObjectMapper, PropertyNamingStrategy}
import com.twitter.finatra.jackson.modules.ScalaObjectMapperModule
在本地测试过,似乎有效
希望这有帮助
我需要 return 一个 json 响应以 camelCase 和 finatra 但默认情况下它是 snake_case。从我目前发现的情况来看,我需要使用 ObjectMapper,但我无法理解创建它后将它传递到哪里。一个例子会很有帮助。这是我拥有的:
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.Controller
class myTargetingController extends Controller {
val endpoint = "http://....."
get(s"$endpoint/?") { request: Request =>
// what do I do with it?
// val objectMapper = ScalaObjectMapper.builder.camelCaseObjectMapper
response.ok.json(myObject)
}
}
============================================= =====================
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.finagle.http.{ Request, Response}
import com.twitter.finatra.http.routing.HttpRouter
import com.twitter.finatra.http.{HttpServer}
import com.twitter.finatra.http.filters.CommonFilters
import com.twitter.util.Future
object MyServerApp extends MyServer
class MyServer extends HttpServer {
override protected def configureHttp(router: HttpRouter) {
router
.filter[CommonFilters]
.add[CorsFilter, MyController]
}
}
P.S。我对 Scala 非常非常陌生
根据评论跟进
定义自定义 ObjectMapperModule
class CamelCaseModule extends ScalaObjectMapperModule {
override val propertyNamingStrategy: PropertyNamingStrategy =
new PropertyNamingStrategy.UpperCamelCaseStrategy
override def additionalMapperConfiguration(mapper: ObjectMapper): Unit = {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
}
}
覆盖服务器的默认 Jackson 模块
override def jacksonModule = new CamelCaseModule
确保你有
"com.twitter" %% "finatra-jackson" % yourFinatraVersion % "test"
在你的 build.sbt
并且您导入
import com.fasterxml.jackson.databind.{DeserializationFeature, Module, ObjectMapper, PropertyNamingStrategy}
import com.twitter.finatra.jackson.modules.ScalaObjectMapperModule
在本地测试过,似乎有效 希望这有帮助