3

I want to upgrade my jersey version to 2.x from 1.x. In my code I had:

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private static final Class<?>[] classes = {
        A.class, B.class, C.class, D.class, E.class,
        F.class, G.class
};
private JAXBContext context;

public JAXBContextResolver() throws Exception {
    context = new JSONJAXBContext(JSONConfiguration.natural()
            .humanReadableFormatting(true).rootUnwrapping(true).build(),
            classes);
}

public JAXBContext getContext(Class<?> objectType) {
    return context;
}
}

But JSONJAXBContext and JSONConfiguration are not defined in jersey 2.x. How can I make the change accordingly?

The question Where did JSONConfiguration go in Jersey 2.5.x? is not answering my question because it does not explain how do I add my class which I want to return as output

Community
  • 1
  • 1
Shiran Maor
  • 309
  • 2
  • 5
  • 18
  • Possible duplicate of [Where did JSONConfiguration go in Jersey 2.5.x?](http://stackoverflow.com/questions/21220760/where-did-jsonconfiguration-go-in-jersey-2-5-x) – Hendrik Jander Jan 11 '16 at 20:30
  • @jah not too helpful since it looks like using completely different approach – Shiran Maor Jan 11 '16 at 20:35

1 Answers1

1

There is no need for this. You either are going to use MOXy or Jackson as your JSON provider in Jersey 2.x. For the latter, you configure with MoxyJsonConfig. For Jackson, you use ObjectMapper. Figure out which provider you are using, and configure the according object. Both can be configured in a ContextResolver like you're currently doing.

As far as your current configurations

  1. You won't need to configure any classes with either of these.
  2. Unwrapped objects are serialized by default.
  3. And to pretty print you would do the following

    Jackson

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    

    MOXy

    MoxyJsonConfig config = new MoxyJsonConfig()
            .setFormattedOutput(true);
    
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • What do you mean by Unwrapped objects? So basicly I can use this example ObjectMapperProvider and I am good? – Shiran Maor Jan 12 '16 at 17:46
  • `{ "ObjectType": { "key":"value"}}` == wrapped root value. `{"key":"value"}` == not wrapped. I see you have a configuration to unwrap the root value, that's why I mentioned it. The native implementation for Jersey 1.x was to wrap it. – Paul Samsotha Jan 13 '16 at 02:43
  • But anyway, looking at your other question, it looks like you are using jettison as the provider. I am not quite sure how to configure that, and I would actually recommend using Jackson instead. You can change the `jersey-media-json-jettison` to `jersey-media-json-jackson`, then yes, you can use a `ContextResolver` – Paul Samsotha Jan 13 '16 at 02:45