本文介绍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的更多相关文章

  1. springboot成神之——ioc容器(依赖注入)

    springboot成神之--ioc容器(依赖注入) spring的ioc功能 文件目录结构 lang Chinese English GreetingService MyRepository MyC ...

  2. java成神之——HttpURLConnection访问api

    HttpURLConnection 访问get资源 访问post资源 访问Delete资源 获取状态码 结语 HttpURLConnection 访问get资源 HttpURLConnection c ...

  3. springboot成神之——springboot入门使用

    springboot创建webservice访问mysql(使用maven) 安装 起步 spring常用命令 spring常见注释 springboot入门级使用 配置你的pom.xml文件 配置文 ...

  4. springboot成神之——mybatis和mybatis-generator

    项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...

  5. springboot成神之——swagger文档自动生成工具

    本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...

  6. springboot成神之——监视器

    Spring Boot 的监视器 依赖 配置 书写监视控制器 常用的一些内置endpoint 定义actuator/info特殊endpoint actuator/shutdown需要post请求才能 ...

  7. springboot成神之——log4j2的使用

    本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...

  8. springboot成神之——mybatis在spring-boot中使用的几种方式

    本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...

  9. springboot成神之——spring文件下载功能

    本文介绍spring文件下载功能 目录结构 DemoApplication WebConfig TestController MediaTypeUtils 前端测试 本文介绍spring文件下载功能 ...

随机推荐

  1. main函数的一点知识

    main函数参数意义. main函数执行前. main还是执行后.

  2. Linux操作系统中的文件目录结构详细介绍

    "/" :Linux文件系统的入口.也是最高一级的目录. "/bin":基本系统所需要的命令,功能和"/usr/bin"类似,这个目录下的文 ...

  3. Win7 x64安装Paramiko

    先说一下我的环境: win7 x64 旗舰版.Python3.5.0.pip8.1.0 pip install paramiko时报错如下: 大概意思: blablabla... 反正大概意思就是少G ...

  4. 从输入url到页面加载的过程

    用户端请求步骤:DNS解析URL地址.生成HTTP请求报文.构建TCP连接.使用IP协议选择传输路线.数据链路层保证数据的可靠传输.物理层将数据转换成电子.光学或微波信号进行传输 网络传输: 从客户机 ...

  5. 从无到有开发自己的Wordpress博客主题---运行环境准备

    目前只做本地开发,只在本地搭建测试环境,最后会在服务器的CentOS中搭建正式环境,我使用的是Mac,最简单的办法就是安装MAMP. 1.下载并安装MAMP 下载地址:https://www.mamp ...

  6. 解决pip安装太慢的问题

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  7. 设置eclipse显示代码错误提示的

    http://jingyan.baidu.com/article/f3e34a128d79aff5ea65356c.html

  8. 阿里云,腾讯云,等等的云 Ubuntu14.04升级16.04

    16.04有很多好处.在此不说了 这几天来回折腾了各种的云,然后发现国内的都没有16.04 但是ubuntu可以直接在线升级 在此记下来升级的过程 不管是腾讯云也好 阿里云也好,或者别的什么云,只要是 ...

  9. 剑指Offer面试题:11.调整数组顺序使奇数位于偶数前面

    一 题目:调整数组顺序使奇数位于偶数前面 题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 二 解题思路 如果不考虑时间复杂度 ...

  10. c++11新特性之宽窄字符转换

    C++11增加了unicode字面量的支持,可以通过L来定义宽字符:str::wstring str = L"中国人": 将宽字符转换为窄字符串需要用到codecvt库中的std: ...