溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2022-01-30 09:37:09 來源:億速云 閱讀:378 作者:iii 欄目:開發(fā)技術(shù)

這篇“Springboot HTTP怎么調(diào)用其他服務(wù)”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Springboot HTTP怎么調(diào)用其他服務(wù)”文章吧。

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

1.GET請求

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 測試成功!";
    }
}

2.POST請求

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è)置請求頭信息
        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 測試成功!";
    }
}

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

使用Get方式,攜帶headers請求數(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請求數(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;
}

以上就是關(guān)于“Springboot HTTP怎么調(diào)用其他服務(wù)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI