Class Codec

java.lang.Object
dev.relism.flash.ext.jackson.Codec

public abstract class Codec extends Object
One Jackson mapper, in the shape a handler needs it.

Every Jackson data format is the same databind model behind a different factory, so this is the whole of what a format module has to say: which mapper, and which content type it writes. What the annotations mean, what a body is checked against and how a type is described are the same for all of them.

Retrieve it once at boot — @Inject private Json json; — and call it on the hot path. The underlying ObjectMapper is thread-safe once configured.

  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Codec(com.fasterxml.jackson.databind.ObjectMapper mapper, dev.relism.flash.http.ContentType contentType)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    body(dev.relism.flash.models.Request request, Class<T> type)
    Reads the request body as type and verifies its constraints.
    dev.relism.flash.http.ContentType
    What this codec writes, for a handler that sets the response type itself.
    com.fasterxml.jackson.databind.ObjectMapper
    The mapper itself, for everything these methods do not cover.
    write(dev.relism.flash.models.Response response, Object value)
    Serializes value and sets this codec's content type on the response.
    writeView(dev.relism.flash.models.Response response, Object value, Class<?> view)
    Like write(dev.relism.flash.models.Response, java.lang.Object), restricted to the fields visible under a Jackson @JsonView.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Codec

      protected Codec(com.fasterxml.jackson.databind.ObjectMapper mapper, dev.relism.flash.http.ContentType contentType)
  • Method Details

    • body

      public <T> T body(dev.relism.flash.models.Request request, Class<T> type) throws Exception
      Reads the request body as type and verifies its constraints.

      Read straight off the request's stream, which Flash reuses per connection: nothing buffers the body to hand it over. A type that declares no constraints is not checked at all.

      Throws:
      dev.relism.flash.exceptions.HttpException - 400 if the body cannot be parsed as type
      ValidationException - 422 if it parses but violates a constraint
      Exception
    • write

      public String write(dev.relism.flash.models.Response response, Object value) throws Exception
      Serializes value and sets this codec's content type on the response.
      Throws:
      Exception
    • writeView

      public String writeView(dev.relism.flash.models.Response response, Object value, Class<?> view) throws Exception
      Like write(dev.relism.flash.models.Response, java.lang.Object), restricted to the fields visible under a Jackson @JsonView.
      Throws:
      Exception
    • contentType

      public dev.relism.flash.http.ContentType contentType()
      What this codec writes, for a handler that sets the response type itself.
    • mapper

      public com.fasterxml.jackson.databind.ObjectMapper mapper()
      The mapper itself, for everything these methods do not cover.