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文件下载功能 ...
随机推荐
- hdu 5981 Guess the number
Guess the number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 160000/160000 K (Java/Other ...
- Ceph Monitor基础架构与模块详解
转自:https://www.ustack.com/blog/ceph-monitor/ Ceph rados cluster离不开Monitor,如果没有Monitor,则Ceph将无法执行一条简单 ...
- 【IDEA】笔记
引言 IDEA是JAVA开发的一个神器,熟悉它能极大提高我们的开发效率.正所谓工欲善其事,必先利其器. 快捷键 快捷键 介绍 Ctrl + F 在当前文件进行文本查找 (必备) Ctrl + R 在当 ...
- LeetCode OJ:Reverse Linked List II(反转链表II)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- “一键”知道自己的IP地址和网络供应商
打开浏览器,然后在地址栏里面输入“www.baidu.com” 进入百度主页以后,在搜索框内输入 “ip”,然后回车就可以了
- Golang的接口
当一只鸟走路像鸭子,游泳像鸭子,叫起来也像鸭子,那么我们就认为它就是鸭子. Duck typing 的理念因此比喻得名. Golang 通过 interface 实现 duck typing. Eff ...
- Mac安装并破解StarUML
1.下载与安装这里不做赘述,直接去官网下载即可 http://staruml.io/ 2.关键的事情来了,破解 一.打开安装包的目录 cd /Applications/StarUML.app/Cont ...
- ASP调用存储过程访问SQL Server
ASP调用存储过程访问SQL Server 2011-02-15 10:22:57 标签:asp 数据库 sQL 存储过程 Server ASP和存储过程(Stored Procedures)的文章 ...
- 【Java 并发】Executor框架机制与线程池配置使用
[Java 并发]Executor框架机制与线程池配置使用 一,Executor框架Executor框架便是Java 5中引入的,其内部使用了线程池机制,在java.util.cocurrent 包下 ...
- php curl请求。header头中添加请求信息
function get_data($key,$authorization,$url){ $headers = array( 'api-key:'.$key, 'authorization ...