org.springframework.web.client.RestTemplate.setMessageConverters()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(218)

本文整理了Java中org.springframework.web.client.RestTemplate.setMessageConverters方法的一些代码示例,展示了RestTemplate.setMessageConverters的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RestTemplate.setMessageConverters方法的具体详情如下:
包路径:org.springframework.web.client.RestTemplate
类名称:RestTemplate
方法名:setMessageConverters

RestTemplate.setMessageConverters介绍

[英]Set the message body converters to use.

These converters are used to convert from and to HTTP requests and responses.
[中]设置要使用的消息正文转换器。
这些转换器用于在HTTP请求和响应之间进行转换。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Set the message body converters to use.
 * <p>These converters are used to convert from and to HTTP requests and responses.
 */
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
  this.syncTemplate.setMessageConverters(messageConverters);
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Set the message body converters to use.
 * <p>These converters are used to convert from and to HTTP requests and responses.
 */
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
  this.syncTemplate.setMessageConverters(messageConverters);
}

代码示例来源:origin: ctripcorp/apollo

@PostConstruct
public void init() {
 restTemplate = new RestTemplate();
 SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
 rf.setReadTimeout(portalConfig.readTimeout());
 rf.setConnectTimeout(portalConfig.connectTimeout());
 MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
 converter.setSupportedMediaTypes(
   Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_OCTET_STREAM));
 restTemplate.setMessageConverters(Arrays.asList(converter, new FormHttpMessageConverter()));
}

代码示例来源:origin: apache/geode

public static RestTemplate getRestTemplate() {
 if (restTemplate == null) {
  restTemplate = new RestTemplate();
  final List<HttpMessageConverter<?>> messageConverters =
    new ArrayList<HttpMessageConverter<?>>();
  messageConverters.add(new ByteArrayHttpMessageConverter());
  messageConverters.add(new ResourceHttpMessageConverter());
  messageConverters.add(new StringHttpMessageConverter());
  messageConverters.add(createMappingJackson2HttpMessageConverter());
  restTemplate.setMessageConverters(messageConverters);
 }
 return restTemplate;
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setup() {
  List<HttpMessageConverter<?>> converters = new ArrayList<>();
  converters.add(new StringHttpMessageConverter());
  converters.add(new MappingJackson2HttpMessageConverter());
  this.restTemplate = new RestTemplate();
  this.restTemplate.setMessageConverters(converters);
  this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setup() {
  List<HttpMessageConverter<?>> converters = new ArrayList<>();
  converters.add(new StringHttpMessageConverter());
  converters.add(new MappingJackson2HttpMessageConverter());
  this.restTemplate = new RestTemplate();
  this.restTemplate.setMessageConverters(converters);
  this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setup() {
  ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
  emptyBodyConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
  List<HttpMessageConverter<?>> converters = new ArrayList<>(3);
  converters.add(emptyBodyConverter);
  converters.add(new ByteArrayHttpMessageConverter());
  converters.add(new ResourceHttpMessageConverter());
  converters.add(new MappingJackson2HttpMessageConverter());
  AllEncompassingFormHttpMessageConverter converter = new AllEncompassingFormHttpMessageConverter();
  converter.setPartConverters(converters);
  restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
  restTemplate.setMessageConverters(Collections.singletonList(converter));
}

代码示例来源:origin: spring-projects/spring-framework

@Test  // SPR-13319
public void standardMultipartResolverWithEncodedFileName() throws Exception {
  byte[] boundary = MimeTypeUtils.generateMultipartBoundary();
  String boundaryText = new String(boundary, "US-ASCII");
  Map<String, String> params = Collections.singletonMap("boundary", boundaryText);
  String content =
      "--" + boundaryText + "\n" +
      "Content-Disposition: form-data; name=\"file\"; filename*=\"utf-8''%C3%A9l%C3%A8ve.txt\"\n" +
      "Content-Type: text/plain\n" +
      "Content-Length: 7\n" +
      "\n" +
      "content\n" +
      "--" + boundaryText + "--";
  RequestEntity<byte[]> requestEntity =
      RequestEntity.post(new URI(baseUrl + "/standard-resolver/spr13319"))
          .contentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params))
          .body(content.getBytes(StandardCharsets.US_ASCII));
  ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter();
  converter.setSupportedMediaTypes(Collections.singletonList(MediaType.MULTIPART_FORM_DATA));
  this.restTemplate.setMessageConverters(Collections.singletonList(converter));
  ResponseEntity<Void> responseEntity = restTemplate.exchange(requestEntity, Void.class);
  assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
}

代码示例来源:origin: org.springframework.boot/spring-boot

configureRequestFactory(restTemplate);
if (!CollectionUtils.isEmpty(this.messageConverters)) {
  restTemplate.setMessageConverters(new ArrayList<>(this.messageConverters));

代码示例来源:origin: spring-projects/spring-framework

@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<>();
  converters.add(new Jaxb2RootElementHttpMessageConverter());
  this.restTemplate = new RestTemplate();
  this.restTemplate.setMessageConverters(converters);
  this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}

代码示例来源:origin: spring-projects/spring-framework

@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<>();
  converters.add(new Jaxb2RootElementHttpMessageConverter());
  this.restTemplate = new RestTemplate();
  this.restTemplate.setMessageConverters(converters);
  this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}

