<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:sec="http://www.springframework.org/schema/security" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <bean id="template" class="org.springframework.web.client.RestTemplate"> <constructor-arg> <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/> </constructor-arg> </bean> </beans>
import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; import java.net.URI; /** * <pre> * Example01.java * @author kanpiaoxue<br> * @version 1.0 * Create Time 2015年1月20日 下午2:59:00<br> * Description :类实现描述 * </pre> */ public class Example01 { public static URI createUri(String host, int port, String path, Object... args) { UriComponents uriComponents = UriComponentsBuilder.newInstance() .port(port).scheme("http").host(host).path(path).build(); if (null != args && args.length > 0) { uriComponents.expand(args); } uriComponents.encode(); return uriComponents.toUri(); } /** * <pre> * @param args * </pre> */ public static void init(String[] args) { context = new FileSystemXmlApplicationContext( "D:/baidu/workspaces/workspace_java/test/src/main/java/com/baidu/learn/spring/restTemplate/spring-ctx-application.xml"); template = context.getBean("template", RestTemplate.class); } public static void main(String[] args) { init(args); // =========================== Example01 e = new Example01(); e.testHttpCode(); e.testHttpLog(); } private static ApplicationContext context; private static RestTemplate template; private final String url = "http://cp01-rd-crm-cdc-db05.cp01.baidu.com:8097/dispatch-cdc-etl-test-leader-4597-20150120041830-ShellRunner-0.out"; /** * <pre> * @param template * @param url * @return 是否2xx的成功状态 * </pre> */ private boolean is2xxSuccessful(RestTemplate template, String url) { HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "text/html"); headers.add( "Accept", "text/html,application/xhtml+xml,application/xml,application/json;q=0.9,image/webp,*/*;q=0.8"); headers.add("Accept-Encoding", "gzip, deflate, sdch"); headers.add("Cache-Control", "max-age=0"); headers.add("Connection", "keep-alive"); HttpEntity<String> requestEntity = new HttpEntity<String>(headers); ResponseEntity<String> responseEntity = template.exchange(url, HttpMethod.GET, requestEntity, String.class); HttpStatus status = responseEntity.getStatusCode(); return status.is2xxSuccessful(); } /** * <pre> * @param template * @param url * @return 读取远程url的文件内容 * </pre> */ private String readContentFromRemote(RestTemplate template, String url) { ResponseEntity<String> entity = template .getForEntity(url, String.class); return entity.getBody(); } private void testHttpCode() { boolean is2xxSuccessful = is2xxSuccessful(template, url); System.out.println(String.format("url:%s : %s", url, is2xxSuccessful)); } private void testHttpLog() { String body = readContentFromRemote(template, url); System.out.println(body); } }
相关推荐
Spring 3.0 RestTemplate
spring rest template 封装,支持文件下载,超时属性设置,http rest接口调用
Riptide 是Spring RestTemplate的一个扩展,提供我们所说的客户端响应路由(client-side response routing)
工作中常用的远程调用请求工具类,支持请求方法:GET、POST、PUT、DELETE,支持媒体格式:text/plain、text/xml、application/json、application/x-www-form-urlencoded,支持请求协议:HTTP、HTTPS,该工具类直接可...
RestTemplate是执行HTTP请求的同步阻塞式的客户端,它在HTTP客户端库(例如JDK HttpURLConnection,Apache HttpComponents,okHttp等)基础封装了更加简单易用的模板方法API。也就是说RestTemplate是一个封装,底层...
主要给大家介绍了Spring Batch读取txt文件并写入数据库的方法,SpringBatch 是一个轻量级、全面的批处理框架。这里我们用它来实现文件的读取并将读取的结果作处理,处理之后再写入数据库中的功能。需要的朋友可以...
主要介绍了Spring RestTemplate基本使用介绍,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了Spring RestTemplate具体使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring源码分析:配置文件读取流程 - ImportNew
我们使用Spring Boot编写多个模块开发时,我们希望各个模块使用各自的配置文件,避免将整个项目的配置文件都写在一起,从而降低各个模块之间的耦合度。
在接入Spring-Cloud-Gateway时,可能有需求进行缓存Json-Body数据或者Form-Urlencoded数据的情况。这篇文章主要介绍了SpringCloud Finchley Gateway 缓存请求Body和Form表单的实现,感兴趣的小伙伴们可以参考一下
主要介绍了如何使用Spring RestTemplate访问restful服务,详细的介绍了什么是RestTemplate以及简单实现,非常具有实用价值,需要的朋友可以参考下
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
spring读取properties.
本篇文章主要介绍了Spring使用RestTemplate模拟form提交示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了spring boot读取Excel操作,结合实例形式详细分析了spring boot解析、读取Excel相关操作技巧,需要的朋友可以参考下
《深入理解Spring Cloud与微服务构建》学习笔记(十一)~使用RestTemplate和Ribbo消费服务
微服务springcloud:eureka整合gateway、RestTemplate和FeignClient
基于Spring RestTemplate的etcd客户端 要使用此功能, me.itzg.etcd.EtcdService配置为Spring Bean。
spring 支持@RequestBody注解依赖包 、 spring 支持@RequestBody注解依赖包 spring 支持@RequestBody注解依赖包