日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

SpringBoot RestTemplate GET POST請求的實例講解

瀏覽:32日期:2023-04-24 15:18:26

一)RestTemplate簡介

RestTemplate是HTTP客戶端庫提供了一個更高水平的API。主要用于Rest服務調用。

RestTemplate方法:

方法組 描述

getForObject

通過GET檢索表示形式。

getForEntity

ResponseEntity通過使用GET 檢索(即狀態,標頭和正文)。

headForHeaders

通過使用HEAD檢索資源的所有標頭。

postForLocation

通過使用POST創建新資源,并Location從響應中返回標頭。

postForObject

通過使用POST創建新資源,并從響應中返回表示形式。

postForEntity

通過使用POST創建新資源,并從響應中返回表示形式。

put

通過使用PUT創建或更新資源。

patchForObject

通過使用PATCH更新資源,并從響應中返回表示形式。請注意,JDK HttpURLConnection不支持PATCH,但是Apache HttpComponents和其他支持。

delete

使用DELETE刪除指定URI處的資源。

optionsForAllow

通過使用ALLOW檢索資源的允許的HTTP方法。

exchange

前述方法的通用性強(且意見少的版本),在需要時提供了額外的靈活性。它接受RequestEntity(包括HTTP方法,URL,標頭和正文作為輸入)并返回ResponseEntity。

這些方法允許使用ParameterizedTypeReference而不是Class使用泛型來指定響應類型。

execute

執行請求的最通用方法,完全控制通過回調接口進行的請求準備和響應提取。

二)RestTemplate案例

第一步:創建一個maven項目,在pom.xml引入一個springboot的版本

pom.xml內容:

<project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.oysept</groupId> <artifactId>spring_resttemplate</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration> <mainClass>com.oysept.RestTemplateApplication</mainClass></configuration> </plugin> <plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId> </plugin> </plugins> </build></project>

application.yml配置:該配置就一個默認端口

server:

port: 8080

創建一個springboot啟動類RestTemplateApplication

package com.oysept; import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplicationpublic class RestTemplateApplication { public static void main(String[] args) { new SpringApplicationBuilder(RestTemplateApplication.class).run(args); }}

到此步驟時,可以先運行RestTemplateApplication中的main方法,檢驗springboot啟動是否正常。

第二步:創建一個RestTemplate配置類并注入,因為在使用時,不提前注入ResttTemplate,在通過@Autowired使用會報RestTemplate找不到

package com.oysept.config; import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate; /** * 注冊一個RestTemplate Bean, 否則直接通過@Autowired使用會報RestTemplate找不到 * @author ouyangjun */@Configurationpublic class RestTemplateConfig { /** * 方式一: 默認是使用JDK原生java.net.HttpURLConnection請求 * @return */ @Bean(name = 'restTemplate') public RestTemplate restTemplate() { return new RestTemplate(); } /** * 方式二: 使用apache http內置請求, 需要在pom.xml中引入相應的apache jar * 可以使用HttpClient,設置一些http連接池等信息 * @return * @Bean(name = 'restTemplate') public RestTemplate restTemplate() { return new RestTemplate(new HttpComponentsClientHttpRequestFactory()); } */ /** * 方式三: 使用OkHttp內置請求, 需要在pom.xml中引入相應的OkHttp3 jar * 可以使用OkHttpClient,設置一些http連接池信息 * @return * @Bean(name = 'restTemplate') public RestTemplate restTemplate() { return new RestTemplate(new OkHttp3ClientHttpRequestFactory()); } */}

第三步:創建一個VO類,用于測試入參和出參

package com.oysept.vo; public class MsgVO { private String msgKey; private String msgValue; public String getMsgKey() {return msgKey;} public void setMsgKey(String msgKey) {this.msgKey = msgKey;} public String getMsgValue() {return msgValue;} public void setMsgValue(String msgValue) {this.msgValue = msgValue;} public String toString() { return 'MsgVO [msgKey: '+this.msgKey+', msgValue: '+this.msgValue+']'; }}

第四步:創建一個服務端接口,用于測試

