Skip to content

Setting media type (json for eg) manually not working for certain cases. #482

Closed

Description

@chirans

As a continuation of discussion we had in Google group, please find the following observations:-

The below response handling doesn't work when I use it with use(new Jackson());

response.status(Status.OK).type(MediaType.json).send("{\"status\":\"failed\", \"msg\" : \"" + msg + "\"}");

But it works if I comment use(new Jackson()); out, not sure if this is expected.

Unfortunately, setting MediaType json manually doesn't work in deferred API and hence I'd need to resort to use(new Jackson()); to send JSON response from a deferred API by returning map (Jackson would convert to JSON)

Activity

  1. changed the title [-]Customizing media type (json for eg) not working[/-] [+]Setting media type (json for eg) manually not working for certain cases.[/+] on Sep 21, 2016
  2. added this to the 1.0.0 milestone on Sep 26, 2016
  3. jknack commented on Sep 26, 2016

    @jknack
    Member

    There two issues here:

    1-- writing raw json doesn't work bc Jackson outputs String, primitives or similar as valid JSON. There are a lot of discussion around this... but there is no formal/unique solution.

    For this specific use case I suggest you to write a custom render, like:

        /** Raw JSON renderer .*/
        renderer((value, ctx) -> {
          if (value instanceof String && ctx.accepts(MediaType.json)) {
            ctx.send(value.toString());
          }
        });
    
        use(new Jackson());

    So, here you test for a String output and application/json and write directly.

    Please note the custom renderer is on top of the Jackson module. Renderers in jooby are executed in the order they were provided.

    2-- We do have a bug while returning results from Deferred API, this is now fixed and this is now possible:

        get("/482/raw", promise(deferred -> {
          exec.execute(deferred.run(() -> Results.json("{\"status\":\"success\"}")));
        }));
  4. paul-hammant commented on Jan 30, 2018

    @paul-hammant
    Contributor

    Would love a one-liner for this: stringsWillBeJson();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions