diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java index b3b749c..996ea59 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -2,6 +2,7 @@ package io.sinistral.proteus.server; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.google.inject.Inject; import io.sinistral.proteus.server.predicates.ServerPredicates; @@ -23,6 +24,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * @author jbauer @@ -40,6 +42,8 @@ public class ServerResponse @Inject protected static ObjectMapper OBJECT_MAPPER; + protected static Map, ObjectWriter> WRITER_CACHE = new ConcurrentHashMap<>(); + protected ByteBuffer body; protected int status = StatusCodes.OK; @@ -634,11 +638,12 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t exchange.getResponseSender().send(ByteBuffer.wrap(XML_MAPPER.writeValueAsBytes(this.entity))); } else { - final Class jsonViewClass = exchange.getAttachment(JsonViewWrapper.JSON_VIEW_KEY); + final Class jsonViewClass = exchange.getAttachment(JsonViewWrapper.JSON_VIEW_KEY); if(jsonViewClass != null) { - exchange.getResponseSender().send(ByteBuffer.wrap(OBJECT_MAPPER.writerWithView(jsonViewClass).writeValueAsBytes(this.entity))); + ObjectWriter writer = WRITER_CACHE.computeIfAbsent(jsonViewClass, (view) -> OBJECT_MAPPER.writerWithView(view)); + exchange.getResponseSender().send(ByteBuffer.wrap(writer.writeValueAsBytes(this.entity))); } else {