最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot?HTTP如何調(diào)用其他服務(wù)

 更新時(shí)間:2022年01月28日 10:09:44   作者:靜忻寒  
這篇文章主要介紹了Springboot?HTTP如何調(diào)用其他服務(wù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

HTTP如何調(diào)用其他服務(wù)

1.GET請(qǐng)求

1.1Client代碼

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
?
@Service
public class UserInfoClient {
? ? public String getUserTotalAmount(){
? ? ? ? Map<String,String> map=new HashMap<String,String>();
? ? ? ? map.put("name","123");
? ? ? ? map.put("password","123");
? ? ? ? URI uri = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/spring/test")
? ? ? ? ? ? ? ? .queryParam("jsonString",JSON.toJSONString(map))
? ? ? ? ? ? ? ? .queryParam("token","12122222111")
? ? ? ? ? ? ? ? .build().encode().toUri();
? ? ? ? RestTemplate restTemplate=new RestTemplate();
? ? ? ? String data=restTemplate.getForObject(uri,String.class);
? ? ? ? System.out.println(data);
? ? ? ? return null;
? ? }
? ? public static void main(String[] args){
? ? ? ? UserInfoClient c=new UserInfoClient();
? ? ? ? c.getUserTotalAmount();
? ? }
}

1.2 Service 代碼

import org.springframework.web.bind.annotation.*;?
@RestController
@RequestMapping(value = "/spring")
public class Test {
? ? @RequestMapping(value = "/test",method = RequestMethod.GET)
? ? public String testSpringBoot(@RequestParam String jsonString,@RequestParam String token){
? ? ? ? System.out.println(jsonString);
? ? ? ? System.out.println(token);
? ? ? ? /*
? ? ? ? ?*邏輯處理
? ? ? ? ?*/
? ? ? ? return "Spring Boot 測(cè)試成功!";
? ? }
}

2.POST請(qǐng)求

2.1Client代碼

import com.alibaba.fastjson.JSON;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
?
@Service
public class UserInfoClient {?
? ? public String getUserTotalAmount(){
? ? ? ? Map<String,String> map=new HashMap<String,String>();
? ? ? ? map.put("name","123");
? ? ? ? map.put("password","123");
? ? ? ? String url="http://localhost:8081/spring/test";
? ? ? ? //設(shè)置請(qǐng)求頭信息
? ? ? ? HttpHeaders headers = new HttpHeaders();
? ? ? ? MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
? ? ? ? headers.setContentType(type);
? ? ? ? headers.add("Accept", MediaType.APPLICATION_JSON.toString());
? ? ? ? //設(shè)置body部分
? ? ? ? HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(map),headers);
? ? ? ? RestTemplate restTemplate=new RestTemplate();
? ? ? ? ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
? ? ? ? System.out.println(result.getBody());
? ? ? ? return null;
? ? }
? ? public static void main(String[] args){
? ? ? ? UserInfoClient c=new UserInfoClient();
? ? ? ? c.getUserTotalAmount();
? ? }
}

2.2 Service代碼

 
import org.springframework.web.bind.annotation.*;
 
@RestController
@RequestMapping(value = "/spring")
public class Test {
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public String testSpringBoot(@RequestBody UserBean userBean){
        System.out.println(userBean);
        /*
         *邏輯處理
         */
        return "Spring Boot 測(cè)試成功!";
    }
}

springboot請(qǐng)求其他服務(wù)器的http接口

使用Get方式,攜帶headers請(qǐng)求數(shù)據(jù)

//注入
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/FaceInfo")
@ResponseBody
public Object ? faceInfo(String startTime,String endTime,Integer size ){
? ? String apiURL = "http://www.05un.cn/wspp/GceGroups";
? ? HttpHeaders headers = new HttpHeaders();
? ?headers.add("userId","38");
? ? // headers.set("userId","38");
? ? headers.setContentType(MediaType.APPLICATION_JSON);
? ? Map<String, Object> requestParam = new HashMap<>();
? ? HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers);
? ? ? ? ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.GET, request, String.class);
? ? String body = entity2.getBody();
? ? return body;
}

使用Post方式,攜帶body請(qǐng)求數(shù)據(jù)

