0

I am running on JBoss 6.1.4 and was using Jackson V1. After changing my dependencies to reference version 2.4.4 (and changing all includes to fasterxml versions), it is like the @JsonSerialize is being ignored. Instead of a String (from the serializer), it is returning the class as JSON that should have been serialized. Logging and System.out inside the serializer is not showing up.

I made no changes to code other than changing includes to use fasterxml.

public class HiDateSerializer extends JsonSerializer<HiDate> {

    @Override
    public void serialize(final HiDate value, final JsonGenerator gen, final SerializerProvider sp) throws IOException, JsonProcessingException {
        if (value == null || value.isNull()) {
            gen.writeString("");
        } else {
            gen.writeString(value.fmt());
        }
     }
 }

Then in my model class, I use annotation for on all HiDate attributes:

@JsonSerialize(using = HiDateSerializer.class)
private HiDate dob;

Here is my dependency:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>

What I was getting previously was either an empty string or a string with just the date. However, now I am getting a JSON representation of the entire HiDate class. It is as if the @JsonSerialize is not being honored anymore.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
Jay Witherspoon
  • 165
  • 1
  • 11
  • Did you change packages from `org.codehaus.*`? – Michał Ziober Feb 15 '19 at 21:07
  • thanks. I did change all the imports from codehaus to fasterxml (that was fun) and in fact removed the codehaus dependency. I have verified that only fasterxml jars are in the ear. – Jay Witherspoon Feb 15 '19 at 21:27
  • I am not sure, because I can not check it, but it looks like you have class loader problem. `JBoss` probably still uses `codehaus` implementation which is included to `JBoss` installation. Your web app probably by default uses `codehause` mapper and you need to define and register mapper from `fasterxml` in your app manually. – Michał Ziober Feb 16 '19 at 07:26
  • A classloader issue does make sense, but it sounds like you are not describing changing the classpath but programatically defining and registering a mapper so that it basically ignores the path? – Jay Witherspoon Feb 17 '19 at 16:28
  • Exactly. In your app you need to define `ObjectMapper` from `fasterxml` and this should fix the problem. – Michał Ziober Feb 17 '19 at 19:31
  • I have read many examples of using the ObjectMapper and either I am misunderstanding or it is not going to give the expected results. Previously the @JsonSerialize would convert to a String "under the covers" and the class still had the HiDate in it. Either I am not understanding it, or using the ObjectMapper would have me change the class and replace HiDate with a String type. In my example above, where would the ObjectMapper registration be done? – Jay Witherspoon Feb 19 '19 at 13:36
  • You use `JBOSS` so your app is probably is web app. Do you use `Spring` or something else? – Michał Ziober Feb 19 '19 at 13:48
  • Yes, it is a web app and we are not using Spring or any other framework. – Jay Witherspoon Feb 19 '19 at 18:18
  • But you need to handle request somehow. What do you use for handling `HTTPRequest`s and serving pages? – Michał Ziober Feb 19 '19 at 18:28
  • Rest service running on JBoss EAP using stateless session beans. – Jay Witherspoon Feb 19 '19 at 18:59
  • I have discovered that while the Jackson jars are in my ear file, they are not used once it is installed... By default, JBoss uses their own json classes and ignores the ones in the ear. That certainly is not helping! Investigating how to exclude the JBoss jars and use what is in my ear... – Jay Witherspoon Feb 21 '19 at 13:09
  • I wrote this in my second comment: "JBoss probably still uses codehaus implementation which is included to JBoss installation". Have you looked on URL-es in my answer? – Michał Ziober Feb 21 '19 at 13:33

1 Answers1

0

I guess, you use default ResteasyJacksonProvider which loads classes from org.codehaus.*. You need to register ResteasyJackson2Provider which uses classes from com.fasterxml.jackson.* packages.

See also:

  1. jackson jboss eap6.1.1 annotations not working
  2. Chapter 21. JSON Support via Jackson
  3. JBoss resteasy - Custom Jackson provider
  4. Using resteasy-jackson2-provider instead of resteasy-jackson-provider
  5. jackson-jaxrs-providers
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Thank you for the links. Finding out that it was not using my jars was a big help. I have upgraded my resteasy on the server to the latest 3.6.3.Final and included that same level in my app, so I believe the versions should match. I am also including resteasy in my app instead of jackson. So all code should be the same. It is now working sometimes, which is really strange. I am using the same @JsonSerialize(using = HiDateSerializer.class) for all the classes. Previously I was always getting an object instead of the String. I am trying to determine for failure. – Jay Witherspoon Feb 22 '19 at 16:45
  • @JayWitherspoon, it sometimes work? That's strange! Are you sure your `Jackson2Provider` is used in all cases? Maybe there is wrong header in request or something like that? If you can, please, extend question with new data. Maybe we could help you somehow. In case it would be another kind of question you can always create new question and describe problem. Original question probably already is outdated. – Michał Ziober Feb 22 '19 at 17:34