Configuring Jackson mapper when using Dropwizard












2














I would like to use the Java 8 java.time with Jersey/Jackson in the context of a Dropwizard app. I understand I need to use jackson-modules-java8 and configure the mapper object.



But how do I configure Jersey's automagic mapper that deserialises the incoming JSON for me? I.e. where would I do mapper.registerModule(new JavaTimeModule());?



To illustrate the current situation here is an example class that represents the incoming JSON:



public class Example {
// Want to use java.time instead
private Date date;
private final String ISO_OFFSET_DATE_TIME = "YYYY-MM-DD'T'HH:mm:ssZ";

@JsonCreator
public Example(@JsonProperty("date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
Date date) {
this.date = date;
}

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
public Date getDate() {
return date;
}
}


As you can see that uses the older Date API. The Jersey resources looks like the following:



@Path("/example")
@Consumes(MediaType.APPLICATION_JSON)
public class ExampleResource {
@POST
public void consume(Example example) {
// Do stuff with example.date
}
}









share|improve this question





























    2














    I would like to use the Java 8 java.time with Jersey/Jackson in the context of a Dropwizard app. I understand I need to use jackson-modules-java8 and configure the mapper object.



    But how do I configure Jersey's automagic mapper that deserialises the incoming JSON for me? I.e. where would I do mapper.registerModule(new JavaTimeModule());?



    To illustrate the current situation here is an example class that represents the incoming JSON:



    public class Example {
    // Want to use java.time instead
    private Date date;
    private final String ISO_OFFSET_DATE_TIME = "YYYY-MM-DD'T'HH:mm:ssZ";

    @JsonCreator
    public Example(@JsonProperty("date")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
    Date date) {
    this.date = date;
    }

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
    public Date getDate() {
    return date;
    }
    }


    As you can see that uses the older Date API. The Jersey resources looks like the following:



    @Path("/example")
    @Consumes(MediaType.APPLICATION_JSON)
    public class ExampleResource {
    @POST
    public void consume(Example example) {
    // Do stuff with example.date
    }
    }









    share|improve this question



























      2












      2








      2







      I would like to use the Java 8 java.time with Jersey/Jackson in the context of a Dropwizard app. I understand I need to use jackson-modules-java8 and configure the mapper object.



      But how do I configure Jersey's automagic mapper that deserialises the incoming JSON for me? I.e. where would I do mapper.registerModule(new JavaTimeModule());?



      To illustrate the current situation here is an example class that represents the incoming JSON:



      public class Example {
      // Want to use java.time instead
      private Date date;
      private final String ISO_OFFSET_DATE_TIME = "YYYY-MM-DD'T'HH:mm:ssZ";

      @JsonCreator
      public Example(@JsonProperty("date")
      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
      Date date) {
      this.date = date;
      }

      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
      public Date getDate() {
      return date;
      }
      }


      As you can see that uses the older Date API. The Jersey resources looks like the following:



      @Path("/example")
      @Consumes(MediaType.APPLICATION_JSON)
      public class ExampleResource {
      @POST
      public void consume(Example example) {
      // Do stuff with example.date
      }
      }









      share|improve this question















      I would like to use the Java 8 java.time with Jersey/Jackson in the context of a Dropwizard app. I understand I need to use jackson-modules-java8 and configure the mapper object.



      But how do I configure Jersey's automagic mapper that deserialises the incoming JSON for me? I.e. where would I do mapper.registerModule(new JavaTimeModule());?



      To illustrate the current situation here is an example class that represents the incoming JSON:



      public class Example {
      // Want to use java.time instead
      private Date date;
      private final String ISO_OFFSET_DATE_TIME = "YYYY-MM-DD'T'HH:mm:ssZ";

      @JsonCreator
      public Example(@JsonProperty("date")
      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
      Date date) {
      this.date = date;
      }

      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
      public Date getDate() {
      return date;
      }
      }


      As you can see that uses the older Date API. The Jersey resources looks like the following:



      @Path("/example")
      @Consumes(MediaType.APPLICATION_JSON)
      public class ExampleResource {
      @POST
      public void consume(Example example) {
      // Do stuff with example.date
      }
      }






