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);
    }
}

原文地址:https://www.cnblogs.com/ye-hcj/p/9627636.html

时间: 2024-07-29 20:54:46

springboot成神之——RestTemplate访问Rest的相关文章

java成神之——HttpURLConnection访问api

HttpURLConnection 访问get资源 访问post资源 访问Delete资源 获取状态码 结语 HttpURLConnection 访问get资源 HttpURLConnection connection = (HttpURLConnection)new URL("http://ip/test").openConnection(); int responseCode = connection.getResponseCode(); InputStream inputStre

springboot成神之——监视器

Spring Boot 的监视器 依赖 配置 书写监视控制器 常用的一些内置endpoint 定义actuator/info特殊endpoint actuator/shutdown需要post请求才能访问 Spring Boot 的监视器 此功能用来控制spring boot程序和查看程序信息 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot

springboot成神之——拦截器

本文介绍spring boot拦截器 创建拦截器类LogInterceptor.java 创建拦截器类OldLoginInterceptor.java 拦截器配置类WebMvcConfig.java 路由InterceptorController.java 本文介绍spring boot拦截器 创建拦截器类LogInterceptor.java package com.springlearn.learn.interceptor; import javax.servlet.http.HttpSer

springboot成神之——websocket发送和请求消息

本文介绍如何使用websocket发送和请求消息 项目目录 依赖 DemoApplication MessageModel WebConfig WebSocketConfig HttpHandshakeInterceptor WebSocketEventListener WebSocketController 前端测试 本文介绍如何使用websocket发送和请求消息 项目目录 依赖 <dependency> <groupId>org.springframework.boot<

如何使用RestTemplate访问restful服务

https://www.jianshu.com/p/c9644755dd5e 一. 什么是RestTemplate 传统情况下在java代码里访问restful服务,一般使用Apache的HttpClient.不过此种方法使用起来太过繁琐.spring提供了一种简单便捷的模板类来进行操作,这就是RestTemplate. 二.一个简单的例子. 定义一个简单的restful接口 @RestController public class TestController { @RequestMappin

java成神之——注释修饰符

注释修饰符 自定义注释 元注释 通过反射在runtime访问注释 内置注释 多注释实例 错误写法 使用容器改写 使用@Repeatable元注释 注释继承 使用反射获取注释 获取类的注释 获取方法的注释 结语 注释修饰符 自定义注释 元注释 用来注释自定义注释的注释 @Target 限定注释允许加载的目标 @Target(ElementType.METHOD) 只能用于方法 @Target({ElementType.FIELD, ElementType.TYPE}) 可以用于字段和类型 ANNO

java成神之——文件IO

文件I/O Path Files File类 File和Path的区别和联系 FileFilter FileOutputStream FileInputStream 利用FileOutputStream和FileInputStream复制文件 FileWriter FileReader BufferedWriter BufferedReader 基本用法 StringWriter InputStreams转换成字符串 BufferedInputStream ObjectOutputStream

前端性能优化成神之路-总结

首先来看一下前端性能优化所涉及的层面有如下四个:网络层面,构建层面,浏览器渲染层面,服务端层面 具体的优化点有:资源合并与压缩,图片编码原理和类型的选择,浏览器的渲染机制,懒加载与预加载,浏览器存储,缓存机制,PWA,Vue-SSR等等 首先来了解一下web前端的本质 web前端的本质是一种GUI软件,是可以直接借鉴其他GUI系统架构设计的方法,但是web前端有点特别 下面是CS架构的GUI软件的开发和部署过程,CS架构的GUI软件在用户从商店下载下来后,是一个APK包,通过解压安装到手机的操作

(030)Spring Boot之RestTemplate访问web服务案例

每一个springboot工程都可以看做一个服务,这也是微服务的基础,使用RestTemplate访问springboot提供的web服务.如下: String BASE_URL="http://127.0.0.1:8080"; RestTemplate res=new RestTemplate(); String body= res.getForObject(BASE_URL+"/soa/product/20",String.class);//请求服务,返回jso