欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java中restTemplate的使用

 更新時間:2022年11月16日 08:18:28   作者:代碼的路  
這篇文章主要為大家詳細介紹了Java中restTemplate用法的相關(guān)資料,文中的示例代碼講解詳細,對我們學習Java有一定的幫助,需要的可以參考一下

本文介紹restTemplate基礎(chǔ)用法。

Java中g(shù)et和post的用法請參考:一文帶你搞懂Java中Get和Post的使用

1 提供get/post接口

1.1 Controller

@RestController
@RequestMapping("/homepage")
public class MyController {

    @Autowired
    MyService myService;

    // 提供get接口
    @GetMapping("/provideGet")
    public Map<String, String> provideGet(){
        return myService.provideGet();
    }

    // 提供post接口
    @PostMapping("/providePost")
    public Map<String, Object> providePost(@RequestParam("number") int number, @RequestParam("name") String name) {
        return myService.providePost(number, name);
    }

    // 提供map參數(shù)的post接口
    @PostMapping("/providePostByMap")
    public Map<String, Object> providePostByMap(@RequestParam Map<String, Object> map) {
        return myService.providePostByMap(map);
    }

    // 調(diào)用get接口
    @GetMapping("/useGet")
    public Map<String, Object> useGet() {
        return myService.useGet();
    }
}

1.2 Service

@Service
@EnableScheduling
public class MyService {

    public Map<String, String> provideGet() {
        Map<String, String> res = new HashMap<>();
        res.put("number", "3");
        res.put("name", "張三get");
        System.out.println("provideGet res:" + res + "\n");
        return res;
    }

    public Map<String, Object> providePost(int number, String name) {
        Map<String, Object> res = new HashMap<>();
        res.put("number", number);
        res.put("name", name);

        return res;
    }

    public Map<String, Object> providePostByMap(Map<String, Object> map) {
        int number = map.get("number") == null ? 0 : Integer.parseInt((String) map.get("number"));
        String name = map.get("name") == null ? "" : (String) map.get("name");
        Map<String, Object> res = new HashMap<>();
        res.put("number", number);
        res.put("name", name);

        System.out.println("providePostByMap res:" + res + "\n");
        return res;
    }
}

2 調(diào)用get/post接口

使用restTemplate調(diào)用get/post接口。

  • getForObject():返回值是HTTP協(xié)議的響應(yīng)體
  • getForEntity():返回的是ResponseEntity,ResponseEntity是對HTTP響應(yīng)的封裝,除了包含響應(yīng)體,還包含HTTP狀態(tài)碼、contentType、contentLength、Header等信息

2.1 Controller

@RestController
@RequestMapping("/homepage")
public class MyController {    
    @Autowired
    MyService myService;

    // 調(diào)用get接口
    @GetMapping("/useGet")
    public Map<String, Object> useGet() {
        return myService.useGet();
    }

    // 調(diào)用get接口驗證賬號密碼
    @GetMapping("/useGetByPsw")
    public Map<String, Object> useGetByPsw() {
        return myService.useGetByPsw();
    }

    // 調(diào)用post接口
    @PostMapping("/usePost")
    public Map<String, Object> usePost() {
        return myService.usePost();
    }
}

2.2 Service

@Service
@EnableScheduling
public class MyService {
    @Resource
    private RestTemplate restTemplate;

    String getURL = "http://localhost:8081/homepage/provideGet";
    String postURL = "http://localhost:8081/homepage/providePostByMap";

    public Map<String, Object> useGet() {
        // getForObject返回值是HTTP協(xié)議的響應(yīng)體
        String strObject = restTemplate.getForObject(getURL, String.class);
        JSONObject jsonObject = JSONObject.parseObject(strObject);

        // getForEntity返回的是ResponseEntity,是對HTTP響應(yīng)的封裝
        ResponseEntity<ResponseResult> responseData = restTemplate.getForEntity(getURL, ResponseResult.class);
        Map<String, Object> returnData = new HashMap<>();
        returnData.put("StatusCode:", responseData.getStatusCode());
        returnData.put("Body:", responseData.getBody());

        System.out.println("useGet jsonObject:" + jsonObject + "\n");
        System.out.println("useGet responseData:" + responseData + "\n");
        System.out.println("useGet returnData:" + returnData + "\n");
        return returnData;
    }

