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文件下载功能 ...
随机推荐
- Ajax基础(二)--获取服务器文件
获取服务器文件相关步骤: 1.创建文件: 2.创建XMLHttpRequest对象: 3.获取文件(注意事项:1)在服务器中运行测试:2)注意编码问题,编码要统一). 3.1 获取xml文件: HTM ...
- colorlog的三个例子
例1:默认的log_colors import logging from logging.handlers import RotatingFileHandler from colorlog impor ...
- vue-cli favicon 怎么引入
把你的 favicon 图标放到 static 目录下,然后修改 link 标签指向 static 目录下的 favicon 图标文件即可
- ubuntu下tar.bz2解压错误
在执行tar -jxvf firefox-latest-x86_64.tar.bz2是出现了如下错误: 出错原因是: (1)帐号权限不足造成的 (2)原tar包损坏 (3)可能磁盘空间不足,可以用命令 ...
- H264的编解码流程?
- Angular提示文件不是一个有效的模块
ERROR in src/app/app.component.ts(2,23): error TS2306: File '/home/jerry/angular/todo/src/app/model. ...
- Unity3D开发之Matrix4x4矩阵变换
在Unity开发中时常会用到Matrix4x4矩阵来变换场景中对象的位置.旋转和缩放.但是很多人都不太理解这儿Matrix4x4变换矩阵.通过DX中的变换矩阵我来讲一讲在unity中这个变换矩阵是怎么 ...
- Unity3d 相关文档
Unity3D研究院之处理摄像机跟随避免相机穿墙拉近或透明的方法 http://www.apkbus.com/android-498-1.html
- 怎么把openrety 里边的 table 优雅的打印出来
1.安装 loarocks 库以后 2.安装 Penlight 插件 3.如下图所示 4.利用dump 函数优雅的打印 table
- word中如何将空格变成换行
大家在工作和学习中可能会遇到文字替换或符号替换,大家要学会txt.doc.xls之间的切换,替换好之后放到最终的文件中,txt好处是没有格式,doc个好处是有格式,而xls主要是分配到单元格中. 那么 ...