Java com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider 代码实例

・12 分钟阅读

以下是展示如何使用com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


/**
 * Binds the JacksonJsonProvider to Jersey.
 */
@Provides
@Singleton
JacksonJsonProvider getJacksonJsonProvider(final ObjectMapper objectMapper)
{
    final JacksonJsonProvider provider = new JacksonJsonProvider();
    provider.setMapper(objectMapper);
    return provider;
}
 

实例 2


@Override
protected void configureServlets() {
    bind(SandwichStatsResource.class);
    bind(SandwichMakerResource.class);
    // hook Jersey into Guice Servlet
    bind(GuiceContainer.class);
    // hook Jackson into Jersey as the POJO <-> JSON mapper
    bind(JacksonJsonProvider.class).in(Scopes.SINGLETON);
    Map<String, String> guiceContainerConfig = new HashMap<String, String>();
    guiceContainerConfig.put(ResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES,
        HttpStatusCodeMetricResourceFilterFactory.class.getCanonicalName());
    serve("/*").with(GuiceContainer.class, guiceContainerConfig);
}
 

实例 3


public WebClient client() {
        WebClient webClient = WebClient.create(IntegrationTest.SERVER_URL,
                        Collections.singletonList(new JacksonJsonProvider()));
        WebClient.getConfig(webClient).getRequestContext()
                        .put("use.async.http.conduit", Boolean.TRUE);
        return webClient;
}
 

实例 4


protected void installJackson()
{
    final ObjectMapper mapper = getJacksonProvider().get();
    bind(ObjectMapper.class).toInstance(mapper);
    bind(JacksonJsonProvider.class)
            .toInstance(new JacksonJsonProvider(mapper, new Annotations[]{Annotations.JACKSON, Annotations.JAXB}));
}
 

实例 5


