0

I am curious to know which JSON marshalling/unmarshalling framework is used by Glassfish/Payara is case of JAX-RS and how I can add custom JSON mapper class into it.

I would like to write a custom serializer for my Enum class. I use provided scope in my pom.xml for jaxrs javaee-api 7.0 so default Glassfish libraries are used.

I tried to use @JsonValue and wrote a class which implements javax.ws.rs.ext.MessageBodyWriter and JsonSerializer<T> as well. Do not work either as I expect.

This is my enum class:

public enum ErrorCode {
    MY_ERROR(123456);

    private int value;

    ErrorCode(final int value) {
        this.value = value;
    }

    @JsonValue
    public int  getValue() {
        return value;
    }
}

The class which uses enum:

public class ErrorInfo {
    private ErrorCode errorCode;

    public String toJson() {
        try {
            return new ObjectMapper().writer().withDefaultPrettyPrinter().writeValueAsString(this);
        } catch (JsonProcessingException e) {
            // TODO: do something here...
        }
    }
}

And the JAX-RS class where I would like to send back the ErrorInfo instance as a JSON:

@Provider
public class MyExceptionMapper implements ExceptionMapper<Throwable> {
    @Override
    public Response toResponse(Throwable throwable) {
        ...
        return Response
                .status(errorInfo.getHttpStatus())
                .type(ExtendedMediaType.APPLICATION_JSON_UTF8)
                .entity(errorInfo)
                .build();
    }
}

If I use the code above then the errorCode value is "MY_ERROR" string instead of the int 123456 value.

If I use my extra errorInfo.toJson() method then @JsonValue annotation does the magic but I would like to avoid writing extra code to handle enum serialization issue.

What is the proper way to configure / add extra enum mapper class to the default JAX-RS JSON library in Glassfish/Payara?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
zappee
  • 20,148
  • 14
  • 73
  • 129

1 Answers1

0

By default, Payara Server uses MOXy to map to/from JSON. You can use an alternative like Jackson, if you add Jackson to your app and add JacksonFeature into JAX-RS classes: Force Glassfish4 to use Jackson instead of Moxy

In the upcoming Payara 5, which will support Java EE 8, JSON marshalling will be handled in a standard way prescribed by the JSON-Binding

OndroMih
  • 7,280
  • 1
  • 26
  • 44
  • Do you know how can I enable Jackson? I added org.glassfish.jersey.media:jersey-media-json-jackson dependency to my pom but I get org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001334: Unsatisfied dependencies for type ObjectProvider with qualifiers @Default – zappee Oct 09 '17 at 17:47
  • I linked the answer how to do it in my answer above. Here's the link again: https://stackoverflow.com/questions/18317927/force-glassfish4-to-use-jackson-instead-of-moxy#18318314 – OndroMih Oct 10 '17 at 08:45
  • I have tried that but it does not work for me: https://groups.google.com/forum/?__hssc=229474563.7.1507570987948&__hstc=229474563.f77c21f0c7f2189af2c0f0d9b585b87f.1497363477167.1507219438875.1507570987948.4&__hsfp=2561377333&hsCtaTracking=ee96a587-4450-4296-b986-ac67f6da0574%7Cff3c7a3b-1cde-4e52-b2ad-e162643b9683#!topic/payara-forum/p7JlVgTc7EI – zappee Oct 10 '17 at 08:55
  • @zappee i had the same issue but i was lucky enough that i had an old project that works. what causes UnsatisfiedResolutionException for me was jersey-media-json-jackson version 2.26 i changed it back to 2.10.4 and i no longer have this Exception – youssef Nov 14 '17 at 14:35