Bug 8622: convert sal-rest-docgen to Jackson
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / jaxrs / JaxbContextResolver.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.sal.rest.doc.jaxrs;
9
10 import com.fasterxml.jackson.annotation.JsonInclude;
11 import com.fasterxml.jackson.databind.ObjectMapper;
12 import javax.ws.rs.Consumes;
13 import javax.ws.rs.Produces;
14 import javax.ws.rs.core.MediaType;
15 import javax.ws.rs.ext.ContextResolver;
16 import javax.ws.rs.ext.Provider;
17 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
18
19 @Provider
20 @Consumes(MediaType.APPLICATION_JSON)
21 @Produces(MediaType.APPLICATION_JSON)
22 public class JaxbContextResolver implements ContextResolver<ObjectMapper> {
23
24     private final ObjectMapper ctx;
25
26     public JaxbContextResolver() {
27         ctx = new ObjectMapper();
28         ctx.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.ALWAYS);
29     }
30
31     @Override
32     public ObjectMapper getContext(final Class<?> klass) {
33         if (ApiDeclaration.class.isAssignableFrom(klass)) {
34             return ctx;
35         }
36
37         return null; // must return null so that JAX-RS can continue context search
38     }
39 }