简体   繁体   English

自定义Json在Glassfish / Payara上与JAX-RS编组

[英]Custom Json marshalling with JAX-RS on Glassfish/Payara

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. 我很想知道Glassfish / Payara使用哪种json编组/解组框架是JAX-RS的情况,以及如何向其中添加自定义json映射器类。

I would like to write a custom serializer for my Enum class. 我想为我的Enum类编写一个自定义序列化程序。 I use provided scope in my pom.xml for jaxrs javaee-api 7.0 so default Glassfish libraries are used. 我将pom.xml中provided范围用于jaxrs javaee-api 7.0,因此使用默认的Glassfish库。

I tried to use @JsonValue and wrote a class wich implements javax.ws.rs.ext.MessageBodyWriter and JsonSerializer<T> as well. 我尝试使用@JsonValue并编写了一个实现javax.ws.rs.ext.MessageBodyWriterJsonSerializer<T>的类。 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: 我想将ErrorInfo实例作为json发送回去的JAX-RS类:

@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. 如果我使用上面的代码,则errorCode值为“ MY_ERROR”字符串,而不是int 123456值。

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. 如果我使用额外的errorInfo.toJson()方法,则@JsonValue注释确实具有魔力,但我想避免编写额外的代码来处理枚举序列化问题。

What is the proper way to configure / add extra enum mapper class to the default JAX-RS json library in Glassfish/Payara? 在Glassfish / Payara中将默认的JAX-RS json库配置/添加额外的枚举映射器类的正确方法是什么?

By default, Payara Server uses MOXy to map to/from JSON. 默认情况下,Payara Server使用MOXy映射到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 如果将Jackson添加到应用程序并将JacksonFeature添加到JAX-RS类,则可以使用Jackson的替代方法强制Glassfish4使用Jackson而不是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 在即将支持Java EE 8的Payara 5中,将按照JSON-Binding规定的标准方式处理JSON编组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM