How to parse JSON request with custom Content-Type

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to parse JSON request with custom Content-Type

Mirko Nasato
Hi all,

It's great to be able to parse a JSON request just by doing context.parse(MyBean.class) but apparently that only works if the request content type is application/json.

In my current project we use custom content types of the application/vnd.mycompany.mytype+json variety, and in that case Ratpack returns a 500 error saying NoSuchParserException: Could not find parser...

Is there any way to configure how to parse custom content types?

For the time being we had to resort to using the Jackson ObjectMapper directly.

Thanks

Mirko
Reply | Threaded
Open this post in threaded view
|

Re: How to parse JSON request with custom Content-Type

Marcin Erdmann
Hi Mirko,

Unfortunately looks like JsonParser is not really configurable at the moment. It also lives in an internal namespaced package (ratpack.jackson.internal) so you probably shouldn't depending on that class to be there in whatever your solution is if you want to not have to change it when upgrading Ratpack version.

The safest solution that comes to my mind is creating your own parser by extending ratpack.parse.ParserSupport, coping and pasting the implementation of JsonParser, changing this condition and adding your new parser to the registry.
Reply | Threaded
Open this post in threaded view
|

Re: How to parse JSON request with custom Content-Type

Luke Daley
Administrator
Hi,

We'll fix this by attempting to parse anything where the type ends in `+json`.

https://github.com/ratpack/ratpack/issues/793

In the meantime, you will have to indeed do the parsing yourself.
Reply | Threaded
Open this post in threaded view
|

Re: How to parse JSON request with custom Content-Type

Mirko Nasato
Thanks. I've also raised

  https://github.com/ratpack/ratpack/issues/794

as generating a 500 Internal Server Error in this case doesn't seem quite right anyway.