      json jackson jersey dropwizard






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 4:23

























      asked Nov 23 '18 at 3:34









      Fred Clausen

      1,24212042




      1,24212042
























          1 Answer
          1






          active

          oldest

          votes


















          2














          JavaTimeModule is registered by default in Dropwizard 1.0.0 and above. For previous versions, the dropwizard-java8 bundle provided support for Java 8 features. Java 8 is the baseline for Dropwizard 1.0.0, and the bundle was merged into baseline.



          Assuming you use Dropwizard 1.0.0 or above, if you still need to access the ObjectMapper, you can do it in your Application<T>:




          • in method void initialize(Bootstrap<T> bootstrap), via bootstrap.getObjectMapper()

          • in method abstract void run(T configuration, Environment environment), via environment.getObjectMapper()


          That way, you can register other modules, or enable or disable Jackson features. Some of them impact how Java 8 types are serialized and deserialized.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440362%2fconfiguring-jackson-mapper-when-using-dropwizard%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            JavaTimeModule is registered by default in Dropwizard 1.0.0 and above. For previous versions, the dropwizard-java8 bundle provided support for Java 8 features. Java 8 is the baseline for Dropwizard 1.0.0, and the bundle was merged into baseline.



            Assuming you use Dropwizard 1.0.0 or above, if you still need to access the ObjectMapper, you can do it in your Application<T>:




            • in method void initialize(Bootstrap<T> bootstrap), via bootstrap.getObjectMapper()

            • in method abstract void run(T configuration, Environment environment), via environment.getObjectMapper()


            That way, you can register other modules, or enable or disable Jackson features. Some of them impact how Java 8 types are serialized and deserialized.






            share|improve this answer


























              2














              JavaTimeModule is registered by default in Dropwizard 1.0.0 and above. For previous versions, the dropwizard-java8 bundle provided support for Java 8 features. Java 8 is the baseline for Dropwizard 1.0.0, and the bundle was merged into baseline.



              Assuming you use Dropwizard 1.0.0 or above, if you still need to access the ObjectMapper, you can do it in your Application<T>:




              • in method void initialize(Bootstrap<T> bootstrap), via bootstrap.getObjectMapper()

              • in method abstract void run(T configuration, Environment environment), via environment.getObjectMapper()


              That way, you can register other modules, or enable or disable Jackson features. Some of them impact how Java 8 types are serialized and deserialized.






              share|improve this answer
























                2












                2








                2






                JavaTimeModule is registered by default in Dropwizard 1.0.0 and above. For previous versions, the dropwizard-java8 bundle provided support for Java 8 features. Java 8 is the baseline for Dropwizard 1.0.0, and the bundle was merged into baseline.



                Assuming you use Dropwizard 1.0.0 or above, if you still need to access the ObjectMapper, you can do it in your Application<T>:




                • in method void initialize(Bootstrap<T> bootstrap), via bootstrap.getObjectMapper()

                • in method abstract void run(T configuration, Environment environment), via environment.getObjectMapper()


                That way, you can register other modules, or enable or disable Jackson features. Some of them impact how Java 8 types are serialized and deserialized.






                share|improve this answer












                JavaTimeModule is registered by default in Dropwizard 1.0.0 and above. For previous versions, the dropwizard-java8 bundle provided support for Java 8 features. Java 8 is the baseline for Dropwizard 1.0.0, and the bundle was merged into baseline.



                Assuming you use Dropwizard 1.0.0 or above, if you still need to access the ObjectMapper, you can do it in your Application<T>:




                • in method void initialize(Bootstrap<T> bootstrap), via bootstrap.getObjectMapper()

                • in method abstract void run(T configuration, Environment environment), via environment.getObjectMapper()


                That way, you can register other modules, or enable or disable Jackson features. Some of them impact how Java 8 types are serialized and deserialized.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 9:03









                vin59

                586




                586






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440362%2fconfiguring-jackson-mapper-when-using-dropwizard%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    How to ignore python UserWarning in pytest?

                    Alexandru Averescu