1 2 Previous Next 25 Replies Latest reply on Jan 9, 2013 6:11 PM by jensaug

    Easy way to implement Jackson in Jboss AS7

    jacktrades

      Having a small project JAX-RS in jboss as-7, everything is working fine but a json response.

       

      Thing is that I want to enable Jackson, (disable jettison completely) so I can control my JSON better.

       

      Can you reference me to the steps I need to do?

        • 1. Re: Easy way to implement Jackson in Jboss AS7
          rcd

          Just include the Jackson JAX-RS provider in your WAR's /lib directory. If you're using Maven and Jackson 2.x, as I am, these are the coordinates:

           

          <groupId>com.fasterxml.jackson.jaxrs</groupId>

          <artifactId>jackson-jaxrs-json-provider</artifactId>

          1 of 1 people found this helpful
          • 2. Re: Easy way to implement Jackson in Jboss AS7
            jacktrades

            I just one to use built-in resteasy jackson jar:

             

            /usr/share/jboss-as/modules/org/jboss/resteasy/resteasy-jackson-provider/main/resteasy-jackson-provider-2.3.2.Final.jar

            • 3. Re: Easy way to implement Jackson in Jboss AS7
              rcd

              Then you need to add it as a dependency. You can accomplish that using a Dependencies entry in MANIFEST.MF or with a jboss-deployment-structure.xml; see here.

               

              To avoid classloading problems, you should probably avoid bundling any Jackson JARs with your application. Just add them as dependencies in the same way you add the provider module. If you want to bundle them anyway, you need to make sure you exclude the Jackson dependencies the provider module uses and create deployment modules to replace them; this can only be done using jboss-deployment-structure.xml.

               

              Also note that the RESTEasy Jackson provider only works with Jackson 1.x, so you cannot use it if you are using Jackson 2.x.

              1 of 1 people found this helpful
              • 4. Re: Easy way to implement Jackson in Jboss AS7
                jacktrades

                My Eclipse Project is not Maven, just a Regular EAR (EJB Project + WEB Project).

                 

                And yes, I'm trying to avoid injecting external JARs.

                 

                Does Glassfish 3.1.1 uses Jackson 1.x as well?

                • 5. Re: Easy way to implement Jackson in Jboss AS7
                  rcd

                  We don't use Glassfish around here, so I don't know. Jackson 2.x is fairly new and the last release of Glassfish was a while ago, if I'm not mistaken, so it would probably be using 1.x.

                  • 6. Re: Easy way to implement Jackson in Jboss AS7
                    jacktrades

                    Let's suppose that Jboss start using Jackson.

                     

                    What if I want to change a bit the format of the JSON REST response. Can you give me a quick Java reference?

                     

                    I want to include in the java response, the name of the parent class. (it's not like that by default)

                    • 7. Re: Easy way to implement Jackson in Jboss AS7
                      rcd

                      You should check the Jackson documentation, but I don't think there's any built-in mechanism for doing what you want. I'm not sure why you want to include the name of the parent class, but I've never encountered a use case where that would be necessary or useful. What are you trying to accomplish?

                      • 8. Re: Easy way to implement Jackson in Jboss AS7
                        jacktrades

                        See this:

                        http://datatables.net/release-datatables/examples/data_sources/js_array.html

                         

                        Actual JSON output:

                        [{"deleted":false,"deletedDate":null,"version":0,"brand":"Mack","plate":"111","id":1},{"deleted":false,"deletedDate":null,"version":0,"brand":"Scania","plate":"222","id":2},{"deleted":false,"deletedDate":null,"version":0,"brand":"Volvo","plate":"333","id":3}]

                         

                        JSON outputs misses 'parent name', in the link above: 'aadata', consequently there is an error parsing the data.

                        • 9. Re: Easy way to implement Jackson in Jboss AS7
                          rcd

                          The easiest way to do what you want is probably to just add a getClassName() method to the class you're serializing. I think Jackson should automatically pick it up and include it in the JSON; if not, you probably just need to add an annotation to tell Jackson to do so.

                          • 10. Re: Easy way to implement Jackson in Jboss AS7
                            jacktrades

                            So, my thinking in switching to Jackson is not too bad, thanks.

                             

                            On my  present situation, (non Maven project), Could you give me more details on how to switch to jackson? Sorry I'm a newbie in Jboss.

                            • 11. Re: Easy way to implement Jackson in Jboss AS7
                              rcd

                              It depends. The easiest way is to just bundle the Jackson JARs (including the JAX-RS provider) with your WAR. In a non-Maven project, the only other option I can think of is to use Dependencies or jboss-deployment-structure.xml to get JBoss to add the necessary modules for you, but this has the disadvantages that the Jackson classes aren't on your classpath (so you can't use any Jackson annotations) and you're stuck with whatever version of Jackson ships with JBoss (the lack of control could cause problems later if the version changes). In my project, I opted to bundle the JARs to avoid these problems.

                               

                              If you had a Maven project, you'd have a third option that combines the above options: JBoss adds the module for you at run-time, and you get it on the classpath at compile-time, using provided scope so it doesn't get bundled at deployment. I'd strongly recommend attempting to Mavenize your project. A year ago, I wasn't using Maven and avoided it as much as possible. Today, I couldn't get by without it. It has a bit of a learning curve to it, but it's a tool well worth learning to use, in my opinion.

                               

                              Outside of all that, Jackson has solid default behavior for serializing and deserializing POJOs. It will probably do most or all of what you want out of the box with zero configuration.

                              • 12. Re: Easy way to implement Jackson in Jboss AS7
                                jacktrades

                                Then I will try to make a Maven project out of this.

                                 

                                Please give me specific guidelines for Eclipse Juno to switch to Jackson.

                                • 13. Re: Easy way to implement Jackson in Jboss AS7
                                  rcd

                                  You don't need to do anything for Eclipse, or at least I didn't. Just get the Jackson classes into your deployment one way or the other. If you Mavenize the project, all you need to do is to add the Jackson dependencies in your pom.xml. Like I said before, most things will probably work without needing to configure Jackson at all in your code, but obviously you should test all of your web service methods to ensure that you aren't hitting any edge cases.

                                  • 14. Re: Easy way to implement Jackson in Jboss AS7
                                    jacktrades

                                    I'll keep you updated.

                                    1 2 Previous Next