代码示例来源:origin: apache/servicecomb-java-chassis

@Override
 public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
  super.setMessageConverters(messageConverters);
  defaultRestTemplate.setMessageConverters(messageConverters);
 }
}

代码示例来源:origin: spring-projects/spring-integration

/**
 * Set a list of {@link HttpMessageConverter}s to be used by the underlying {@link RestTemplate}.
 * Converters configured via this method will override the default converters.
 * @param messageConverters The message converters.
 * @see RestTemplate#setMessageConverters(java.util.List)
 */
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
  this.restTemplate.setMessageConverters(messageConverters);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void requestAvoidsDuplicateAcceptHeaderValues() throws Exception {
  HttpMessageConverter firstConverter = mock(HttpMessageConverter.class);
  given(firstConverter.canRead(any(), any())).willReturn(true);
  given(firstConverter.getSupportedMediaTypes())
      .willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
  HttpMessageConverter secondConverter = mock(HttpMessageConverter.class);
  given(secondConverter.canRead(any(), any())).willReturn(true);
  given(secondConverter.getSupportedMediaTypes())
      .willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
  HttpHeaders requestHeaders = new HttpHeaders();
  mockSentRequest(GET, "http://example.com/", requestHeaders);
  mockResponseStatus(HttpStatus.OK);
  mockTextResponseBody("Hello World");
  template.setMessageConverters(Arrays.asList(firstConverter, secondConverter));
  template.getForObject("http://example.com/", String.class);
  assertEquals("Sent duplicate Accept header values", 1,
      requestHeaders.getAccept().size());
}

代码示例来源:origin: spring-projects/spring-framework

public void exchangeParameterizedType() throws Exception {
  GenericHttpMessageConverter converter = mock(GenericHttpMessageConverter.class);
  template.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));
  ParameterizedTypeReference<List<Integer>> intList = new ParameterizedTypeReference<List<Integer>>() {};
  given(converter.canRead(intList.getType(), null, null)).willReturn(true);

代码示例来源:origin: spring-projects/spring-hateoas

private static final RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {
  RestTemplate template = new RestTemplate();
  template.setMessageConverters(getDefaultMessageConverters(mediaTypes));
  return template;
}

代码示例来源:origin: stackoverflow.com

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.TEXT_HTML);
jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
messageConverters.add(jaxbMessageConverter);
restTemplate.setMessageConverters(messageConverters);

代码示例来源:origin: stackoverflow.com

@Bean
public RestTemplate restTemplate() {

  final RestTemplate restTemplate = new RestTemplate();

  restTemplate.setMessageConverters(Arrays.asList(
      new FormHttpMessageConverter(),
      new StringHttpMessageConverter()
  ));
  restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("client", "secret"));

  return restTemplate;
}

代码示例来源:origin: briandilley/jsonrpc4j

/**
 * Check RestTemplate contains required converters
 */
private void initRestTemplate() {
  boolean isContainsConverter = false;
  for (HttpMessageConverter<?> httpMessageConverter : this.restTemplate.getMessageConverters()) {
    if (MappingJacksonRPC2HttpMessageConverter.class.isAssignableFrom(httpMessageConverter.getClass())) {
      isContainsConverter = true;
      break;
    }
  }
  if (!isContainsConverter) {
    final MappingJacksonRPC2HttpMessageConverter messageConverter = new MappingJacksonRPC2HttpMessageConverter();
    messageConverter.setObjectMapper(this.getObjectMapper());
    final List<HttpMessageConverter<?>> restMessageConverters = new ArrayList<>();
    restMessageConverters.addAll(this.restTemplate.getMessageConverters());
    // Place JSON-RPC converter on the first place!
    restMessageConverters.add(0, messageConverter);
    this.restTemplate.setMessageConverters(restMessageConverters);
  }
  // use specific JSON-RPC error handler if it has not been changed to custom 
  if (restTemplate.getErrorHandler() instanceof org.springframework.web.client.DefaultResponseErrorHandler) {
    restTemplate.setErrorHandler(JsonRpcResponseErrorHandler.INSTANCE);
  }
}

代码示例来源:origin: stackoverflow.com

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();
messageConverter.setObjectMapper(mapper);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);

相关文章