//注入
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/FaceInfo")
@ResponseBody
public Object ? faceInfo(String startTime,String endTime,Integer size ){
? ? String apiURL = "http://www.0531yun.cn/wsjc/app/Login";
? ? HttpHeaders headers = new HttpHeaders();
? ? headers.setContentType(MediaType.APPLICATION_JSON);
? ? Map<String, Object> requestParam = new HashMap<>();
? ? requestParam.put("loginName", "jnr");
? ? requestParam.put("password", "jn");
? ? HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers);
? ? String s=request.toString();
? ? ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.POST, request, String.class);
? ? String body = entity2.getBody();
? ? return body;
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決Springboot get請(qǐng)求是參數(shù)過(guò)長(zhǎng)的情況

    解決Springboot get請(qǐng)求是參數(shù)過(guò)長(zhǎng)的情況

    這篇文章主要介紹了解決Springboot get請(qǐng)求是參數(shù)過(guò)長(zhǎng)的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • Java框架Struts2實(shí)現(xiàn)圖片上傳功能

    Java框架Struts2實(shí)現(xiàn)圖片上傳功能

    這篇文章主要為大家詳細(xì)介紹了Java框架Struts2實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • java?MongoDB實(shí)現(xiàn)列表分頁(yè)查詢的示例代碼

    java?MongoDB實(shí)現(xiàn)列表分頁(yè)查詢的示例代碼

    本文主要介紹了java?MongoDB實(shí)現(xiàn)列表分頁(yè)查詢的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Spring Boot JPA中使用@Entity和@Table的實(shí)現(xiàn)

    Spring Boot JPA中使用@Entity和@Table的實(shí)現(xiàn)

    這篇文章主要介紹了Spring Boot JPA中使用@Entity和@Table的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Java多線程之多線程異常捕捉

    Java多線程之多線程異常捕捉

    在java多線程程序中,所有線程都不允許拋出未捕獲的checked exception,也就是說(shuō)各個(gè)線程需要自己把自己的checked exception處理掉,通過(guò)此篇文章給大家分享Java多線程之多線程異常捕捉,需要的朋友可以參考下
    2015-08-08
  • 使用Java的Spring框架編寫(xiě)第一個(gè)程序Hellow world

    使用Java的Spring框架編寫(xiě)第一個(gè)程序Hellow world

    這篇文章主要介紹了Java的Spring框架并用其開(kāi)始編寫(xiě)第一個(gè)程序Hellow world的方法,Spring是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • 微信跳一跳輔助Java代碼實(shí)現(xiàn)

    微信跳一跳輔助Java代碼實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了微信跳一跳輔助的Java代碼實(shí)現(xiàn)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • SpringBoot如何優(yōu)雅的處理重復(fù)請(qǐng)求

    SpringBoot如何優(yōu)雅的處理重復(fù)請(qǐng)求

    對(duì)于一些用戶請(qǐng)求,在某些情況下是可能重復(fù)發(fā)送的,如果是查詢類操作并無(wú)大礙,但其中有些是涉及寫(xiě)入操作的,一旦重復(fù)了,可能會(huì)導(dǎo)致很嚴(yán)重的后果,所以本文給大家介紹了SpringBoot優(yōu)雅的處理重復(fù)請(qǐng)求的方法,需要的朋友可以參考下
    2023-12-12
  • 5種Java經(jīng)典創(chuàng)建型模式詳解

    5種Java經(jīng)典創(chuàng)建型模式詳解

    這篇文章主要為大家詳細(xì)介紹了5種Java經(jīng)典創(chuàng)建型模式,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Spring Boot與ActiveMQ整合的步驟

    Spring Boot與ActiveMQ整合的步驟

    今天小編就為大家分享一篇關(guān)于Spring Boot與ActiveMQ整合的步驟,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01

最新評(píng)論

多伦县| 集贤县| 隆林| 永靖县| 柳林县| 白朗县| 象山县| 黄冈市| 齐河县| 盐城市| 拉孜县| 东乡县| 安国市| 镶黄旗| 崇仁县| 伊通| 耒阳市| 若羌县| 武陟县| 昔阳县| 开阳县| 盱眙县| 石阡县| 千阳县| 玉屏| 汾阳市| 平湖市| 邓州市| 垦利县| 虹口区| 库尔勒市| 杭州市| 讷河市| 德令哈市| 久治县| 腾冲县| 南开区| 蕉岭县| 横峰县| 百色市| 余姚市|