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

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

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

RestTemplate.postForObject介绍

暂无

代码示例

代码示例来源:origin: macrozheng/mall

@ApiOperation("postForEntity jsonBody")
@RequestMapping(value = "/post2", method = RequestMethod.POST)
@ResponseBody
public Object postForObject(@RequestBody PmsBrand brand) {
  String url = HOST_MALL_ADMIN + "/brand/create";
  CommonResult commonResult = restTemplate.postForObject(url, brand, CommonResult.class);
  return commonResult;
}

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

private void sendMsg(String serverAddress, String topic, Object msg) {
 HttpHeaders headers = new HttpHeaders();
 headers.setContentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM + ";charset=UTF-8"));
 HttpEntity<Object> request = new HttpEntity<>(msg, headers);
 try {
  //send msg by hermes RestAPI
  restTemplate.postForObject(serverAddress + "/topics/" + topic, request, Object.class);
 } catch (Exception e) {
  Tracer.logError("Send publish msg to hermes failed", e);
 }
}

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

RestTemplate restTemplate = new RestTemplate();

String url = "endpoint url";
String requestJson = "{\"queriedQuestion\":\"Is there pain in your hand?\"}";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
String answer = restTemplate.postForObject(url, entity, String.class);
System.out.println(answer);

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

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Authorization", "Basic " + base64Creds);
headers.add("Content-Type", "application/json");

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

HttpEntity<ObjectToPass> request = new HttpEntity<ObjectToPass>(ObjectToPass, headers);

restTemplate.postForObject(urlPost, request, Boolean.class);

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

@Test
public void postForObject() throws URISyntaxException {
  String s = template.postForObject(baseUrl + "/{method}", helloWorld, String.class, "post");
  assertEquals("Invalid content", helloWorld, s);
}

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

@Test  // SPR-15015
public void postWithoutBody() throws Exception {
  assertNull(template.postForObject(baseUrl + "/jsonpost", null, String.class));
}

代码示例来源:origin: codingapi/tx-lcn

@Override
  public void onException(TxException txException) {
    if (txManagerConfig.isExUrlEnabled()) {
      executorService.submit(() -> {
        try {
          if (!txManagerConfig.getExUrl().startsWith("http")) {
            txManagerConfig.setExUrl("http://127.0.0.1:" + managerServicePort + txManagerConfig.getExUrl());
          }
          restTemplate.postForObject(txManagerConfig.getExUrl(), txException, String.class);
        } catch (Exception ignored) {
        }
      });
    }
  }
}

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

@Test
public void jsonPostForObjectWithJacksonView() throws URISyntaxException {
  HttpHeaders entityHeaders = new HttpHeaders();
  entityHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
  MySampleBean bean = new MySampleBean("with", "with", "without");
  MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
  jacksonValue.setSerializationView(MyJacksonView1.class);
  HttpEntity<MappingJacksonValue> entity = new HttpEntity<>(jacksonValue, entityHeaders);
  String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class);
  assertTrue(s.contains("\"with1\":\"with\""));
  assertFalse(s.contains("\"with2\":\"with\""));
  assertFalse(s.contains("\"without\":\"without\""));
}

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

@Test
public void jsonPostForObject() throws URISyntaxException {
  HttpHeaders entityHeaders = new HttpHeaders();
  entityHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
  MySampleBean bean = new MySampleBean();
  bean.setWith1("with");
  bean.setWith2("with");
  bean.setWithout("without");
  HttpEntity<MySampleBean> entity = new HttpEntity<>(bean, entityHeaders);
  String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class);
  assertTrue(s.contains("\"with1\":\"with\""));
  assertTrue(s.contains("\"with2\":\"with\""));
  assertTrue(s.contains("\"without\":\"without\""));
}

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

validatedToken = restTemplate.postForObject(introspectionUrl, form, String.class);
} catch (RestClientException rce) {
  logger.error("validateToken", rce);

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

String registered = restTemplate.postForObject(serverConfig.getRegistrationEndpointUri(), entity, String.class);

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

userInfoString = restTemplate.postForObject(serverConfiguration.getUserInfoUri(), form, String.class);
} else if (serverConfiguration.getUserInfoTokenMethod().equals(UserInfoTokenMethod.QUERY)) {
  URIBuilder builder = new URIBuilder(serverConfiguration.getUserInfoUri());

代码示例来源:origin: jmdhappy/xxpay-master

try {
  URI uri = new URI(respUrl);
  notifyResult = restTemplate.postForObject(uri, null, String.class);
}catch (Exception e) {
  _log.error(e, "通知商户系统异常");

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

@Test
public void postForObject() throws Exception {
  mockTextPlainHttpMessageConverter();
  HttpHeaders requestHeaders = new HttpHeaders();
  mockSentRequest(POST, "http://example.com", requestHeaders);
  mockResponseStatus(HttpStatus.OK);
  String expected = "42";
  mockResponseBody(expected, MediaType.TEXT_PLAIN);
  String result = template.postForObject("http://example.com", "Hello World", String.class);
  assertEquals("Invalid POST result", expected, result);
  assertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
  verify(response).close();
}

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

@Test
public void postForObjectNull() throws Exception {
  mockTextPlainHttpMessageConverter();
  HttpHeaders requestHeaders = new HttpHeaders();
  mockSentRequest(POST, "http://example.com", requestHeaders);
  mockResponseStatus(HttpStatus.OK);
  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.setContentType(MediaType.TEXT_PLAIN);
  responseHeaders.setContentLength(10);
  given(response.getHeaders()).willReturn(responseHeaders);
  given(response.getBody()).willReturn(StreamUtils.emptyInput());
  given(converter.read(String.class, response)).willReturn(null);
  String result = template.postForObject("http://example.com", null, String.class);
  assertNull("Invalid POST result", result);
  assertEquals("Invalid content length", 0, requestHeaders.getContentLength());
  verify(response).close();
}

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

jsonString = restTemplate.postForObject(serverConfig.getTokenEndpointUri(), form, String.class);
} catch (RestClientException e) {

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

@Override
public <T> T postForObject(String url, Object request, Class<T> responseType,
  Map<String, ?> uriVariables) throws RestClientException {
 return getRestTemplate(url).postForObject(url, request, responseType, uriVariables);
}

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

@Override
public <T> T postForObject(String url, Object request, Class<T> responseType,
  Object... uriVariables) throws RestClientException {
 return getRestTemplate(url).postForObject(url, request, responseType, uriVariables);
}

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

@Override
public <T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException {
 return getRestTemplate(url).postForObject(url, request, responseType);
}

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

RestTemplate restTemplate = new RestTemplate();             
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());              
Serializer serializer = new Persister(new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter(serializer));
restTemplate.postForObject(URL, udata, String.class);

相关文章