@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; }
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); } }
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);
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);
@Test public void postForObject() throws URISyntaxException { String s = template.postForObject(baseUrl + "/{method}", helloWorld, String.class, "post"); assertEquals("Invalid content", helloWorld, s); }
@Test // SPR-15015 public void postWithoutBody() throws Exception { assertNull(template.postForObject(baseUrl + "/jsonpost", null, String.class)); }
@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) { } }); } } }
@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\"")); }
@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\"")); }
validatedToken = restTemplate.postForObject(introspectionUrl, form, String.class); } catch (RestClientException rce) { logger.error("validateToken", rce);
String registered = restTemplate.postForObject(serverConfig.getRegistrationEndpointUri(), entity, String.class);
userInfoString = restTemplate.postForObject(serverConfiguration.getUserInfoUri(), form, String.class); } else if (serverConfiguration.getUserInfoTokenMethod().equals(UserInfoTokenMethod.QUERY)) { URIBuilder builder = new URIBuilder(serverConfiguration.getUserInfoUri());
try { URI uri = new URI(respUrl); notifyResult = restTemplate.postForObject(uri, null, String.class); }catch (Exception e) { _log.error(e, "通知商户系统异常");
@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(); }
@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(); }
jsonString = restTemplate.postForObject(serverConfig.getTokenEndpointUri(), form, String.class); } catch (RestClientException e) {
@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); }
@Override public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException { return getRestTemplate(url).postForObject(url, request, responseType, uriVariables); }
@Override public <T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException { return getRestTemplate(url).postForObject(url, request, responseType); }
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);