本文整理汇总了Java中org.springframework.web.client.RestTemplate.getForEntity方法的典型用法代码示例。如果您正苦于以下问题:Java RestTemplate.getForEntity方法的具体用法?Java RestTemplate.getForEntity怎么用?Java RestTemplate.getForEntity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.client.RestTemplate
的用法示例。
在下文中一共展示了RestTemplate.getForEntity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getResourcesFromGet
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Get the resources for a given URL. It can throw a number of RuntimeExceptions (connection not
* found etc - which are all wrapped in a RestClientException).
*
* @param rt the RestTemplate to use
* @param targetURI the url to access
* @return the returns object or null if not found
*/
public R getResourcesFromGet(final RestTemplate rt, final URI targetURI) {
ResponseEntity<R> resp = rt.getForEntity(targetURI, getTypeClass());
if (resp != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is not null: " + resp.getStatusCode());
}
if (resp.getStatusCode() == HttpStatus.OK) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is OK");
}
this.processHeaders(targetURI, resp.getHeaders());
return resp.getBody();
} else {
return null;
}
} else {
return null;
}
}
示例2: getResourcesFromGet
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Override
protected List<JsonRole> getResourcesFromGet(final RestTemplate rt, final URI targetURI)
throws HttpStatusCodeException, UpdaterHttpException {
ResponseEntity<JsonRoles> resp = rt.getForEntity(targetURI, JsonRoles.class);
if (resp != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is not null: " + resp.getStatusCode());
}
if (resp.getStatusCode() == HttpStatus.OK) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is OK");
}
return resp.getBody().getRoles();
} else {
throw new UpdaterHttpException(
"unable to collect roles - status code: " + resp.getStatusCode().toString());
}
} else {
throw new UpdaterHttpException("unable to collect roles - HTTP response was null");
}
}
示例3: getResourcesFromGet
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Override
protected List<JsonDomain> getResourcesFromGet(final RestTemplate rt, final URI targetURI)
throws HttpStatusCodeException, UpdaterHttpException {
ResponseEntity<JsonDomains> resp = rt.getForEntity(targetURI, JsonDomains.class);
if (resp != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is not null: " + resp.getStatusCode());
}
if (resp.getStatusCode() == HttpStatus.OK) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is OK");
}
return resp.getBody().getDomains();
} else {
throw new UpdaterHttpException(
"unable to collect domains - status code: " + resp.getStatusCode().toString());
}
} else {
throw new UpdaterHttpException("unable to collect domains - HTTP response was null");
}
}
示例4: testErrorsSerializedAsJsonApi
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Test
public void testErrorsSerializedAsJsonApi() throws IOException {
RestTemplate testRestTemplate = new RestTemplate();
try {
testRestTemplate
.getForEntity("http://localhost:" + this.port + "/doesNotExist", String.class);
Assert.fail();
}
catch (HttpClientErrorException e) {
assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
String body = e.getResponseBodyAsString();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(JacksonModule.createJacksonModule());
Document document = mapper.readerFor(Document.class).readValue(body);
Assert.assertEquals(1, document.getErrors().size());
ErrorData errorData = document.getErrors().get(0);
Assert.assertEquals("404", errorData.getStatus());
Assert.assertEquals("Not Found", errorData.getTitle());
Assert.assertEquals("No message available", errorData.getDetail());
}
}
示例5: main
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public static void main(String[] args) {
// TODO Auto-generated method stub
RestTemplate template = new RestTemplate();
Book book = template.getForObject("http://localhost:8080/Ch11_Spring_Reactive_Web/books/14", Book.class);
System.out.println(book.getAuthor() + "\t" + book.getISBN());
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<List> responseEntity = template
.getForEntity("http://localhost:8080/Ch11_Spring_Reactive_Web/books", List.class);
List<ArrayList<Book>> books = responseEntity.getBody();
int i = 0;
for (ArrayList l : books) {
for (int j = 0; i < l.size(); j++) {
LinkedHashMap<String, Book> map = (LinkedHashMap<String, Book>) l.get(i);
Set<Entry<String, Book>> set = map.entrySet();
System.out.println("***\tBook:-"+i +"\t****");
for (Entry<String, Book> entry : set) {
System.out.print(entry.getValue() + "\t");
}
i++;
System.out.println();
}
}
}
示例6: TracingRestTemplateTest
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public TracingRestTemplateTest() {
final RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.<ClientHttpRequestInterceptor>singletonList(
new TracingRestTemplateInterceptor(mockTracer)));
client = new Client<RestTemplate>() {
@Override
public <T> ResponseEntity<T> getForEntity(String url, Class<T> clazz) {
return restTemplate.getForEntity(url, clazz);
}
@Override
public RestTemplate template() {
return restTemplate;
}
};
mockServer = MockRestServiceServer.bindTo(client.template()).build();
}
示例7: serviceHttpCollection
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
private ResponseEntity<String> serviceHttpCollection ( String serviceName, ObjectNode httpConfig )
throws IOException {
String httpCollectionUrl = httpConfig
.get( "httpCollectionUrl" )
.asText();
JsonNode user = httpConfig.get( "user" );
JsonNode pass = httpConfig.get( "pass" );
if ( httpConfig.has( Application.getCurrentLifeCycle() ) ) {
user = httpConfig
.get( Application.getCurrentLifeCycle() )
.get( "user" );
pass = httpConfig
.get( Application.getCurrentLifeCycle() )
.get( "pass" );
}
RestTemplate localRestTemplate = getRestTemplate( serviceCollector.getMaxCollectionAllowedInMs(), user,
pass, serviceName + " collection password" );
ResponseEntity<String> collectionResponse;
if ( Application.isRunningOnDesktop() && httpCollectionUrl.startsWith( "classpath" ) ) {
File stubResults = new File( getClass()
.getResource( httpCollectionUrl.substring( httpCollectionUrl.indexOf( ":" ) + 1 ) )
.getFile() );
logger.warn( "******** Application.isRunningOnDesktop() - using: " + stubResults
.getAbsolutePath() );
collectionResponse = new ResponseEntity<String>( FileUtils.readFileToString( stubResults ),
HttpStatus.OK );
} else {
collectionResponse = localRestTemplate.getForEntity( httpCollectionUrl, String.class );
// logger.debug("Raw Response: \n{}",
// collectionResponse.toString());
}
return collectionResponse;
}
示例8: getResourcesFromGet
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Override
protected List<JsonProject> getResourcesFromGet(final RestTemplate rt, final URI targetURI)
throws HttpStatusCodeException, UpdaterHttpException {
ResponseEntity<JsonProjects> resp = rt.getForEntity(targetURI, JsonProjects.class);
if (resp != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is not null: " + resp.getStatusCode());
}
if (resp.getStatusCode() == HttpStatus.OK) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is OK");
}
return resp.getBody().getProjects();
} else {
throw new UpdaterHttpException(
"unable to collect projects - status code: " + resp.getStatusCode().toString());
}
} else {
throw new UpdaterHttpException("unable to collect projects - HTTP response was null");
}
}
示例9: getResourcesFromGet
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Override
protected List<JsonRoleAssignment> getResourcesFromGet(final RestTemplate rt, final URI targetURI)
throws HttpStatusCodeException, UpdaterHttpException {
ResponseEntity<JsonRoleAssignments> resp = rt.getForEntity(targetURI, JsonRoleAssignments.class);
if (resp != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is not null: " + resp.getStatusCode());
}
if (resp.getStatusCode() == HttpStatus.OK) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is OK");
}
return resp.getBody().getRoleAssignments();
} else {
throw new UpdaterHttpException(
"unable to collect roleAssigments - status code: " + resp.getStatusCode().toString());
}
} else {
throw new UpdaterHttpException("unable to collect roleAssignments - HTTP response was null");
}
}
示例10: getResourcesFromGet
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Override
protected List<JsonUser> getResourcesFromGet(final RestTemplate rt, final URI targetURI)
throws HttpStatusCodeException, UpdaterHttpException {
ResponseEntity<JsonUsers> resp = rt.getForEntity(targetURI, JsonUsers.class);
if (resp != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is not null: " + resp.getStatusCode());
}
if (resp.getStatusCode() == HttpStatus.OK) {
if (LOG.isDebugEnabled()) {
LOG.debug("response is OK");
}
return resp.getBody().getUsers();
} else {
throw new UpdaterHttpException(
"unable to collect users - status code: " + resp.getStatusCode().toString());
}
} else {
throw new UpdaterHttpException("unable to collect users - HTTP response was null");
}
}
示例11: main
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public static void main(String[] args) {
// TODO Auto-generated method stub
RestTemplate template = new RestTemplate();
ResponseEntity<Book[]> responseEntity=template.getForEntity("http://localhost:8081/Ch09_Spring_Rest_JDBC/books", Book[].class);
Book[] books=responseEntity.getBody();
for(Book book:books)
System.out.println(book.getAuthor()+"\t"+book.getISBN());
}
示例12: loginSucceeds
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Test
public void loginSucceeds() {
RestTemplate template = new TestRestTemplate("user", "foo");
ResponseEntity<String> response = template.getForEntity("http://localhost:" + port
+ "/user", String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
示例13: connect
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
private T connect(final PathBuilder config) throws RestClientException {
final RestTemplate restTemplate = new RestTemplate(httpMessageConverters);
final SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setReadTimeout(2000);
rf.setConnectTimeout(2000);
config.addParam("t", RestfulContext.getToken());
final ResponseEntity<T> re = restTemplate.getForEntity(config.buildEndpointURI(), dtoType);
return re.getBody();
}
示例14: sendGetCommand
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public static String sendGetCommand(String url) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<?> result = restTemplate.getForEntity(url, String.class);
String body = result.getBody().toString();
MediaType contentType = result.getHeaders().getContentType();
HttpStatus statusCode = result.getStatusCode();
logger.info("REST PUT COMMAND " + contentType + " " + statusCode);
return body;
}
示例15: isValidCustomerId
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public boolean isValidCustomerId(long customerId) {
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> entity = restTemplate.getForEntity(customerURL() + customerId, String.class);
return entity.getStatusCode().is2xxSuccessful();
} catch (final HttpClientErrorException e) {
if (e.getStatusCode().value() == 404)
return false;
else
throw e;
}
}