/**
 * Obtain a JerseyClient whit SSL
 * 
 * @return
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public static JerseyClient getJerseyClient(boolean isSSL) {
        ClientBuilder clientBuilder = JerseyClientBuilder.newBuilder().register(MultiPartFeature.class);
        // Create a secure JerseyClient
        if (isSSL) {
                try {
                        HostnameVerifier verifier = new HostnameVerifier() {
                                public boolean verify(String hostname, SSLSession session) {
                                        return true;
                                }
                        };
                        TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
                                public X509Certificate[] getAcceptedIssuers() {
                                        return null;
                                }
                                public void checkServerTrusted(X509Certificate[] chain, String authType)
                                                throws CertificateException {
                                }
                                public void checkClientTrusted(X509Certificate[] chain, String authType)
                                                throws CertificateException {
                                }
                        } };
                        SSLContext sslContext = sslContext = SSLContext.getInstance("SSL");
                        sslContext.init(null, tm, new SecureRandom());
                        clientBuilder.sslContext(sslContext).hostnameVerifier(verifier);
                } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                } catch (KeyManagementException e) {
                        e.printStackTrace();
                }
        }
        return (JerseyClient) clientBuilder.build().register(JacksonJsonProvider.class);
}
 

实例 6


private JacksonJsonProvider getJsonProvider() {
  if (delegate != null) {
    return delegate;
  }
  synchronized (this) {
    if (delegate != null) {
      return delegate;
    }
    for (Object o : application.getSingletons()) {
      if (o instanceof JacksonJsonProvider) {
        delegate = (JacksonJsonProvider) o;
        return delegate;
      }
    }
    delegate = new JacksonJsonProvider();
    return delegate;
  }
}
 

实例 7


@Bean @DependsOn( "cxf" )
public Server jaxRsServer() {
    final JAXRSServerFactoryBean factory = 
        RuntimeDelegate.getInstance().createEndpoint( 
            jaxRsApiApplication(), 
            JAXRSServerFactoryBean.class 
        );
    factory.setServiceBeans( Arrays.< Object >asList( peopleRestService() ) );
    factory.setAddress( factory.getAddress() );
    factory.setInInterceptors( 
        Arrays.< Interceptor< ? extends Message > >asList( 
            new JAXRSBeanValidationInInterceptor()
        ) 
    );
    factory.setOutInterceptors( 
        Arrays.< Interceptor< ? extends Message > >asList( 
            new JAXRSBeanValidationOutInterceptor() 
        ) 
    );
    factory.setProviders( 
        Arrays.asList( 
            new ValidationExceptionMapper(), 
            new JacksonJsonProvider() 
        ) 
    );
    return factory.create();
}
 

实例 8


public ParaClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.register(GenericExceptionMapper.class);
        clientConfig.register(new JacksonJsonProvider(ParaObjectUtils.getJsonMapper()));
        clientConfig.connectorProvider(new HttpUrlConnectorProvider().useSetMethodWorkaround());
        SSLContext sslContext = SslConfigurator.newInstance().securityProtocol("TLSv1.2").createSSLContext();
        apiClient = ClientBuilder.newBuilder().
                        sslContext(sslContext).
                        withConfig(clientConfig).build();
}
 

实例 9


@Override
public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(JacksonConfig.class);
        classes.add(JacksonJsonProvider.class);
        classes.add(Jackson2JsonpInterceptor.class);
        return classes;
}
 

实例 10


public ResteasyClientFactory() {
    super();
    addContextResolver(new CustomContextResolver());
    jsonProvider = new JacksonJsonProvider();
    addMessageBodyReader(jsonProvider);
    addMessageBodyWriter(jsonProvider);
    streamProvider = new InputStreamProvider();
    addMessageBodyReader(streamProvider);
    addMessageBodyWriter(streamProvider);
}
 

实例 11


public <V extends SimpleResponse> V execute(Operation<V> operation) throws Exception {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.register(JacksonJsonProvider.class);
        clientConfig.register(ObjectMapperProvider.class);
        Client client = ClientBuilder.newClient(clientConfig);
        try {
                WebTarget target = client.target(getURI());
                return operation.perform(target);
        } finally {
                client.close();
        }
}
 

实例 12


protected void configureServlets() {
    //noinspection unchecked
    install( new GuicyFigModule( ServletFig.class, CoordinatorFig.class ) );
    install( new ChopClientModule() );
    // Hook Jersey into Guice Servlet
    bind( GuiceContainer.class );
    // Hook Jackson into Jersey as the POJO <-> JSON mapper
    bind( JacksonJsonProvider.class ).asEagerSingleton();
    bind( IController.class ).to( Controller.class );
    bind( RunnerRegistry.class ).to( RunnerRegistryImpl.class );
    bind( RunManager.class ).to( RunManagerImpl.class );
    bind( ResetResource.class );
    bind( StopResource.class );
    bind( StartResource.class );
    bind( StatsResource.class );
    bind( StatusResource.class );
    Map<String, String> params = new HashMap<String, String>();
    params.put( PACKAGES_KEY, getClass().getPackage().toString() );
    serve( "/*" ).with( GuiceContainer.class, params );
}
 

实例 13


@Test
@RunAsClient
@InSequence(10)
public void shouldReturnAllPersons(@ArquillianResource URL baseURL) {
    Client client = ClientBuilder.newBuilder()
            .register(JacksonJsonProvider.class)
            .build();
    Response response = client.target(baseURL + "r/persons")
            .request(MediaType.APPLICATION_JSON)
            .get();
    response.bufferEntity();
    logResponse("shouldReturnAllPersons", response);
    Assert.assertEquals(Collections.emptyList(), response.readEntity(new GenericType<Collection<Person>>() {}));
}
 

实例 14


@Provides
@Singleton
public JacksonJsonProvider getJacksonJsonProvider(@Json ObjectMapper objectMapper)
{
  final JacksonJsonProvider provider = new JacksonJsonProvider();
  provider.setMapper(objectMapper);
  return provider;
}
 

实例 15


public static Client createJerseyClient() {
        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getClasses().add(MimeMultipartProvider.class);
        clientConfig.getClasses().add(JacksonJsonProvider.class);
        return Client.create(clientConfig);
}
 
讨论
淘淘あ西西 profile image