List of usage examples for org.springframework.web.client RestTemplate postForObject
@Override @Nullable public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException
From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java
private static void testValidatorAddSuccess(RestTemplate template, String cseUrlPrefix) { Map<String, String> params = new HashMap<>(); params.put("a", "5"); params.put("b", "20"); int result = template.postForObject(cseUrlPrefix + "add", params, Integer.class); TestMgr.check(25, result);//w ww . j a v a 2 s. co m }
From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java
private static void testPost(RestTemplate template, String cseUrlPrefix) { Map<String, String> params = new HashMap<>(); params.put("a", "5"); params.put("b", "3"); int result = template.postForObject(cseUrlPrefix + "/compute/add", params, Integer.class); TestMgr.check(8, result);/*from w ww .j a v a 2 s .c om*/ Person person = new Person(); person.setName("world"); Person resultPerson = template.postForObject(cseUrlPrefix + "/compute/sayhello", person, Person.class); TestMgr.check("hello world", resultPerson.getName()); HttpHeaders headers = new HttpHeaders(); headers.add("prefix", "haha"); HttpEntity<Person> reqEntity = new HttpEntity<>(person, headers); TestMgr.check("haha world", template.postForObject(cseUrlPrefix + "/compute/saysomething", reqEntity, String.class)); }
From source file:com.iflytek.rest.demo.UserServiceTest.java
public static String requestAccessToken() { RestTemplate restTemplate = new RestTemplate(); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.add("client_id", "Hb0YhmOo"); form.add("client_secret", "R7odNVS0KPtgXJ1TKQbHAxFP6EHdSW5d"); form.add("grant_type", "client_credentials"); //?password & client_credentials //grant_typeclient_credentials????username & password? form.add("username", "admin_test_333"); form.add("password", "passw0rd"); String result = restTemplate.postForObject(OAUTH_SERVER_URL, form, String.class); System.out.println(result);//from w w w . j ava2 s .c o m String access_token = JSON.parseObject(result).getString("access_token"); return access_token; }
From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java
private static void testClientTimeoutAdd(RestTemplate template, String cseUrlPrefix) { Map<String, String> params = new HashMap<>(); params.put("a", "5"); params.put("b", "20"); boolean isExcep = false; try {// www . j a va 2 s . c o m template.postForObject(cseUrlPrefix + "add", params, Integer.class); } catch (InvocationException e) { isExcep = true; TestMgr.check(490, e.getStatus().getStatusCode()); TestMgr.check("CommonExceptionData [message=Cse Internal Bad Request]", e.getErrorData()); } TestMgr.check(true, isExcep); }
From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java
@SuppressWarnings({ "rawtypes" }) private static void testValidatorAddFail(RestTemplate template, String cseUrlPrefix) { Map<String, String> params = new HashMap<>(); params.put("a", "5"); params.put("b", "3"); boolean isExcep = false; try {//from w w w . ja v a2 s . c o m template.postForObject(cseUrlPrefix + "add", params, Integer.class); } catch (InvocationException e) { isExcep = true; TestMgr.check(400, e.getStatus().getStatusCode()); TestMgr.check(Status.BAD_REQUEST, e.getReasonPhrase()); // Message dependends on locale, so just check the short part. // 'must be greater than or equal to 20', propertyPath=add.arg1, rootBeanClass=class org.apache.servicecomb.demo.jaxrs.server.Validator, messageTemplate='{javax.validation.constraints.Min.message}'}]] // ignored Map data = (Map) e.getErrorData(); TestMgr.check(true, data.get("message").toString().contains("propertyPath=add.b")); } TestMgr.check(true, isExcep); }
From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java
private static void testRawJsonParam(RestTemplate template, String cseUrlPrefix) throws Exception { Map<String, String> person = new HashMap<>(); person.put("name", "Tom"); String jsonPerson = RestObjectMapperFactory.getRestObjectMapper().writeValueAsString(person); TestMgr.check("hello Tom", template.postForObject(cseUrlPrefix + "/compute/testrawjson", jsonPerson, String.class)); }
From source file:io.pivotal.strepsirrhini.chaoslemur.infrastructure.StandardDirectorUtils.java
private static String getBoshDirectorUaaToken(String host, String directorName, String password) throws GeneralSecurityException { SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).useTLS() .build();/* w w w . jav a2 s. c o m*/ SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier()); HttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling() .setSSLSocketFactory(connectionFactory).build(); RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient)); MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); String base64Passowrd = encodePassword(directorName, password); headers.add("Authorization", "Basic " + base64Passowrd); headers.add("Content-Type", "application/x-www-form-urlencoded"); String postArgs = "grant_type=client_credentials"; HttpEntity<String> requestEntity = new HttpEntity<String>(postArgs, headers); String uri = "https://" + host + ":8443/oauth/token"; UaaToken response = restTemplate.postForObject(uri, requestEntity, UaaToken.class); log.info("Uaa token:" + response); return response.getAccess_token(); }
From source file:org.apache.servicecomb.demo.springmvc.client.SpringmvcClient.java
private static void testSpringMvcDefaultValues(RestTemplate template, String microserviceName) { String cseUrlPrefix = "cse://" + microserviceName + "/SpringMvcDefaultValues/"; //default values HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); String result = template.postForObject(cseUrlPrefix + "/form", request, String.class); TestMgr.check("Hello 20bobo", result); headers = new HttpHeaders(); HttpEntity<String> entity = new HttpEntity<>(null, headers); result = template.postForObject(cseUrlPrefix + "/header", entity, String.class); TestMgr.check("Hello 20bobo30", result); result = template.getForObject(cseUrlPrefix + "/query?d=10", String.class); TestMgr.check("Hello 20bobo4010", result); boolean failed = false; try {/* w w w .ja v a 2s . co m*/ result = template.getForObject(cseUrlPrefix + "/query2", String.class); } catch (InvocationException e) { failed = true; TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST); } failed = false; try { result = template.getForObject(cseUrlPrefix + "/query2?d=2&e=2", String.class); } catch (InvocationException e) { failed = true; TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST); } TestMgr.check(failed, true); failed = false; try { result = template.getForObject(cseUrlPrefix + "/query2?a=&d=2&e=2", String.class); } catch (InvocationException e) { failed = true; TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST); } TestMgr.check(failed, true); result = template.getForObject(cseUrlPrefix + "/query2?d=30&e=2", String.class); TestMgr.check("Hello 20bobo40302", result); failed = false; try { result = template.getForObject(cseUrlPrefix + "/query3?a=2&b=2", String.class); } catch (InvocationException e) { failed = true; TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST); } TestMgr.check(failed, true); result = template.getForObject(cseUrlPrefix + "/query3?a=30&b=2", String.class); TestMgr.check("Hello 302", result); result = template.getForObject(cseUrlPrefix + "/query3?a=30", String.class); TestMgr.check("Hello 30null", result); //input values headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<Map<String, String>> requestPara = new HttpEntity<>(null, headers); result = template.postForObject(cseUrlPrefix + "/form?a=30&b=sam", requestPara, String.class); TestMgr.check("Hello 30sam", result); headers = new HttpHeaders(); headers.add("a", "30"); headers.add("b", "sam"); headers.add("c", "40"); entity = new HttpEntity<>(null, headers); result = template.postForObject(cseUrlPrefix + "/header", entity, String.class); TestMgr.check("Hello 30sam40", result); result = template.getForObject(cseUrlPrefix + "/query?a=3&b=sam&c=5&d=30", String.class); TestMgr.check("Hello 3sam530", result); result = template.getForObject(cseUrlPrefix + "/query2?a=3&b=4&c=5&d=30&e=2", String.class); TestMgr.check("Hello 345302", result); }
From source file:eu.falcon.semantic.client.DenaClient.java
public static String addInstances(String fileClassPath, String format) { RestTemplate restTemplate = new RestTemplate(); LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); //final String uri = "http://localhost:8090/api/v1/ontology/instances/publish"; final String uri = "http://falconsemanticmanager.euprojects.net/api/v1/ontology/instances/publish"; map.add("file", new ClassPathResource(fileClassPath)); map.add("format", format); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<LinkedMultiValueMap<String, Object>>( map, headers);//from ww w . j a va 2 s . c om String result = restTemplate.postForObject(uri, entity, String.class); return result; }
From source file:eu.falcon.semantic.client.DenaClient.java
public static String publishOntology(String fileClassPath, String format, String dataset) { RestTemplate restTemplate = new RestTemplate(); LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); final String uri = "http://localhost:8090/api/v1/ontology/publish"; //final String uri = "http://falconsemanticmanager.euprojects.net/api/v1/ontology/publish"; map.add("file", new ClassPathResource(fileClassPath)); map.add("format", format); map.add("dataset", dataset); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<LinkedMultiValueMap<String, Object>>( map, headers);/*from ww w .j a va 2s .c o m*/ String result = restTemplate.postForObject(uri, entity, String.class); return result; }