package com.oysept.controller; import java.util.ArrayList;import java.util.List; import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController; import com.oysept.vo.MsgVO; /** * 服務端, 提供接口被調用 * @author ouyangjun */@RestControllerpublic class ServerController { // 無參GET請求: http://localhost:8080/server/get @RequestMapping(value = '/server/get', method = RequestMethod.GET) public String get() { return '/server/get'; } // 帶參GET請求: http://localhost:8080/server/get/param?param=111222333444 @RequestMapping(value = '/server/get/param', method = RequestMethod.GET) public String getParam(@RequestParam(value = 'param') String param) { return '/server/get/param,' + param; } // 路徑中帶參GET請求: http://localhost:8080/server/get/url/AAAA/BBBB @RequestMapping(value = '/server/get/url/{one}/{two}', method = RequestMethod.GET) public String getUrl(@PathVariable('one') String one, @PathVariable('two') String two) { return '/get/url/{one}/{two},' + one + ',' + two; } // 無參GET請求, 返回List: http://localhost:8080/server/get/list @RequestMapping(value = '/server/get/list', method = RequestMethod.GET) public List<Object> getList() { List<Object> list = new ArrayList<Object>(); list.add(11); list.add('AA'); return list; } // 無參GET請求, 返回對象: http://localhost:8080/server/get/MsgVO @RequestMapping(value = '/server/get/MsgVO', method = RequestMethod.GET) public MsgVO getMsgVO() { MsgVO vo = new MsgVO(); vo.setMsgKey('keyAAA'); vo.setMsgValue('valueBBB'); return vo; } // POST請求, 表單參數, application/x-www-form-urlencoded @RequestMapping(value = '/server/post/form', method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public MsgVO postForm(MsgVO msgVO) { System.out.println('msgKey: ' + msgVO.getMsgKey() + ', msgValue: ' + msgVO.getMsgValue()); return msgVO; } // POST請求, JSON參數, application/json @RequestMapping(value = '/server/post/json', method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public MsgVO postJson(@RequestBody MsgVO msgVO) { System.out.println('msgKey: ' + msgVO.getMsgKey() + ', msgValue: ' + msgVO.getMsgValue()); return msgVO; }}

第五步:創建一個測試服務端接口的API

import的類和注入的RestTemplate:

package com.oysept.controller; import java.net.URI;import java.util.HashMap;import java.util.List;import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springframework.http.MediaType;import org.springframework.http.RequestEntity;import org.springframework.http.ResponseEntity;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import org.springframework.web.util.UriComponentsBuilder; import com.oysept.vo.MsgVO; /** * 客戶端, 調用服務端提供的接口 * @author ouyangjun */@RestControllerpublic class ClientController { // 使用默認請求方式 @Autowired @Qualifier(value = 'restTemplate') private RestTemplate restTemplate; // 在此處添加客戶端測試代碼}

1、GET請求

// 直接在瀏覽中輸入訪問地址: http://localhost:8080/client/get@RequestMapping(value = '/client/get', method = RequestMethod.GET)public String get() { // 無參GET請求 String get = restTemplate.getForObject('http://localhost:8080/server/get', String.class); System.out.println('==>/server/get return: ' + get); // 帶參GET請求 String getParam = restTemplate.getForObject('http://localhost:8080/server/get/param?param=111222333444', String.class); System.out.println('==>/server/get/param return: ' + getParam); // 帶參GET url請求 String getUrlParam = restTemplate.getForObject('http://localhost:8080/server/get/url/{one}/{two}', String.class, 'AAAA', 'BBBB'); System.out.println('==>/server/get/url/{one}/{two} return: ' + getUrlParam); // 帶參GET url請求 Map<String, String> vars = new HashMap<String, String>(); vars.put('one', 'HHHH'); vars.put('two', 'EEEE'); String getUrlVars = restTemplate.getForObject('http://localhost:8080/server/get/url/{one}/{two}', String.class, vars); System.out.println('==>/server/get/url/{one}/{two} return: ' + getUrlVars); // 無參GET請求, 返回List @SuppressWarnings('unchecked') List<String> getList = restTemplate.getForObject('http://localhost:8080/server/get/list', List.class); System.out.println('==>/server/get/list return: ' + getList); // GET請求, 返回對象 ResponseEntity<MsgVO> entity = restTemplate.getForEntity('http://localhost:8080/server/get/MsgVO', MsgVO.class); System.out.println('==>/server/get/list return: ' + entity.getBody()); return 'GET SUCCESS';}

2、GET url中傳參請求

// 直接在瀏覽中輸入訪問地址: http://localhost:8080/client/get/request// GET請求, url參數, 在表頭中添加參數@RequestMapping(value = '/client/get/request', method = RequestMethod.GET)public String getRequest() { // url中參數 Map<String, String> vars = new HashMap<String, String>(); vars.put('one', 'HHHH'); vars.put('two', 'EEEE'); // 請求地址 String uriTemplate = 'http://localhost:8080/server/get/url/{one}/{two}'; // 給URL地址encode轉碼 URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(vars).toUri(); // GET請求參數 RequestEntity<Void> requestEntity = RequestEntity.get(uri).header('MyHeader', 'aaabbbcccddd').build(); // 響應 ResponseEntity<String> response = restTemplate.exchange(requestEntity, String.class); // 結果 System.out.println('==>/get/request header: ' + response.getHeaders().getFirst('MyHeader')); System.out.println('==>/get/request body: ' + response.getBody()); return 'POST SUCCESS';}

3、POST application/x-www-form-urlencoded表單傳參請求

// 直接在瀏覽中輸入訪問地址: http://localhost:8080/client/postForm// POST請求, form表單入參@RequestMapping(value = '/client/postForm', method = RequestMethod.GET)public String postForm() { // uri String uriTemplate = 'http://localhost:8080/server/post/form'; // 設置請求頭為form形式: application/x-www-form-urlencoded HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // 設置參數, 和MsgVO中變量名對應 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add('msgKey', '1234'); map.add('msgValue', 'TestTest'); // 封裝請求參數 HttpEntity<MultiValueMap<String, String>> requestb = new HttpEntity<MultiValueMap<String, String>>(map, headers); ResponseEntity<String> response = restTemplate.postForEntity(uriTemplate, requestb, String.class); System.out.println('==>/server/post/form return: ' + response.getBody()); return 'POST SUCCESS';}

4、POST application/json JSON傳參請求

// 直接在瀏覽中輸入訪問地址: http://localhost:8080/client/postJson// POST請求, JSON入參@RequestMapping(value = '/client/postJson', method = RequestMethod.GET)public String postJson() { // json入參 MsgVO vo = new MsgVO(); vo.setMsgKey('TTT'); vo.setMsgValue('KKK'); String uriTemplate = 'http://localhost:8080/server/post/json'; URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand().toUri(); RequestEntity<MsgVO> requestEntity = RequestEntity.post(uri) .header('Content-Type', 'application/json; charset=UTF-8') .body(vo); ResponseEntity<MsgVO> response = restTemplate.exchange(requestEntity, MsgVO.class); System.out.println('==>/server/post/json return: ' + response.getBody()); return 'POST SUCCESS';}

項目結構圖:

SpringBoot RestTemplate GET POST請求的實例講解

以上這篇SpringBoot RestTemplate GET POST請求的實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
首页欧美精品中文字幕| 国产成人免费av一区二区午夜| 日韩在线第七页| 久久香蕉网站| 国产毛片一区| 免费在线日韩av| 精品久久电影| 7m精品国产导航在线| 久久av一区| 日韩国产高清在线| 国产精品.xx视频.xxtv| 国产欧美日韩精品高清二区综合区 | 99riav国产精品| 蜜桃传媒麻豆第一区在线观看| 日本麻豆一区二区三区视频| 麻豆久久一区| 免费av一区二区三区四区| 久久国产66| 狠狠久久伊人中文字幕| 99久久婷婷| 久久av免费看| 日韩av免费| 中文字幕一区二区三区在线视频| 7777精品| 国产日韩电影| 国产精品毛片| 国产九九精品| 99精品网站| 日韩区一区二| 日韩福利一区| 亚洲日韩视频| 国产日韩欧美一区| 国产精品精品国产一区二区| 欧美特黄一区| 嫩草伊人久久精品少妇av杨幂| 群体交乱之放荡娇妻一区二区| 美女精品一区| 福利在线免费视频| 日韩一区免费| 亚洲天堂av影院| 亚洲人成网站在线在线观看| 福利精品一区| 亚洲精品亚洲人成在线观看| 国产精品欧美一区二区三区不卡| 香蕉久久夜色精品国产| 亚洲永久精品唐人导航网址| 国产乱人伦丫前精品视频| 亚洲韩日在线| 国产精品白丝av嫩草影院| 午夜国产一区二区| 精品一区二区三区的国产在线观看| 欧美中文日韩| 日韩影院二区| 国产极品久久久久久久久波多结野| 免费视频亚洲| 精品视频网站| 亚洲精品激情| 日韩午夜免费| 激情亚洲影院在线观看| 国产福利资源一区| 一区二区三区四区精品视频| 日本精品不卡| 成人午夜毛片| 麻豆成人在线观看| 久久国产乱子精品免费女| 欧美在线综合| 日韩亚洲在线| 欧美日韩国产亚洲一区| av资源新版天堂在线| 国产欧美一级| 欧美一区成人| 日本午夜精品久久久| 综合色一区二区| 久久一二三区| 免费在线观看不卡| 日韩午夜黄色| 亚洲高清毛片| 久久伦理在线| 久久久久久久久丰满| 成人小电影网站| 成人精品高清在线视频| 国产成人精品三级高清久久91 | 98精品视频| 成人国产精选| 天堂√8在线中文| 国产一区福利| 国产一区二区三区四区| 黄色精品视频| 高清一区二区| 国产盗摄——sm在线视频| a国产在线视频| 久久天堂av| 欧美日韩国产一区精品一区| 国产精品免费看| 亚洲毛片一区| 日本欧美韩国一区三区| 欧美日韩一区二区三区不卡视频 | 蜜桃国内精品久久久久软件9| 免费污视频在线一区| 久久美女精品| 亚洲一区观看| 日本亚洲最大的色成网站www | 最新日韩欧美| 亚洲人成网站在线在线观看| 日韩一区二区三区精品视频第3页| 日韩精品免费视频人成| 欧美日韩伊人| 精品国产精品久久一区免费式| 久久精品日韩欧美| 国产夫妻在线| 夜久久久久久| 日韩中文字幕| 精品国产亚洲一区二区三区在线 | 在线看片日韩| 91久久久精品国产| 久久国产人妖系列| 亚洲欧美日本国产专区一区| 亚洲欧美日韩视频二区| 亚洲精品黄色| 久久精品一区二区国产| 国产精品日本一区二区不卡视频 | 精品资源在线| 免费av一区二区三区四区| 最新国产精品| 欧美aaaaaa午夜精品| 免费一二一二在线视频| 最新亚洲一区| 日韩精品视频中文字幕| 国产精品chinese| 夜鲁夜鲁夜鲁视频在线播放| 99视频在线精品国自产拍免费观看| 日韩中文字幕在线一区| 国精品产品一区| 国产一级一区二区| 国产欧美另类| 国内亚洲精品| 国产探花在线精品一区二区| 四虎成人av| 在线国产精品一区| 国产一区二区三区四区五区传媒 | 国产精品精品国产一区二区| 午夜国产欧美理论在线播放 | 日韩制服丝袜av| 高清av一区| 综合一区在线| 日韩精品一卡| 国产精品资源| 亚洲在线一区| 成人美女视频| 国产日韩欧美三级| 91超碰国产精品| 精品久久中文| 日韩欧美高清一区二区三区| 欧美亚洲日本精品| 欧美精品三级在线| 亚洲一区国产一区| 神马久久午夜| 国产精品一站二站| 欧美特黄a级高清免费大片a级| 精品国产欧美| 日韩和欧美一区二区| 欧美日韩激情在线一区二区三区| 嫩草伊人久久精品少妇av杨幂| 蜜芽一区二区三区| 激情六月综合| 精品国产99| 97成人在线| 亚洲综合日韩| 日韩影院二区| 久久精品二区亚洲w码| 蜜桃视频在线观看一区| 日韩久久视频| 国产精品99久久免费| 免费一区二区视频| jiujiure精品视频播放| 伊人久久在线| 粉嫩av一区二区三区四区五区| 日韩欧美2区| 亚洲一区二区三区四区电影| 欧美亚洲国产精品久久| 中文字幕在线视频久| 欧美国产亚洲精品| 国产欧美日韩一级| 日韩一区二区三区精品视频第3页| 亚洲少妇一区| 一区免费视频| 国产高清一区二区| 久久国产免费| 亚洲成人av观看| 日韩在线高清| 91看片一区| 久久国产直播| 亚洲天堂黄色| 国产精品97| 91成人精品视频| 欧美美女一区| 国产亚洲激情| 亚洲欧洲美洲国产香蕉| 99视频一区| 视频一区欧美日韩|