當前位置: 首頁>>代碼示例>>Java>>正文


Java TestRestTemplate.postForObject方法代碼示例

本文整理匯總了Java中org.springframework.boot.test.web.client.TestRestTemplate.postForObject方法的典型用法代碼示例。如果您正苦於以下問題:Java TestRestTemplate.postForObject方法的具體用法?Java TestRestTemplate.postForObject怎麽用?Java TestRestTemplate.postForObject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.boot.test.web.client.TestRestTemplate的用法示例。


在下文中一共展示了TestRestTemplate.postForObject方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: verifyAuthentication

import org.springframework.boot.test.web.client.TestRestTemplate; //導入方法依賴的package包/類
private void verifyAuthentication(ClientDetails config, HttpStatus finalStatus) {
	String baseUrl = "http://localhost:" + this.context.getWebServer().getPort();
	TestRestTemplate rest = new TestRestTemplate();
	// First, verify the web endpoint can't be reached
	assertEndpointUnauthorized(baseUrl, rest);
	// Since we can't reach it, need to collect an authorization token
	HttpHeaders headers = getHeaders(config);
	String url = baseUrl + "/oauth/token";
	JsonNode tokenResponse = rest.postForObject(url,
			new HttpEntity<>(getBody(), headers), JsonNode.class);
	String authorizationToken = tokenResponse.findValue("access_token").asText();
	String tokenType = tokenResponse.findValue("token_type").asText();
	String scope = tokenResponse.findValues("scope").get(0).toString();
	assertThat(tokenType).isEqualTo("bearer");
	assertThat(scope).isEqualTo("\"read\"");
	// Now we should be able to see that endpoint.
	headers.set("Authorization", "BEARER " + authorizationToken);
	ResponseEntity<String> securedResponse = rest
			.exchange(new RequestEntity<Void>(headers, HttpMethod.GET,
					URI.create(baseUrl + "/securedFind")), String.class);
	assertThat(securedResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
	assertThat(securedResponse.getBody()).isEqualTo(
			"You reached an endpoint " + "secured by Spring Security OAuth2");
	ResponseEntity<String> entity = rest.exchange(new RequestEntity<Void>(headers,
			HttpMethod.POST, URI.create(baseUrl + "/securedSave")), String.class);
	assertThat(entity.getStatusCode()).isEqualTo(finalStatus);
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:28,代碼來源:OAuth2AutoConfigurationTests.java

示例2: verifyAuthentication

import org.springframework.boot.test.web.client.TestRestTemplate; //導入方法依賴的package包/類
private void verifyAuthentication(ClientDetails config, HttpStatus finalStatus) {
	String baseUrl = "http://localhost:"
			+ this.context.getEmbeddedServletContainer().getPort();
	TestRestTemplate rest = new TestRestTemplate();
	// First, verify the web endpoint can't be reached
	assertEndpointUnauthorized(baseUrl, rest);
	// Since we can't reach it, need to collect an authorization token
	HttpHeaders headers = getHeaders(config);
	String url = baseUrl + "/oauth/token";
	JsonNode tokenResponse = rest.postForObject(url,
			new HttpEntity<MultiValueMap<String, Object>>(getBody(), headers),
			JsonNode.class);
	String authorizationToken = tokenResponse.findValue("access_token").asText();
	String tokenType = tokenResponse.findValue("token_type").asText();
	String scope = tokenResponse.findValues("scope").get(0).toString();
	assertThat(tokenType).isEqualTo("bearer");
	assertThat(scope).isEqualTo("\"read\"");
	// Now we should be able to see that endpoint.
	headers.set("Authorization", "BEARER " + authorizationToken);
	ResponseEntity<String> securedResponse = rest
			.exchange(new RequestEntity<Void>(headers, HttpMethod.GET,
					URI.create(baseUrl + "/securedFind")), String.class);
	assertThat(securedResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
	assertThat(securedResponse.getBody()).isEqualTo(
			"You reached an endpoint " + "secured by Spring Security OAuth2");
	ResponseEntity<String> entity = rest.exchange(new RequestEntity<Void>(headers,
			HttpMethod.POST, URI.create(baseUrl + "/securedSave")), String.class);
	assertThat(entity.getStatusCode()).isEqualTo(finalStatus);
}
 
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:30,代碼來源:OAuth2AutoConfigurationTests.java


注:本文中的org.springframework.boot.test.web.client.TestRestTemplate.postForObject方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。