/** * Adds a set of default HttpMessageConverter instances to the given list. * Subclasses can call this method from {@link #configureMessageConverters(List)}. * @param messageConverters the list to add the default message converters to */ protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) { StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(); stringConverter.setWriteAcceptCharset(false); messageConverters.add(new ByteArrayHttpMessageConverter()); messageConverters.add(stringConverter); messageConverters.add(new ResourceHttpMessageConverter()); messageConverters.add(new SourceHttpMessageConverter<Source>()); messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { messageConverters.add(new AtomFeedHttpMessageConverter()); messageConverters.add(new RssChannelHttpMessageConverter()); } if (jaxb2Present) { messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { messageConverters.add(new MappingJackson2HttpMessageConverter()); } else if (jacksonPresent) { messageConverters.add(new MappingJacksonHttpMessageConverter()); } }
/** * Create a new instance of the {@link RestTemplate} using default settings. * Default {@link HttpMessageConverter}s are initialized. */ @SuppressWarnings("deprecation") public RestTemplate() { this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(new StringHttpMessageConverter()); this.messageConverters.add(new ResourceHttpMessageConverter()); this.messageConverters.add(new SourceHttpMessageConverter<Source>()); this.messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { this.messageConverters.add(new AtomFeedHttpMessageConverter()); this.messageConverters.add(new RssChannelHttpMessageConverter()); } if (jaxb2Present) { this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { this.messageConverters.add(new MappingJackson2HttpMessageConverter()); } else if (jacksonPresent) { this.messageConverters.add(new org.springframework.http.converter.json.MappingJacksonHttpMessageConverter()); } }
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) { final MappingJackson2HttpMessageConverter jsonMessageConverter = jacksonMessageConverter(); final StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8")); stringHttpMessageConverter.setWriteAcceptCharset(false); messageConverters.add(new CSVMessageConverter()); messageConverters.add(new ByteArrayHttpMessageConverter()); messageConverters.add(stringHttpMessageConverter); messageConverters.add(new ResourceHttpMessageConverter()); messageConverters.add(new SourceHttpMessageConverter<>()); messageConverters.add(new AllEncompassingFormHttpMessageConverter()); messageConverters.add(jsonMessageConverter); messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); }
public AllEncompassingFormHttpMessageConverter() { addPartConverter(new SourceHttpMessageConverter<Source>()); if (jaxb2Present && !jackson2Present) { addPartConverter(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { addPartConverter(new MappingJackson2HttpMessageConverter()); } else if (gsonPresent) { addPartConverter(new GsonHttpMessageConverter()); } if (jackson2XmlPresent) { addPartConverter(new MappingJackson2XmlHttpMessageConverter()); } }
@Before public void setup() { List<Person> composers = Arrays.asList( new Person("Johann Sebastian Bach").setSomeDouble(21), new Person("Johannes Brahms").setSomeDouble(.0025), new Person("Edvard Grieg").setSomeDouble(1.6035), new Person("Robert Schumann").setSomeDouble(Double.NaN)); List<Person> performers = Arrays.asList( new Person("Vladimir Ashkenazy").setSomeBoolean(false), new Person("Yehudi Menuhin").setSomeBoolean(true)); this.people = new PeopleWrapper(composers, performers); List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); converters.add(new Jaxb2RootElementHttpMessageConverter()); this.restTemplate = new RestTemplate(); this.restTemplate.setMessageConverters(converters); this.mockServer = MockRestServiceServer.createServer(this.restTemplate); }
@Before public void setup() { List<Person> composers = Arrays.asList( new Person("Johann Sebastian Bach").setSomeDouble(21), new Person("Johannes Brahms").setSomeDouble(.0025), new Person("Edvard Grieg").setSomeDouble(1.6035), new Person("Robert Schumann").setSomeDouble(Double.NaN)); this.people = new PeopleWrapper(composers); List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); converters.add(new Jaxb2RootElementHttpMessageConverter()); this.restTemplate = new RestTemplate(); this.restTemplate.setMessageConverters(converters); this.mockServer = MockRestServiceServer.createServer(this.restTemplate); }
@Test public void shouldFailIfNoConverterFoundForBodyOfUnknownContentType() { final MockSetup setup = new MockSetup("https://api.example.com", Collections.emptyList()); final MockRestServiceServer server = setup.getServer(); final Http unit = setup.getHttpBuilder() .converter(new Jaxb2RootElementHttpMessageConverter()).build(); // we never actually make the request, but the mock server is doing some magic pre-actively server.expect(requestTo(url)) .andRespond(withSuccess()); exception.expect(RestClientException.class); exception.expectMessage("no suitable HttpMessageConverter found "); exception.expectMessage("org.zalando.riptide.model.Success"); unit.delete(url) .body(new Success(true)) .dispatch(contentType()); server.verify(); }
@Before public void setup() { MockitoAnnotations.initMocks(this); /* * mockMvc object is being set up to support both XML and JSON * * runtime 'com.fasterxml.jackson.core:jackson-core:2.2.2' * * runtime 'com.fasterxml.jackson.core:jackson-databind:2.2.2' * * runtime 'javax.xml.bind:jaxb-api:2.2.9' */ this.mockMvc = standaloneSetup(controller) .setMessageConverters(new MappingJackson2HttpMessageConverter(), new Jaxb2RootElementHttpMessageConverter()).build(); }
@SuppressWarnings("deprecation") public AllEncompassingFormHttpMessageConverter() { addPartConverter(new SourceHttpMessageConverter<Source>()); if (jaxb2Present) { addPartConverter(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { addPartConverter(new MappingJackson2HttpMessageConverter()); } else if (jacksonPresent) { addPartConverter(new org.springframework.http.converter.json.MappingJacksonHttpMessageConverter()); } }
/** * 只考虑微信的消息转换,后期可以优化 * 其实这里完全可以使用系统的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; }
/** * Create a new instance of the {@link RestTemplate} using default settings. * Default {@link HttpMessageConverter}s are initialized. */ public RestTemplate() { this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(new StringHttpMessageConverter()); this.messageConverters.add(new ResourceHttpMessageConverter()); this.messageConverters.add(new SourceHttpMessageConverter<Source>()); this.messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { this.messageConverters.add(new AtomFeedHttpMessageConverter()); this.messageConverters.add(new RssChannelHttpMessageConverter()); } if (jackson2XmlPresent) { this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter()); } else if (jaxb2Present) { this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { this.messageConverters.add(new MappingJackson2HttpMessageConverter()); } else if (gsonPresent) { this.messageConverters.add(new GsonHttpMessageConverter()); } }
@Override protected void initRestTemplate(RestTemplate restTemplate) { super.initRestTemplate(restTemplate); restTemplate.setErrorHandler(new DCTMJaxbErrorHandler(restTemplate.getMessageConverters())); for(HttpMessageConverter<?> c : restTemplate.getMessageConverters()) { if(c instanceof FormHttpMessageConverter) { ((FormHttpMessageConverter)c).addPartConverter(new Jaxb2RootElementHttpMessageConverter()); break; } } }
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(objectMapperBuilder().build()); converters.add(new Jaxb2RootElementHttpMessageConverter()); converters.add(new Jaxb2CollectionHttpMessageConverter<>()); converters.add(converter); super.configureMessageConverters(converters); }
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MappingJackson2HttpMessageConverter()); converters.add(new Jaxb2RootElementHttpMessageConverter()); converters.add(new Jaxb2CollectionHttpMessageConverter<>()); converters.add(new HtmlMessageConverter()); }
private static RestTemplate createRestTemplate() { List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); messageConverters.add(new ByteArrayHttpMessageConverter()); messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8"))); messageConverters.add(new ResourceHttpMessageConverter()); messageConverters.add(new SourceHttpMessageConverter<Source>()); messageConverters.add(new AllEncompassingFormHttpMessageConverter()); messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); messageConverters.add(new MappingJackson2HttpMessageConverter()); return new RestTemplate(messageConverters); }
public NaverOAuth2ApiBinding(final String accessToken) { super(accessToken); super.setRequestFactory(ClientHttpRequestFactorySelector.bufferRequests(getRestTemplate().getRequestFactory())); // set interceptors getRestTemplate().setInterceptors(Arrays.asList(new ClientHttpRequestInterceptor[]{new AdminKeyHeaderOAuth2RequestInterceptor(accessToken)})); // set xml->class http message converters getRestTemplate().setMessageConverters(Arrays.asList(new HttpMessageConverter<?>[]{new Jaxb2RootElementHttpMessageConverter()})); // init api operations userOperation = new UserOperationImpl(getRestTemplate(), isAuthorized()); }
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new StringHttpMessageConverter()); converters.add(new Jaxb2RootElementHttpMessageConverter()); converters.add(new MappingJacksonHttpMessageConverter()); converters.add(new ReportCsvMessageConverter()); }
private ManagedList<?> getMessageConverters(Element element, Object source, ParserContext parserContext) { Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters"); ManagedList<? super Object> messageConverters = new ManagedList<Object>(); if (convertersElement != null) { messageConverters.setSource(source); for (Element beanElement : DomUtils.getChildElementsByTagName(convertersElement, "bean", "ref")) { Object object = parserContext.getDelegate().parsePropertySubElement(beanElement, null); messageConverters.add(object); } } if (convertersElement == null || Boolean.valueOf(convertersElement.getAttribute("register-defaults"))) { messageConverters.setSource(source); messageConverters.add(createConverterDefinition(ByteArrayHttpMessageConverter.class, source)); RootBeanDefinition stringConverterDef = createConverterDefinition(StringHttpMessageConverter.class, source); stringConverterDef.getPropertyValues().add("writeAcceptCharset", false); messageConverters.add(stringConverterDef); messageConverters.add(createConverterDefinition(ResourceHttpMessageConverter.class, source)); messageConverters.add(createConverterDefinition(SourceHttpMessageConverter.class, source)); messageConverters.add(createConverterDefinition(AllEncompassingFormHttpMessageConverter.class, source)); if (romePresent) { messageConverters.add(createConverterDefinition(AtomFeedHttpMessageConverter.class, source)); messageConverters.add(createConverterDefinition(RssChannelHttpMessageConverter.class, source)); } if (jaxb2Present) { messageConverters.add(createConverterDefinition(Jaxb2RootElementHttpMessageConverter.class, source)); } if (jackson2Present) { messageConverters.add(createConverterDefinition(MappingJackson2HttpMessageConverter.class, source)); } else if (jacksonPresent) { messageConverters.add(createConverterDefinition(MappingJacksonHttpMessageConverter.class, source)); } } return messageConverters; }
/** * Create a new instance of the {@link RestTemplate} using default settings. * Default {@link HttpMessageConverter}s are initialized. */ public RestTemplate() { this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(new StringHttpMessageConverter()); this.messageConverters.add(new ResourceHttpMessageConverter()); this.messageConverters.add(new SourceHttpMessageConverter<Source>()); this.messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { this.messageConverters.add(new AtomFeedHttpMessageConverter()); this.messageConverters.add(new RssChannelHttpMessageConverter()); } if (jaxb2Present) { this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { this.messageConverters.add(new MappingJackson2HttpMessageConverter()); } else if (jacksonPresent) { this.messageConverters.add(new MappingJacksonHttpMessageConverter()); } }
public AllEncompassingFormHttpMessageConverter() { addPartConverter(new SourceHttpMessageConverter()); if (jaxb2Present) { addPartConverter(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { addPartConverter(new MappingJackson2HttpMessageConverter()); } else if (jacksonPresent) { addPartConverter(new MappingJacksonHttpMessageConverter()); } }
@Before public void setup() { MockitoAnnotations.initMocks(this); this.mockMvc = standaloneSetup(controller).setMessageConverters( new MappingJackson2HttpMessageConverter(), new Jaxb2RootElementHttpMessageConverter()).build(); }
private List<HttpMessageConverter<?>> converters() { List<HttpMessageConverter<?>> result = Lists.newArrayList(); result.add(new ByteArrayHttpMessageConverter()); result.add(new StringHttpMessageConverter()); result.add(new ResourceHttpMessageConverter()); result.add(new SourceHttpMessageConverter<>()); result.add(new AllEncompassingFormHttpMessageConverter()); result.add(new Jaxb2RootElementHttpMessageConverter()); result.add(gsonHttpMessageConverter); return result; }
public static RestTemplate getRest() { RestTemplate rest = new RestTemplate(); List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); converters.add(new Jaxb2RootElementHttpMessageConverter()); converters.add(new FormHttpMessageConverter()); converters.add(new MappingJacksonHttpMessageConverter()); rest.setMessageConverters(converters); return rest; }
@Before public void setup() { MockitoAnnotations.initMocks(this); this.mockMvc = standaloneSetup(controller) .setMessageConverters(new MappingJackson2HttpMessageConverter(), new Jaxb2RootElementHttpMessageConverter()).build(); }
@Before public void setup() { MockitoAnnotations.initMocks(this); this.mockMvc = standaloneSetup(controller) .setMessageConverters( new MappingJackson2HttpMessageConverter(), new Jaxb2RootElementHttpMessageConverter()).build(); }
@Bean @Qualifier("example") public ClientHttpMessageConverters exampleHttpMessageConverters() { return new ClientHttpMessageConverters(singletonList(new Jaxb2RootElementHttpMessageConverter())); }
@Test public void shouldOverride() { assertThat(unit.getConverters(), contains(instanceOf(Jaxb2RootElementHttpMessageConverter.class))); }
private void filterXmlConverters(RestTemplate restTemplate) { Stream<HttpMessageConverter<?>> messageConverterStream = restTemplate.getMessageConverters().stream(); Predicate<HttpMessageConverter<?>> noXmlConverters = httpMessageConverter -> !(httpMessageConverter instanceof Jaxb2RootElementHttpMessageConverter) && !(httpMessageConverter instanceof MappingJackson2XmlHttpMessageConverter); List<HttpMessageConverter<?>> filtered = messageConverterStream.filter(noXmlConverters).collect(Collectors.toList()); restTemplate.setMessageConverters(filtered); }
/** * JAXB converter * @return */ @Bean public HttpMessageConverter<Object> jaxbConverter() { Jaxb2RootElementHttpMessageConverter converter = new Jaxb2RootElementHttpMessageConverter(); return converter; }