From ab0a062b954c431d5dcc2abcbca8caba856deae8 Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Mon, 4 Nov 2019 13:49:07 +0100 Subject: Add response type for JSON serialization using Jackson --- .../com/yahoo/restapi/JacksonJsonResponse.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 container-core/src/main/java/com/yahoo/restapi/JacksonJsonResponse.java (limited to 'container-core') diff --git a/container-core/src/main/java/com/yahoo/restapi/JacksonJsonResponse.java b/container-core/src/main/java/com/yahoo/restapi/JacksonJsonResponse.java new file mode 100644 index 00000000000..d3b960651b0 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/restapi/JacksonJsonResponse.java @@ -0,0 +1,48 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.restapi; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.log.LogLevel; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.logging.Logger; + +/** + * A JSON response using Jackson for serialization. + * + * @author bjorncs + */ +public class JacksonJsonResponse extends HttpResponse { + + private static final Logger log = Logger.getLogger(JacksonJsonResponse.class.getName()); + private static final ObjectMapper defaultJsonMapper = new ObjectMapper(); + + private final ObjectMapper jsonMapper; + private final T entity; + + public JacksonJsonResponse(int statusCode, T entity) { + this(statusCode, entity, defaultJsonMapper); + } + + public JacksonJsonResponse(int statusCode, T entity, ObjectMapper jsonMapper) { + super(statusCode); + this.entity = entity; + this.jsonMapper = jsonMapper; + } + + @Override + public void render(OutputStream outputStream) throws IOException { + if (log.isLoggable(LogLevel.DEBUG)) { + String json = jsonMapper.writeValueAsString(entity); + log.log(LogLevel.DEBUG, "Writing the following JSON to response output stream:\n" + json); + outputStream.write(json.getBytes()); + } else { + jsonMapper.writeValue(outputStream, entity); + } + } + + @Override public String getContentType() { return "application/json"; } + public T getEntity() { return entity; } +} -- cgit v1.2.3 From 725f1158944463c07aaa45f61dd801cf45948301 Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Mon, 4 Nov 2019 15:34:57 +0100 Subject: Update abi-spec.json with JacksonJsonResponse --- container-core/abi-spec.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'container-core') diff --git a/container-core/abi-spec.json b/container-core/abi-spec.json index e5875247855..8905a27a127 100644 --- a/container-core/abi-spec.json +++ b/container-core/abi-spec.json @@ -892,6 +892,21 @@ ], "fields": [] }, + "com.yahoo.restapi.JacksonJsonResponse": { + "superClass": "com.yahoo.container.jdisc.HttpResponse", + "interfaces": [], + "attributes": [ + "public" + ], + "methods": [ + "public void (int, java.lang.Object)", + "public void (int, java.lang.Object, com.fasterxml.jackson.databind.ObjectMapper)", + "public void render(java.io.OutputStream)", + "public java.lang.String getContentType()", + "public java.lang.Object getEntity()" + ], + "fields": [] + }, "com.yahoo.restapi.MessageResponse": { "superClass": "com.yahoo.container.jdisc.HttpResponse", "interfaces": [], -- cgit v1.2.3