我有一个代表城市的简单数据类
data class City(
val name: String,
val centroid: Coordinates
)
Run Code Online (Sandbox Code Playgroud)
出于外部兼容性的原因,该Coordinates类型被定义为typealias
typealias Coordinates = Pair<Double, Double>
val Coordinates.lat
get() = first
val Coordinates.lon
get() = second
Run Code Online (Sandbox Code Playgroud)
如何配置 Jackson,以便它能够将以下 JSON 反序列化为 的实例City?
{
"name": "Praha",
"centroid": {
"lat": 50.2141,
"lon": 14.42342
}
}
Run Code Online (Sandbox Code Playgroud) 在 Jersey 中使用拦截器我可以操作输出,但是,我还想向响应添加一个 Header,该值是根据输出结果计算的。
@Sha256Sum
public class Sha256SumInterceptor implements WriterInterceptor {
public static final String SHA256_HASH_HEADER_NAME = "SHA256-SUM";
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
// Retrieve the OutputStream, read the contents and calculate the hashsum.
// Set the header value in context.
context.proceed();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,问题是当我最终阅读了整个流时,我无法将标题设置为调用 context.proceed 和写入的内容(从而使我能够对其进行任何操作)我无法再设置标题。
简而言之,我的问题是:如何将整个流输出捕获为字节 [],从字节数组计算结果,并最终在对计算结果的响应中设置标头?我不想耗尽输出流。