    public Map<String, Object> useGetByPsw() {

        RestTemplateBuilder builder = new RestTemplateBuilder();
        RestTemplate restTemplate = builder.basicAuthentication("username", "password").build();

        // getForEntity返回的是ResponseEntity,是對HTTP響應(yīng)的封裝
        ResponseEntity<ResponseResult> responseData = restTemplate.getForEntity(getURL, ResponseResult.class);
        Map<String, Object> returnData = new HashMap<>();
        returnData.put("StatusCode:", responseData.getStatusCode());
        returnData.put("Body:", responseData.getBody());

        System.out.println("useGetByPsw returnData:" + responseData + "\n");
        System.out.println("useGetByPsw returnData:" + returnData + "\n");
        return returnData;
    }

    public Map<String, Object> usePost() {
        //RestTemplate在postForObject時,用MultiValueMap,不可使用HashMap。
        MultiValueMap<String, String> sendData = new LinkedMultiValueMap<>();
        sendData.add("number", "3");
        sendData.add("name", "張三post");

        // getForObject返回值是HTTP協(xié)議的響應(yīng)體
        String strObject = restTemplate.postForObject(postURL, sendData, String.class);
        JSONObject jsonObject = JSONObject.parseObject(strObject);

        // getForEntity返回的是ResponseEntity,是對HTTP響應(yīng)的封裝
        ResponseEntity<ResponseResult> responseData = restTemplate.postForEntity(postURL, sendData, ResponseResult.class);
        Map<String, Object> returnData = new HashMap<>();
        returnData.put("StatusCode:", responseData.getStatusCode());
        returnData.put("Body:", responseData.getBody());

        System.out.println("usePost jsonObject:" + jsonObject + "\n");
        System.out.println("usePost responseData:" + responseData + "\n");
        System.out.println("usePost returnData:" + returnData + "\n");
        return returnData;
    }
}

到此這篇關(guān)于詳解Java中restTemplate的使用的文章就介紹到這了,更多相關(guān)Java restTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入Synchronized和java.util.concurrent.locks.Lock的區(qū)別詳解

    深入Synchronized和java.util.concurrent.locks.Lock的區(qū)別詳解

    本篇文章是對Synchronized和java.util.concurrent.locks.Lock的區(qū)別進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Spring中的@Configuration詳解

    Spring中的@Configuration詳解

    這篇文章主要介紹了Spring中的@Configuration詳解,Spring 提供了豐富的特性和功能,包括依賴注入、面向切面編程、事務(wù)管理、數(shù)據(jù)訪問、Web應(yīng)用程序開發(fā)等,需要的朋友可以參考下
    2023-08-08
  • Spring?boot數(shù)據(jù)庫依賴詳解

    Spring?boot數(shù)據(jù)庫依賴詳解

    這篇文章主要介紹了Spring?boot數(shù)據(jù)庫依賴,需要的朋友可以參考下
    2023-09-09
  • SpringBoot整合mybatis-plus實現(xiàn)分頁查詢功能

    SpringBoot整合mybatis-plus實現(xiàn)分頁查詢功能

    這篇文章主要介紹了SpringBoot整合mybatis-plus實現(xiàn)分頁查詢功能,pringBoot分頁查詢的兩種寫法,一種是手動實現(xiàn),另一種是使用框架實現(xiàn),現(xiàn)在我將具體的實現(xiàn)流程分享一下,需要的朋友可以參考下
    2023-11-11
  • SpringBoot集成IJPay實現(xiàn)微信v3支付的示例代碼

    SpringBoot集成IJPay實現(xiàn)微信v3支付的示例代碼

    本文主要介紹了SpringBoot集成IJPay實現(xiàn)微信v3支付的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • springboot 通過代碼自動生成pid的方法

    springboot 通過代碼自動生成pid的方法

    這篇文章主要介紹了springboot 通過代碼自動生成pid的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • JavaWeb實現(xiàn)裁剪圖片上傳完整代碼

    JavaWeb實現(xiàn)裁剪圖片上傳完整代碼

    這篇文章主要為大家詳細介紹了javaWeb實現(xiàn)裁剪圖片上傳完整代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • SpringBoot中@EnableAutoConfiguration注解的實現(xiàn)

    SpringBoot中@EnableAutoConfiguration注解的實現(xiàn)

    Spring Boot@EnableAutoConfiguration是一個強大的工具,可以簡化配置過程,從而實現(xiàn)快速開發(fā),本文主要介紹了SpringBoot中@EnableAutoConfiguration注解的實現(xiàn),感興趣的可以了解一下
    2024-01-01
  • 解決spring data redis的那些坑

    解決spring data redis的那些坑

    這篇文章主要介紹了spring data redis的那些坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot集成Kafka的步驟

    SpringBoot集成Kafka的步驟

    這篇文章主要介紹了SpringBoot集成Kafka的步驟,幫助大家更好的理解和使用SpringBoot,感興趣的朋友可以了解下
    2021-01-01

最新評論