java类javax.ws.rs.ext.ContextResolver的实例源码

JacksonJsonPropertyBoxProvider.java 文件源码 项目:holon-json 阅读 24 收藏 0 点赞 0 评论 0
/**
 * Get the {@link PropertySetRefIntrospector} instance to use.
 * @return The {@link PropertySetRefIntrospector} instance to use, from {@link ContextResolver} if available or the
 *         default one
 */
private PropertySetRefIntrospector getPropertySetRefIntrospector() {
    if (propertySetRefIntrospector == null) {
        // init using a contextresolver, if available
        ContextResolver<PropertySetRefIntrospector> contextResolver = providers
                .getContextResolver(PropertySetRefIntrospector.class, MediaType.APPLICATION_JSON_TYPE);
        if (contextResolver != null) {
            propertySetRefIntrospector = contextResolver.getContext(PropertySetRefIntrospector.class);
        }
        if (propertySetRefIntrospector == null) {
            // use default
            propertySetRefIntrospector = PropertySetRefIntrospector.getDefault();
        }
    }
    return propertySetRefIntrospector;
}
GsonJsonProvider.java 文件源码 项目:holon-json 阅读 24 收藏 0 点赞 0 评论 0
/**
 * Get the {@link Gson} instance to use.
 * @return The {@link Gson} instance to use, from {@link ContextResolver} if available or the default one
 */
private Gson getGson() {
    if (gson == null) {
        // init using a contextresolver, if available
        ContextResolver<Gson> contextResolver = providers.getContextResolver(Gson.class,
                MediaType.APPLICATION_JSON_TYPE);
        if (contextResolver != null) {
            gson = contextResolver.getContext(Gson.class);
        }
        if (gson == null) {
            // use default
            gson = GsonConfiguration.builder().create();
        }
    }
    return gson;
}
GsonJsonProvider.java 文件源码 项目:holon-json 阅读 27 收藏 0 点赞 0 评论 0
/**
 * Get the {@link PropertySetRefIntrospector} instance to use.
 * @return The {@link PropertySetRefIntrospector} instance to use, from {@link ContextResolver} if available or the
 *         default one
 */
private PropertySetRefIntrospector getPropertySetRefIntrospector() {
    if (propertySetRefIntrospector == null) {
        // init using a contextresolver, if available
        ContextResolver<PropertySetRefIntrospector> contextResolver = providers
                .getContextResolver(PropertySetRefIntrospector.class, MediaType.APPLICATION_JSON_TYPE);
        if (contextResolver != null) {
            propertySetRefIntrospector = contextResolver.getContext(PropertySetRefIntrospector.class);
        }
        if (propertySetRefIntrospector == null) {
            // use default
            propertySetRefIntrospector = PropertySetRefIntrospector.getDefault();
        }
    }
    return propertySetRefIntrospector;
}
ExampleAuth.java 文件源码 项目:holon-jaxrs 阅读 19 收藏 0 点赞 0 评论 0
public void configureJaxrsApplication() {

        AccountProvider provider = id -> { // <5>
            // a test provider wich always returns an Account with given id and s3cr3t as password
            return Optional.ofNullable(Account.builder(id).credentials(Credentials.builder().secret("s3cr3t").build())
                    .enabled(true).build());
        };

        Realm realm = Realm.builder() // <6>
                .resolver(AuthenticationToken.httpBasicResolver()) // <7>
                .authenticator(Account.authenticator(provider)) // <8>
                .withDefaultAuthorizer().build();

        ContextResolver<Realm> realmContextResolver = new ContextResolver<Realm>() { // <9>

            @Override
            public Realm getContext(Class<?> type) {
                return realm;
            }
        };

        register(realmContextResolver); // <10>
    }
