spring - 如何配置 Spring 以使用 Json4s 序列化响应主体?
问题描述
我在 Scala 项目中使用 Spring Boot,并且已经使用 Json4s 对 JSON 进行序列化和反序列化。到目前为止,我一直在编写这样的端点:
@RequestMapping(path = Array("/getSomething"), produces = Array(MediaType.APPLICATION_JSON_VALUE))
def getSomething: String = {
// do some things
val resultValue: ResultType = ??? // where ResultType is some case class that can be serialized with json4s
json4s.native.Serialization.write(resultValue)
}
但是,我真的希望能够避免最后一步,同时更清楚端点的返回类型是什么。此外,我希望能够提取返回类型以生成 API 文档。所以相反,我想写这样的东西:
@RequestMapping(path = Array("/getSomething"), produces = Array(MediaType.APPLICATION_JSON_VALUE))
def getSomething: ResultType = {
// do some things
resultValue
}
但是,当我这样做时,结果总是只是{}
. 我认为这是因为 Spring 使用的是 Jackson 而不是 Json4s,并且我没有注释用于 Jackson 的案例类。我要做的就是添加一些在每个端点上调用的拦截器,并将结果转换为 JSON 字符串。编写拦截器很容易(它只是json4s.native.Serialization.write
),但是我怎样才能注册它以便让 Spring 每次都自动使用它呢?
解决方案
您可以使用该json4s-jackson
模块并通过创建一个 Spring Bean 来使用自定义序列化器注册您的案例类,例如:
@Bean def json4sCustomizer: Jackson2ObjectMapperBuilderCustomizer = builder => {
builder.serializerByType(classOf[ResultType], new JsonSerializer[ResultType] {
val json4sSerializer = new json4s.jackson.JValueSerializer
implicit val formats:Formats = DefaultFormats
override def serialize(value: ResultType, gen: JsonGenerator, serializers: SerializerProvider): Unit =
json4sSerializer.serialize(json4s.Extraction.decompose(value), gen, serializers)
})
}
完全替换 Jackson 将涉及HTTP 消息转换器,并且可能更方便/更不方便,具体取决于您的应用程序的其他问题
@Bean def json4sConverter: HttpMessageConverter[AnyRef] = new AbstractJsonHttpMessageConverter {
override def readInternal(resolvedType: Type, reader: Reader): AnyRef = ???
override def writeInternal(value: Any, typ: Type, writer: Writer): Unit = ???
}
推荐阅读
- html - Phoenix 无法生成带有错误“无效模块名称”的 html 模板
- list - 如何修复 Haskell 中的解析错误(可能不正确的缩进或不匹配的括号)
- c# - C#:如何转换任务
到任务的 IEnumerable - javascript - 每次获取文档时都将基于其他字段的新字段添加到文档中
- typescript - 打字稿>泛型>联合约束
- reactjs - TabNavigator 中图标的 onpress 事件(使用 react-navigation 反应原生)
- android - 在方向更改时保存实时视频流
- perl - Perl 一个线性命令将 EST 时间转换为 GMT 时间
- python - 如何在包括“或”在内的多个条件下使用 np.where?
- asp.net-core-webapi - 将 WebAPI GET 方法中的参数传递给过滤列表