/** * 是否有必要模仿Spring不提供RestTemplate,只提供RestTemplateBuilder * * @return dummy */ @Bean(name = WxBeans.WX_API_TEMPLATE_NAME) public WxApiTemplate wxApiTemplate() { RestTemplateBuilder builder = new RestTemplateBuilder(); builder = builder.requestFactory(new WxApiHttpRequestFactory(wxProperties)) .errorHandler(new WxResponseErrorHandler()); HttpMessageConverters converters = this.messageConverters.getIfUnique(); List<HttpMessageConverter<?>> converterList = new ArrayList<>(); // 加入默认转换 converterList.add(new WxHttpInputMessageConverter()); if (converters != null) { converterList.addAll(converters.getConverters()); builder = builder.messageConverters(Collections.unmodifiableList(converterList)); } return new WxApiTemplate(builder.build()); }
@Bean public Decoder feignDecoder () { val springConverters = messageConverters.getObject().getConverters(); val decoderConverters = new ArrayList<HttpMessageConverter<?>>(springConverters.size() + 1); decoderConverters.addAll(springConverters); decoderConverters.add(new SpringManyMultipartFilesReader(4096)); val httpMessageConverters = new HttpMessageConverters(decoderConverters); return new SpringDecoder(new ObjectFactory<HttpMessageConverters>() { @Override public HttpMessageConverters getObject () { return httpMessageConverters; } }); }
@Bean @ConditionalOnMissingBean(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.class) public HttpMessageConverters customConverters(FastJsonHttpMessageConverter converter) { Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); if (null == converter) { Class<?> converterClass = properties.getConverter(); converter = (FastJsonHttpMessageConverter) BeanUtils.instantiate(converterClass); } FastJsonConfig config = new FastJsonConfig(); List<SerializerFeature> features = properties.getFeatures(); if (!CollectionUtils.isBlank(features)) { SerializerFeature[] featureArray = new SerializerFeature[features.size()]; config.setSerializerFeatures(features.toArray(featureArray)); } converter.setFastJsonConfig(config); messageConverters.add(converter); return new HttpMessageConverters(true, messageConverters); }
@Bean public HttpMessageConverters customConverters() { Gson gson = new GsonBuilder() .setPrettyPrinting() .create(); GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter(); gsonHttpMessageConverter.setGson(gson); // normally you can simply exclude jackson from the classpath to cause // Spring to use gson, but minio also depends on jackson List<HttpMessageConverter<?>> converters = new RestTemplate().getMessageConverters(); converters.removeIf(converter -> converter.getClass() == MappingJackson2HttpMessageConverter.class); converters.add(gsonHttpMessageConverter); return new HttpMessageConverters(false, converters); }
private String registerHttpMessageConverters(final String id) { return registry.register(id, HttpMessageConverters.class, () -> { final List<Object> list = list(); LOG.debug("Client [{}]: Registering StringHttpMessageConverter", id); list.add(genericBeanDefinition(StringHttpMessageConverter.class) .addPropertyValue("writeAcceptCharset", false) .getBeanDefinition()); final String objectMapperId = findObjectMapper(id); LOG.debug("Client [{}]: Registering MappingJackson2HttpMessageConverter referencing [{}]", id, objectMapperId); list.add(genericBeanDefinition(MappingJackson2HttpMessageConverter.class) .addConstructorArgReference(objectMapperId) .getBeanDefinition()); LOG.debug("Client [{}]: Registering StreamConverter referencing [{}]", id, objectMapperId); list.add(genericBeanDefinition(Streams.class) .setFactoryMethod("streamConverter") .addConstructorArgReference(objectMapperId) .getBeanDefinition()); return BeanDefinitionBuilder.genericBeanDefinition(ClientHttpMessageConverters.class) .addConstructorArgValue(list); }); }
@Bean public HttpMessageConverters messageConverters() { final ObjectMapper objectMapper = new ObjectMapper(); setupObjectMapper(objectMapper); return new HttpMessageConverters( // Prevent default converters false, // Have Jackson2 converter as the sole converter Arrays.<HttpMessageConverter<?>>asList(new MappingJackson2HttpMessageConverter(objectMapper))); }
/** * 配置使用springmvc fastjson * @return */ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); }
@Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //处理中文乱码 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); return new HttpMessageConverters((HttpMessageConverter<?>) fastConverter); }
@Bean public HttpMessageConverters customConverters() { MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); jsonConverter.setSupportedMediaTypes(Arrays.asList( new MediaType("application", "json"), new MediaType("text", "json") )); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JsonOrgModule()); jsonConverter.setObjectMapper(objectMapper); return new HttpMessageConverters(jsonConverter); }
@Bean public HttpMessageConverters messageConverters() { GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter(); gsonHttpMessageConverter.setGson( new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create()); final List<HttpMessageConverter<?>> converters = Lists.newArrayList( new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter(), new AllEncompassingFormHttpMessageConverter(), gsonHttpMessageConverter); return new HttpMessageConverters() { @Override public List<HttpMessageConverter<?>> getConverters() { return converters; } }; }
/** * 只考虑微信的消息转换,后期可以优化 * 其实这里完全可以使用系统的Bean,但是这里我想特殊处理,只对微信消息做转换,所以定制化了几个converter * * @return dummy */ private HttpMessageConverters getDefaultWxMessageConverters() { StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8); stringConverter.setWriteAcceptCharset(false); MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); Jaxb2RootElementHttpMessageConverter xmlConverter = new Jaxb2RootElementHttpMessageConverter(); AllEncompassingFormHttpMessageConverter formConverter = new AllEncompassingFormHttpMessageConverter(); ResourceHttpMessageConverter resourceConverter = new ResourceHttpMessageConverter(); HttpMessageConverters wxMessageConverters = new HttpMessageConverters(stringConverter, jsonConverter, xmlConverter, formConverter, resourceConverter); return wxMessageConverters; }
@Bean public HttpMessageConverters customConverters() { List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new FunctionalSyntaxHttpMessageConverter()); converters.add(new ManchesterSyntaxHttpMessageConverter()); converters.add(new OwlXmlHttpMessageConverter()); converters.add(new RdfXmlOntologyHttpMessageConverter()); converters.add(new TurtleOntologyHttpMessageConverter()); return new HttpMessageConverters(converters); }
@org.springframework.context.annotation.Primary @org.springframework.context.annotation.Bean public feign.codec.Decoder compensableFeignDecoder(@Autowired feign.codec.Decoder decoder, @Autowired ObjectFactory<HttpMessageConverters> objectFactory) { CompensableFeignDecoder feignDecoder = new CompensableFeignDecoder(decoder); feignDecoder.setObjectFactory(objectFactory); return feignDecoder; }
@org.springframework.context.annotation.Primary @org.springframework.context.annotation.Bean public feign.codec.Decoder compensableFeignDecoder(@Autowired ObjectFactory<HttpMessageConverters> objectFactory) { CompensableFeignDecoder feignDecoder = new CompensableFeignDecoder(); feignDecoder.setObjectFactory(objectFactory); return feignDecoder; }
@Bean public HttpMessageConverters messageConverters(ObjectMapper objectMapper) { return new HttpMessageConverters( // Prevent default converters false, // Have Jackson2 converter as the sole converter Arrays.<HttpMessageConverter<?>>asList(new MappingJackson2HttpMessageConverter(objectMapper))); }
private BeanDefinition registerHttpMessageConverters(final String id) { // we use the wrong type here since that's the easiest way to influence the name // we want exampleHttpMessageConverters, rather than exampleClientHttpMessageConverters final String convertersId = registry.registerIfAbsent(id, HttpMessageConverters.class, () -> { final List<Object> list = list(); log.debug("Client [{}]: Registering StringHttpMessageConverter", id); list.add(genericBeanDefinition(StringHttpMessageConverter.class) .addPropertyValue("writeAcceptCharset", false) .getBeanDefinition()); final String objectMapperId = findObjectMapper(id); log.debug("Client [{}]: Registering MappingJackson2HttpMessageConverter referencing [{}]", id, objectMapperId); list.add(genericBeanDefinition(MappingJackson2HttpMessageConverter.class) .addConstructorArgReference(objectMapperId) .getBeanDefinition()); ifPresent("org.zalando.riptide.stream.Streams", () -> { log.debug("Client [{}]: Registering StreamConverter referencing [{}]", id, objectMapperId); list.add(genericBeanDefinition(Streams.class) .setFactoryMethod("streamConverter") .addConstructorArgReference(objectMapperId) .getBeanDefinition()); }); return BeanDefinitionBuilder.genericBeanDefinition(ClientHttpMessageConverters.class) .addConstructorArgValue(list); }); final AbstractBeanDefinition converters = BeanDefinitionBuilder.genericBeanDefinition() .setFactoryMethod("getConverters") .getBeanDefinition(); converters.setFactoryBeanName(convertersId); return converters; }
@org.springframework.context.annotation.Primary @org.springframework.context.annotation.Bean public feign.codec.Decoder transactionFeignDecoder(@Autowired ObjectFactory<HttpMessageConverters> objectFactory, @Autowired feign.codec.Decoder decoder) { TransactionFeignDecoder feignDecoder = new TransactionFeignDecoder(decoder); feignDecoder.setObjectFactory(objectFactory); return feignDecoder; }
@Bean HttpMessageConverters fastJsonHttpMessageConverters() { return new HttpMessageConverters(new EncrypConverter()); }
public TestRestTemplateConfig( final ObjectProvider<HttpMessageConverters> messageConverters, final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) { super(messageConverters, restTemplateCustomizers); }
public CustomEncode(ObjectFactory<HttpMessageConverters> messageConverters) { this.messageConverters = messageConverters; }
public WxInvokerConfiguration( WxProperties wxProperties, ObjectProvider<HttpMessageConverters> messageConverters) { this.wxProperties = wxProperties; this.messageConverters = messageConverters; }
@Bean public HttpMessageConverters getJacksonHttpMessageConverters(ObjectMapper objectMapper) { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper); converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.TEXT_HTML, MediaType.TEXT_PLAIN)); return new HttpMessageConverters(converter); }
@Bean public HttpMessageConverters httpMessageConverters() { //FastJson return new HttpMessageConverters(fastJsonHttpMessageConverter4()); }
@Bean public Decoder feignDecoder() { HttpMessageConverter jacksonConverter = new QuoteOnDesignMessageConverter(); ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter); return new ResponseEntityDecoder(new SpringDecoder(objectFactory)); }
@Bean public HttpMessageConverters customConverters() { HttpMessageConverter<?> additional = new FastJsonHttpMessageConverter(); return new HttpMessageConverters(additional); }
@Bean @ConditionalOnMissingBean public HttpMessageConverters messageConverters() { return new HttpMessageConverters(this.converters == null ? Collections.<HttpMessageConverter<?>>emptyList() : this.converters); }
@Bean @Primary public HttpMessageConverters httpMessageConverters() { return new HttpMessageConverters(); }
@org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean(feign.codec.Decoder.class) @org.springframework.context.annotation.Bean public feign.codec.Decoder feignDecoder(@Autowired ObjectFactory<HttpMessageConverters> messageConverters) { return new ResponseEntityDecoder(new SpringDecoder(messageConverters)); }
public ObjectFactory<HttpMessageConverters> getObjectFactory() { return objectFactory; }
public void setObjectFactory(ObjectFactory<HttpMessageConverters> objectFactory) { this.objectFactory = objectFactory; }
@Bean public FluxReturnValueHandler fluxReturnValueHandler(FunctionInspector inspector, HttpMessageConverters converters) { return new FluxReturnValueHandler(inspector, converters.getConverters()); }
@Bean @ConditionalOnMissingBean public HttpMessageConverters messageConverters() { return new HttpMessageConverters(this.converters); }
public ResultErrorDecoder(HttpMessageConverters httpMessageConverters) { super(); this.httpMessageConverters = httpMessageConverters; }
public ExtResponseEntityDecoder(ObjectFactory<HttpMessageConverters> messageConverter) { this.messageConverters = messageConverter; }
public SpringConverterFactory(ObjectFactory<HttpMessageConverters> messageConverters, ConversionService conversionService) { this.messageConverters = messageConverters; this.conversionService = conversionService; }
@Bean public SpringConverterFactory springConverterFactory(ConversionService conversionService, ObjectFactory<HttpMessageConverters> messageConverters) { return new SpringConverterFactory(messageConverters, conversionService); }