springboot成神之——RestTemplate访问Rest
本文介绍RestTemplate访问Rest
demo
package com.springlearn.learn;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON_UTF8}));
// Get
// HttpEntity<String> entity = new HttpEntity<String>(headers);
// RestTemplate restTemplate = new RestTemplate();
// ResponseEntity<String> response = restTemplate.exchange("https://www.baidu.com/s?wd=a", HttpMethod.GET, entity,String.class);
// Post
// HttpEntity<Object> entity = new HttpEntity<>(new Object[]{"1"}, headers);
// RestTemplate restTemplate = new RestTemplate();
// Object response = restTemplate.postForObject("https://www.baidu.com/s?wd=a", entity, Object.class);
// Put 和Post类似
// 使用put方法即可
// 或者使用restTemplate.exchange("...", HttpMethod.PUT, entity, String.class);
// Delete 直接delete即可
System.out.println("结果:"+response);
SpringApplication.run(DemoApplication.class, args);
}
}
springboot成神之——RestTemplate访问Rest的更多相关文章
- springboot成神之——ioc容器(依赖注入)
springboot成神之--ioc容器(依赖注入) spring的ioc功能 文件目录结构 lang Chinese English GreetingService MyRepository MyC ...
- java成神之——HttpURLConnection访问api
HttpURLConnection 访问get资源 访问post资源 访问Delete资源 获取状态码 结语 HttpURLConnection 访问get资源 HttpURLConnection c ...
- springboot成神之——springboot入门使用
springboot创建webservice访问mysql(使用maven) 安装 起步 spring常用命令 spring常见注释 springboot入门级使用 配置你的pom.xml文件 配置文 ...
- springboot成神之——mybatis和mybatis-generator
项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...
- springboot成神之——swagger文档自动生成工具
本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...
- springboot成神之——监视器
Spring Boot 的监视器 依赖 配置 书写监视控制器 常用的一些内置endpoint 定义actuator/info特殊endpoint actuator/shutdown需要post请求才能 ...
- springboot成神之——log4j2的使用
本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- springboot成神之——spring文件下载功能
本文介绍spring文件下载功能 目录结构 DemoApplication WebConfig TestController MediaTypeUtils 前端测试 本文介绍spring文件下载功能 ...
随机推荐
- 三十三 Python分布式爬虫打造搜索引擎Scrapy精讲—数据收集(Stats Collection)
Scrapy提供了方便的收集数据的机制.数据以key/value方式存储,值大多是计数值. 该机制叫做数据收集器(Stats Collector),可以通过 Crawler API 的属性 stats ...
- hp g6 2328tx 加装ssd 机械硬盘安装到光驱位置 问题小结
惠普 g6 笔记本从13年入手至今,三年有余,性能啥的开始跟不上了,所有入手了一块ssd,闪迪(sanDisk)加强版 240G的固态硬盘,准备升下级.狗东下午下单,第二天早上7点多到的,把老硬盘换下 ...
- Qt中QT_BEGIN_NAMESPACE和QT_END_NAMESPACE的作用
在Qt中,我们经常会看到 QT_BEGIN_NAMESPACE class QAction; class QMenu; class QPlainTextEdit; QT_END_NAMESPACE 这 ...
- linux TCP Fast Open开启和测试
linux上要开启TCP Fast Open,内核版本至少为3.7.0, 且需要设置 /proc/sys/net/ipv4/tcp_fastopen 为3. 开启后,如果有连接进来,使用如下命令查看: ...
- 演示使用Metasploit入侵Windows
我使用Kali Linux的IP地址是192.168.0.112:在同一局域网内有一台运行Windows XP(192.168.0.108)的测试电脑. 本文演示怎么使用Metasploit入侵win ...
- LINUX 命令—netstat [简单实用]
1.--当我们在检查程序是否启动或者网络状况的时候 会查看本机活跃的端口,就需要这个命令: |--"netstat – Print network connections, routing ...
- Notepad++如何取消打开最近的历史文件
1.设置 2.首选项 3.备份 4.取消勾选 "Remember current session for next launch" 5.重新启动即可. 出处:http://www. ...
- Java开发前期准备工作
配置Java开发环境变量 在"系统变量"中设置3项属性,JAVA_HOME, PATH, CLASSPATH. 变量设置参数如下: 变量名:JAVA_HOME 变量值:C:\Pro ...
- Jmeter ----关于上传图片接口
转自:http://www.cnblogs.com/linglingyuese/p/4514808.html 需求 1 2 3 4 5 6 7 8 9 post上传 Request: { &quo ...
- windows下通过Git Bash使用Git常用命令
Git跟SVN最大不同的地方就是分布式.SVN的集中式与Git的分布式决定各自的业务场景.既然是分布式的,那么大部分操作就是本地操作.一般Git操作都是通过IDE,比如Eclipse,如果装了Git ...