-
Notifications
You must be signed in to change notification settings - Fork 149
2.0.0 Content Negotiation (with Muuntaja)
Tommi Reiman edited this page Oct 12, 2017
·
6 revisions
- Compojure-api 2.0.0 supports JSON, EDN and Transit out-of-the-box. YAML and MessagePack are optional.
- 2.0.0 will use Muuntaja instead of ring-middleware-format for content negotiation as it's up to 10x faster.
- Muuntaja configuration guide: https://github.com/metosin/muuntaja/wiki/Configuration
(require '[compojure.api.sweet :refer :all])
(require '[ring.util.http-response :refer [ok]])
(require '[metosin.transit.dates :as transit-dates]) ;; https://github.com/metosin/metosin-common
(require '[muuntaja.core :as m])
;; create a muuntaja instance with custom transit-settings
(def muuntaja
(m/create
(update-in
muuntaja/default-options
[:formats "application/transit+json"]
merge
{:decoder-opts {:handlers transit-dates/readers}
:encoder-opts {:handlers transit-dates/writers}})))
(api
{:formats muuntaja}
(GET "/pizza" []
(ok {:now (org.joda.time.DateTime/now)})))
Is options map is passed, it's automatically coerced into Muuntaja instance
(api
{:formats
(update-in
muuntaja/default-options
[:formats "application/transit+json"]
merge
{:decoder-opts {:handlers transit-dates/readers}
:encoder-opts {:handlers transit-dates/writers}})}
(GET "/pizza" []
(ok {:now (org.joda.time.DateTime/now)})))
(require '[muuntaja.middleware])
(def app
(->
(routes
(api
(POST "/echo" []
:body [data {:name String}]
(ok data)))
(api
(POST "/echo2" []
:body [data {:name String}]
(ok data))))
(muuntaja.middeleware/wrap-format)))
... or with configuration:
(def app
(->
(routes
(api
(POST "/echo" []
:body [data {:name String}]
(ok data)))
(api
(POST "/echo2" []
:body [data {:name String}]
(ok data))))
(muuntaja.middeleware/wrap-format muuntaja)))