PropertyBoxFormDataProvider.java 文件源码 项目:holon-jaxrs 阅读 26 收藏 0 点赞 0 评论 0
/**
 * Get the {@link PropertySetRefIntrospector} instance to use.
 * @return The {@link PropertySetRefIntrospector} instance to use, from {@link ContextResolver} if available or the
 *         default one
 */
private PropertySetRefIntrospector getPropertySetRefIntrospector() {
    if (_propertySetRefIntrospector == null) {
        // init using a contextresolver, if available
        ContextResolver<PropertySetRefIntrospector> contextResolver = providers
                .getContextResolver(PropertySetRefIntrospector.class, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
        if (contextResolver != null) {
            _propertySetRefIntrospector = contextResolver.getContext(PropertySetRefIntrospector.class);
        }
        if (_propertySetRefIntrospector == null) {
            // use default
            _propertySetRefIntrospector = PropertySetRefIntrospector.getDefault();
        }
    }
    return _propertySetRefIntrospector;
}
PropertyBoxFormDataProvider.java 文件源码 项目:holon-jaxrs 阅读 23 收藏 0 点赞 0 评论 0
/**
 * Get the {@link FormDataPropertyValueSerializer} instance to use.
 * @return The {@link FormDataPropertyValueSerializer} instance to use, from {@link ContextResolver} if available or
 *         the default one
 */
private FormDataPropertyValueSerializer getSerializer() {
    if (_serializer == null) {
        // init using a contextresolver, if available
        ContextResolver<FormDataPropertyValueSerializer> contextResolver = providers.getContextResolver(
                FormDataPropertyValueSerializer.class, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
        if (contextResolver != null) {
            _serializer = contextResolver.getContext(FormDataPropertyValueSerializer.class);
        }
        if (_serializer == null) {
            // use default
            _serializer = FormDataPropertyValueSerializer.getDefault();
        }
    }
    return _serializer;
}
PropertyBoxFormDataProvider.java 文件源码 项目:holon-jaxrs 阅读 27 收藏 0 点赞 0 评论 0
/**
 * Get the {@link FormDataPropertyValueDeserializer} instance to use.
 * @return The {@link FormDataPropertyValueDeserializer} instance to use, from {@link ContextResolver} if available
 *         or the default one
 */
private FormDataPropertyValueDeserializer getDeserializer() {
    if (_deserializer == null) {
        // init using a contextresolver, if available
        ContextResolver<FormDataPropertyValueDeserializer> contextResolver = providers.getContextResolver(
                FormDataPropertyValueDeserializer.class, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
        if (contextResolver != null) {
            _deserializer = contextResolver.getContext(FormDataPropertyValueDeserializer.class);
        }
        if (_deserializer == null) {
            // use default
            _deserializer = FormDataPropertyValueDeserializer.getDefault();
        }
    }
    return _deserializer;
}
ResourceUtils.java 文件源码 项目:holon-jaxrs 阅读 30 收藏 0 点赞 0 评论 0
/**
 * Lookup a resource of given <code>resourceType</code> type, either using a suitable {@link ContextResolver} if
 * <code>providers</code> is not <code>null</code> and a {@link ContextResolver} for given <code>resourceType</code>
 * is available, or trying to obtain the resource from {@link Context} using given <code>resourceType</code> as
 * context resource key.
 * @param <R> Resource type
 * @param caller Caller class
 * @param resourceType Resource type to lookup (not null)
 * @param providers JAX-RS {@link Providers}, if available
 * @return Resource instance, or an empty optional if not available
 */
public static <R> Optional<R> lookupResource(Class<?> caller, Class<R> resourceType, Providers providers) {
    ObjectUtils.argumentNotNull(resourceType, "Resource type must be not null");

    R resource = null;

    // try to use a ContextResolver, if available
    ContextResolver<R> resolver = providers.getContextResolver(resourceType, null);
    if (resolver != null) {
        resource = resolver.getContext(caller);
    }

    if (resource == null) {
        // lookup in context
        resource = Context.get()
                .resource(resourceType,
                        (caller != null) ? caller.getClassLoader() : ClassUtils.getDefaultClassLoader())
                .orElse(null);
    }

    return Optional.ofNullable(resource);
}
DefaultProviderFilter.java 文件源码 项目:geeMVC-Java-MVC-Framework 阅读 18 收藏 0 点赞 0 评论 0
protected List<ContextResolver<?>> sortedList(Map<ProviderKey, ContextResolver<?>> providerMap, MediaType targetMediaType) {
    List<ContextResolver<?>> contextResolvers = new ArrayList<>(providerMap.values());

    Collections.sort(contextResolvers, new Comparator<ContextResolver<?>>() {
        public int compare(ContextResolver<?> cr1, ContextResolver<?> cr2) {

            Set<String> providerMediaTypes1 = mediaTypes(cr1.getClass().getAnnotationsByType(Produces.class));
            Set<String> providerMediaTypes2 = mediaTypes(cr2.getClass().getAnnotationsByType(Produces.class));

            int score1 = relevanceScore(providerMediaTypes1, targetMediaType);
            int score2 = relevanceScore(providerMediaTypes2, targetMediaType);

            if (score1 == score2)
                return 0;

            return (score2 - score1) < 0 ? -1 : 1;
        }
    });

    return contextResolvers;
}
LinksRequestResource.java 文件源码 项目:oreva 阅读 19 收藏 0 点赞 0 评论 0
@POST
public Response createLink(@Context HttpHeaders httpHeaders,
    @Context UriInfo uriInfo,
    @Context ContextResolver<ODataProducer> producerResolver,
    @Context SecurityContext securityContext,
    String payload) {
  log.info(String.format(
      "createLink(%s,%s,%s,%s)",
      sourceEntity.getEntitySetName(),
      sourceEntity.getEntityKey(),
      targetNavProp,
      targetEntityKey));

  ODataProducer producer = producerResolver.getContext(ODataProducer.class);

  OEntityId newTargetEntity = parseRequestUri(httpHeaders, uriInfo, payload);
  producer.createLink(ODataContextImpl.builder().aspect(httpHeaders).aspect(securityContext).build(), sourceEntity, targetNavProp, newTargetEntity);
  return noContent();
}
LinksRequestResource.java 文件源码 项目:oreva 阅读 18 收藏 0 点赞 0 评论 0
@PUT
public Response updateLink(@Context HttpHeaders httpHeaders,
    @Context UriInfo uriInfo,
    @Context ContextResolver<ODataProducer> producerResolver,
    @Context SecurityContext securityContext,
    String payload) {
  log.info(String.format(
      "updateLink(%s,%s,%s,%s)",
      sourceEntity.getEntitySetName(),
      sourceEntity.getEntityKey(),
      targetNavProp,
      targetEntityKey));

  ODataProducer producer = producerResolver.getContext(ODataProducer.class);

  OEntityId newTargetEntity = parseRequestUri(httpHeaders, uriInfo, payload);
  producer.updateLink(ODataContextImpl.builder().aspect(httpHeaders).aspect(securityContext).build(), sourceEntity, targetNavProp, targetEntityKey, newTargetEntity);
  return noContent();
}
LinksRequestResource.java 文件源码 项目:oreva 阅读 19 收藏 0 点赞 0 评论 0
@DELETE
public Response deleteLink(@Context HttpHeaders httpHeaders,
    @Context UriInfo uriInfo,
    @Context ContextResolver<ODataProducer> producerResolver,
    @Context SecurityContext securityContext) {
  log.info(String.format(
      "deleteLink(%s,%s,%s,%s)",
      sourceEntity.getEntitySetName(),
      sourceEntity.getEntityKey(),
      targetNavProp,
      targetEntityKey));

  ODataProducer producer = producerResolver.getContext(ODataProducer.class);

  producer.deleteLink(ODataContextImpl.builder().aspect(httpHeaders).aspect(securityContext).build(), sourceEntity, targetNavProp, targetEntityKey);
  return noContent();
}
ServiceDocumentResource.java 文件源码 项目:oreva 阅读 18 收藏 0 点赞 0 评论 0
@GET
@Produces({ ODataConstants.APPLICATION_XML_CHARSET_UTF8, ODataConstants.TEXT_JAVASCRIPT_CHARSET_UTF8, ODataConstants.APPLICATION_JAVASCRIPT_CHARSET_UTF8 })
public Response getServiceDocument(
    @Context HttpHeaders httpHeaders,
    @Context UriInfo uriInfo,
    @Context ContextResolver<ODataProducer> producerResolver,
    @QueryParam("$format") String format,
    @QueryParam("$callback") String callback) {

  ODataProducer producer = producerResolver.getContext(ODataProducer.class);

  EdmDataServices metadata = producer.getMetadata();

  StringWriter w = new StringWriter();
  FormatWriter<EdmDataServices> fw = FormatWriterFactory.getFormatWriter(EdmDataServices.class, httpHeaders.getAcceptableMediaTypes(), format, callback);
  fw.write(uriInfo, w, metadata);

  return Response.ok(w.toString(), fw.getContentType())
      .header(ODataConstants.Headers.DATA_SERVICE_VERSION, ODataConstants.DATA_SERVICE_VERSION_HEADER)
      .build();
}
ODataBatchPutUnit.java 文件源码 项目:oreva 阅读 20 收藏 0 点赞 0 评论 0
@Override
protected Response delegate(HttpHeaders httpHeaders, URI baseUri, ContextResolver<ODataProducer> producerResolver) throws Exception {

  ODataProducer producer = producerResolver.getContext(ODataProducer.class);
  OEntity entity = new BatchRequestResource().getRequestEntity(httpHeaders, getResourceHeaders(),
      getUriInfo(),
      getResourceContent(),
      producer.getMetadata(),
      getEnitySetName(),
      OEntityKey.parse(getEntityKey()), false);
  producer.updateEntity(null, getEnitySetName(), entity);
  Response response = Response.ok().header(ODataConstants.Headers.DATA_SERVICE_VERSION, ODataConstants.DATA_SERVICE_VERSION_HEADER)
      .header(ODataConstants.Headers.CONTENT_TYPE, getResourceHeaders().getFirst(ODataConstants.Headers.CONTENT_TYPE))
      .header(ODataConstants.Headers.CONTENT_LENGTH, 0).build();

  return response;
}
ODataBatchMergeUnit.java 文件源码 项目:oreva 阅读 22 收藏 0 点赞 0 评论 0
@Override
protected Response delegate(HttpHeaders httpHeaders, URI baseUri, ContextResolver<ODataProducer> producerResolver) throws Exception {
  ODataProducer producer = producerResolver.getContext(ODataProducer.class);
  OEntity entity = new BatchRequestResource().getRequestEntity(httpHeaders, getResourceHeaders(),
      getUriInfo(),
      getResourceContent(),
      producer.getMetadata(),
      getEnitySetName(),
      OEntityKey.parse(getEntityKey()), false);
  producer.mergeEntity(null, getEnitySetName(), entity);
  Response response = Response.ok().header(ODataConstants.Headers.DATA_SERVICE_VERSION, ODataConstants.DATA_SERVICE_VERSION_HEADER)
      .header(ODataConstants.Headers.CONTENT_TYPE, getResourceHeaders().getFirst(ODataConstants.Headers.CONTENT_TYPE))
      .header(ODataConstants.Headers.CONTENT_LENGTH, 0).build();

  return response;
}
EntityRequestResource.java 文件源码 项目:oreva 阅读 21 收藏 0 点赞 0 评论 0
@GET
@Produces({ ODataConstants.APPLICATION_ATOM_XML_CHARSET_UTF8, ODataConstants.TEXT_JAVASCRIPT_CHARSET_UTF8, ODataConstants.APPLICATION_JAVASCRIPT_CHARSET_UTF8 })
public Response getEntity(@Context HttpHeaders httpHeaders,
    @Context UriInfo uriInfo,
    @Context ContextResolver<ODataProducer> producerResolver,
    @Context SecurityContext securityContext,
    @PathParam("entitySetName") String entitySetName,
    @PathParam("id") String id,
    @QueryParam("$format") String format,
    @QueryParam("$callback") String callback,
    @QueryParam("$expand") String expand,
    @QueryParam("$select") String select) {

  ODataProducer producer = producerResolver.getContext(ODataProducer.class);
  return getEntityImpl(httpHeaders, uriInfo, securityContext, producer, entitySetName, id, format, callback, expand, select);
}
ValidatingMessageBodyReader.java 文件源码 项目:opennmszh 阅读 21 收藏 0 点赞 0 评论 0
public T readFrom(final Class<T> clazz, final Type type, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> parameters, final InputStream stream) throws IOException, WebApplicationException {
    LogUtils.debugf(this, "readFrom: %s/%s/%s", clazz.getSimpleName(), type, mediaType);

    JAXBContext jaxbContext = null;
    final ContextResolver<JAXBContext> resolver = providers.getContextResolver(JAXBContext.class, mediaType);
    try {

        if (resolver != null) {
            jaxbContext = resolver.getContext(clazz);
        }

        if (jaxbContext == null) {
            jaxbContext = JAXBContext.newInstance(clazz);

        }

        return JaxbUtils.unmarshal(clazz, new InputSource(stream), jaxbContext);

    } catch (final JAXBException e) {
        LogUtils.warnf(this, e, "An error occurred while unmarshaling a %s object", clazz.getSimpleName());
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
RESTEasyConnector.java 文件源码 项目:openstack-maven-CIET-students 阅读 20 收藏 0 点赞 0 评论 0
public OpenStackProviderFactory() {
    super();

    addContextResolver(new ContextResolver<ObjectMapper>() {
        public ObjectMapper getContext(Class<?> type) {
            return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
        }
    });

    jsonProvider = new JacksonJsonProvider();
    addMessageBodyReader(jsonProvider);
    addMessageBodyWriter(jsonProvider);

    streamProvider = new InputStreamProvider();
    addMessageBodyReader(streamProvider);
    addMessageBodyWriter(streamProvider);
}
Contexts.java 文件源码 项目:tomee 阅读 20 收藏 0 点赞 0 评论 0
private static Set<Class<?>> contextClasses() {
    final Set<Class<?>> classes = new HashSet<>(); classes.add(UriInfo.class);
    classes.add(SecurityContext.class);
    classes.add(HttpHeaders.class);
    classes.add(ContextResolver.class);
    classes.add(Providers.class);
    classes.add(Request.class);
    /* TODO: when we have jaxrs 2
    classes.add(ResourceInfo.class);
    classes.add(ResourceContext.class);
    */
    classes.add(Application.class);
    classes.add(HttpServletRequest.class);
    classes.add(HttpServletResponse.class);
    classes.add(ServletConfig.class);
    classes.add(ServletContext.class);
    classes.add(MessageContext.class);
    return classes;
}
ValidatingMessageBodyReader.java 文件源码 项目:OpenNMS 阅读 22 收藏 0 点赞 0 评论 0
public T readFrom(final Class<T> clazz, final Type type, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> parameters, final InputStream stream) throws IOException, WebApplicationException {
    LogUtils.debugf(this, "readFrom: %s/%s/%s", clazz.getSimpleName(), type, mediaType);

    JAXBContext jaxbContext = null;
    final ContextResolver<JAXBContext> resolver = providers.getContextResolver(JAXBContext.class, mediaType);
    try {

        if (resolver != null) {
            jaxbContext = resolver.getContext(clazz);
        }

        if (jaxbContext == null) {
            jaxbContext = JAXBContext.newInstance(clazz);

        }

        return JaxbUtils.unmarshal(clazz, new InputSource(stream), jaxbContext);

    } catch (final JAXBException e) {
        LogUtils.warnf(this, e, "An error occurred while unmarshaling a %s object", clazz.getSimpleName());
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
JerseyAuthAutoConfiguration.java 文件源码 项目:holon-jaxrs 阅读 22 收藏 0 点赞 0 评论 0
@Override
public void customize(ResourceConfig config) {
    // Realm ContextResolver
    config.register(new ContextResolver<Realm>() {

        @Override
        public Realm getContext(Class<?> type) {
            return realm;
        }
    });
    // Authentication
    config.register(AuthenticationFeature.class);
    // Authorization
    config.register(RolesAllowedDynamicFeature.class);
}
TestAuthzStd.java 文件源码 项目:holon-jaxrs 阅读 18 收藏 0 点赞 0 评论 0
@Override
protected Application configure() {

    final AccountProvider provider = id -> {
        Account account = null;
        if ("a1".equals(id)) {
            account = Account.builder(id).credentials(Credentials.builder().secret("p1").build()).enabled(true)
                    .permission("R1").permission("R2").build();
        } else if ("a2".equals(id)) {
            account = Account.builder(id).credentials(Credentials.builder().secret("p2").build()).enabled(true)
                    .permission("R1").permission("R3").build();
        }
        return Optional.ofNullable(account);
    };

    final Realm realm = Realm.builder().resolver(AuthenticationToken.httpBasicResolver())
            .authenticator(Account.authenticator(provider)).withDefaultAuthorizer().build();

    return new ResourceConfig()
            .register(RolesAllowedDynamicFeature.class)
            .register(new ContextResolver<Realm>() {

                @Override
                public Realm getContext(Class<?> type) {
                    return realm;
                }
            }).register(ProtectedResource.class).register(SemiProtectedResource.class)
            .register(PublicResource.class);
}
TestAuth.java 文件源码 项目:holon-jaxrs 阅读 25 收藏 0 点赞 0 评论 0
@Override
protected Application configure() {

    final AccountProvider provider = id -> {
        Account account = null;
        if ("a1".equals(id)) {
            account = Account.builder(id).credentials(Credentials.builder().secret("p1").build()).enabled(true)
                    .permission("R1").permission("R2").build();
        } else if ("a2".equals(id)) {
            account = Account.builder(id).credentials(Credentials.builder().secret("p2").build()).enabled(true)
                    .permission("R1").permission("R3").build();
        }
        return Optional.ofNullable(account);
    };

    final Realm realm = Realm.builder().resolver(AuthenticationToken.httpBasicResolver())
            .authenticator(Account.authenticator(provider)).withDefaultAuthorizer().build();

    return new ResourceConfig()// .register(AuthenticationFeature.class) // using auto-config
            .register(RolesAllowedDynamicFeature.class).register(new ContextResolver<Realm>() {

                @Override
                public Realm getContext(Class<?> type) {
                    return realm;
                }
            }).register(ProtectedResource.class).register(SemiProtectedResource.class)
            .register(PublicResource.class);
}
ObjectMapperProviderTest.java 文件源码 项目:kladr-api-client 阅读 18 收藏 0 点赞 0 评论 0
@Test
public void testSkipUnknownFields() throws IOException {
    final ContextResolver<ObjectMapper> contextResolver = new ObjectMapperProvider();
    final ObjectMapper objectMapper = contextResolver.getContext(ObjectMapper.class);
    final TestEntity testEntity = objectMapper.readValue(JSON, TestEntity.class);
    Assert.assertEquals(TEST_NAME, testEntity.getName());
}
JaxRsResolverTest.java 文件源码 项目:geeMVC-Java-MVC-Framework 阅读 27 收藏 0 点赞 0 评论 0
@Test
public void testLocateGensonContextResolver() {
    Providers providers = instance(Providers.class);

    RuntimeDelegate.setInstance(new DefaultRuntimeDelegate());

    ContextResolver contextResolver = providers.getContextResolver(Genson.class, MediaType.APPLICATION_JSON_TYPE);

    assertNotNull(contextResolver);
    assertEquals(TestGensonContextResolver3.class, contextResolver.getClass());
}
JaxRsResolverTest.java 文件源码 项目:geeMVC-Java-MVC-Framework 阅读 24 收藏 0 点赞 0 评论 0
@Test
public void testLocateObjectContextResolver() {
    Providers providers = instance(Providers.class);

    RuntimeDelegate.setInstance(new DefaultRuntimeDelegate());

    ContextResolver contextResolver = providers.getContextResolver(Person.class, MediaType.APPLICATION_JSON_TYPE);

    assertNotNull(contextResolver);
    assertEquals(TestGensonContextResolver6.class, contextResolver.getClass());
}
JaxRsResolverTest.java 文件源码 项目:geeMVC-Java-MVC-Framework 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void testLocateApplicationContextResolver() {
    Providers providers = instance(Providers.class);

    RuntimeDelegate.setInstance(new DefaultRuntimeDelegate());

    ContextResolver contextResolver = providers.getContextResolver(Application.class, MediaType.APPLICATION_JSON_TYPE);

    assertNotNull(contextResolver);
    assertEquals(TestApplicationContextResolver.class, contextResolver.getClass());
}
JerseyAutoConfiguration.java 文件源码 项目:https-github.com-g0t4-jenkins2-course-spring-boot 阅读 27 收藏 0 点赞 0 评论 0
@Bean
public ResourceConfigCustomizer resourceConfigCustomizer(
        final ObjectMapper objectMapper) {
    addJaxbAnnotationIntrospectorIfPresent(objectMapper);
    return new ResourceConfigCustomizer() {
        @Override
        public void customize(ResourceConfig config) {
            config.register(JacksonFeature.class);
            config.register(new ObjectMapperContextResolver(objectMapper),
                    ContextResolver.class);
        }
    };
}
GsonFeature.java 文件源码 项目:javaone2015-cloudone 阅读 18 收藏 0 点赞 0 评论 0
@Override
public boolean configure(final FeatureContext context) {
    final Configuration config = context.getConfiguration();

    final String jsonFeature = CommonProperties.getValue(config.getProperties(),
            config.getRuntimeType(),
            InternalProperties.JSON_FEATURE,
            JSON_FEATURE,
            String.class);
    // Other JSON providers registered.
    if (!JSON_FEATURE.equalsIgnoreCase(jsonFeature)) {
        return false;
    }

    // Disable other JSON providers.
    String propName = PropertiesHelper.getPropertyNameForRuntime(InternalProperties.JSON_FEATURE,
                                                                 config.getRuntimeType());
    context.property(propName, JSON_FEATURE);

    // Register
    context.register(GsonProvider.class);
    if (gsonBuilder != null) {
        context.register(new ContextResolver<GsonBuilder>() {
            @Override
            public GsonBuilder getContext(Class type) {
                return gsonBuilder;
            }
        });
    }
    return true;
}
GsonProvider.java 文件源码 项目:javaone2015-cloudone 阅读 25 收藏 0 点赞 0 评论 0
@PostConstruct
public void postConstruct() {
    final ContextResolver<GsonBuilder> contextResolver = providers.getContextResolver(GsonBuilder.class, MediaType.APPLICATION_JSON_TYPE);
    if (contextResolver != null) {
        final GsonBuilder builder = contextResolver.getContext(GsonBuilder.class);
        if (builder != null) {
            gson = builder.create();
        }
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号