zoukankan      html  css  js  c++  java
  • 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);
    	}
    }
    
    
  • 相关阅读:
    虚方法 C++快速入门23
    抽象方法 C++快速入门24
    网址探测器 零基础入门学习Delphi41
    网址探测器 零基础入门学习Delphi41
    抽象方法 C++快速入门24
    虚方法 C++快速入门23
    运算符重载 C++快速入门25
    静态属性和静态方法2 C++快速入门22
    linux系统中的文件访问控制列表ACL
    linux系统中的文件类型
  • 原文地址:https://www.cnblogs.com/ye-hcj/p/9627636.html
Copyright © 2011-2022 走看看