国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

OpenHarmony:使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互

這篇具有很好參考價值的文章主要介紹了OpenHarmony:使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

流程圖:
OpenHarmony:使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互,鴻蒙,網(wǎng)絡(luò),spring boot,交互,harmonyos,鴻蒙開發(fā),鴻蒙next,移動開發(fā)

一、簡單的交互

前端請求函數(shù)

firstGet(): Promise<AxiosResponse>{
    return axios.get('http://192.168.211.1:8090/test/1');
}
getAaddB(a: number, b: number): Promise<AxiosResponse>{
   return axios.get('http://192.168.211.1:8090/test/2', {
      params: {
        a: a,
        b: b
      }
   })
}

這兩個函數(shù)是使用axios庫發(fā)起HTTP GET請求的函數(shù),用于與服務(wù)器進行通信

  • 服務(wù)器端點: http://192.168.211.1:8090/test/1 這是我本機的ip地址和springboot運行端口,使用在windows終端輸入ipconfig可查看
  • 返回值: 該函數(shù)返回一個Promise,該Promise在請求成功時將包含AxiosResponse對象,其中包含了從服務(wù)器接收到的響應(yīng)信息。

后端controller

package com.example.demospring.controller;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/test")
@RestController
public class test1 {
    @GetMapping("/1")
    public String test11(){
        return "這是axios發(fā)送get請求從后端獲取的數(shù)據(jù)";
    }   
    @GetMapping("/2")
    public int AB(@RequestParam int a, @RequestParam int b){
        return a + b;
    }
}

test1()方法:

  • 功能: 當(dāng)接收到GET請求 /test/1 時,該方法返回一個字符串 “這是axios發(fā)送get請求從后端獲取的數(shù)據(jù)”。
  • 備注: 這是一個簡單的用于演示GET請求的方法,返回字符串?dāng)?shù)據(jù)。

二、axios與Spring Boot進行前后端交互的實現(xiàn)

一、前后端交互配置

  • Arkts目錄結(jié)構(gòu)

OpenHarmony:使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互,鴻蒙,網(wǎng)絡(luò),spring boot,交互,harmonyos,鴻蒙開發(fā),鴻蒙next,移動開發(fā)

前端axios封裝一個簡單的網(wǎng)絡(luò)請求 在src/main/ets/network/AxiosRequest.ets里

import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from ‘@ohos/axios’ // 公共請求前綴 axios.defaults.baseURL = “http://192.168.211.1:8090” // 添加請求攔截器… // 添加響應(yīng)攔截器… // 導(dǎo)出 export default axios; export {AxiosResponse}

  • 后端用于配置跨域資源共享(CORS)的設(shè)置 登錄后復(fù)制 @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(“/**”) // 映射的路徑,這里是所有路徑 .allowedOrigins(““) // 允許的來源,這里是所有來源,可以設(shè)置具體的域名或 IP 地址 .allowedMethods(“PUT”, “GET”, “POST”, “DELETE”) // 允許的 HTTP 方法 .allowedHeaders(””) // 允許的 HTTP 頭部 .allowCredentials(false) // 是否支持用戶憑據(jù),這里是不支持 .maxAge(300); // 預(yù)檢請求的有效期,單位秒 } @RequestMapping(“/hello”):這個注解用于類級別,表示所有在這個控制器中的方法的URL映射的基本路徑 登錄后復(fù)制 @RestController @RequestMapping(“/hello”) public class SumUpController { … }

二、不同請求的參數(shù)傳遞與后端接收返回代碼

1.get請求獲取數(shù)據(jù)

axios請求

export function get1(): Promise<AxiosResponse> {
  return axios.get('/hello/get1');
}

后端controller

@GetMapping("/get1")
public String get1(){
    return "這是你拿到的數(shù)據(jù)";
}

2.get請求傳遞多個參數(shù)

axios請求

export function get2(a: number, b: number): Promise<AxiosResponse> {
  return axios.get('/hello/get2', {
    //params字段包含了將要發(fā)送到后端的參數(shù)。
    params: {
      a: a,
      b: b
    }
  });
}

后端controller

@GetMapping("/get2")
 //使用@RequestParam注解從URL中獲取參數(shù)a和b。
 public String get2(@RequestParam int a, @RequestParam int b){
    return "你傳的兩個數(shù)是" + a + " " + b;
 }

@RequestParam 注解允許你自定義請求參數(shù)的名稱,并提供其他選項,如設(shè)置默認值或?qū)?shù)標記為必需

3.get請求路徑參數(shù)

axios請求

export function get3(ps: number, pn: number): Promise<AxiosResponse> {
  //注意要用``(反引號)
  return axios.get(`/hello/get3/${pn}/${ps}`);
}

后端controller

@GetMapping("/get3/{pn}/{ps}")
public String get3(@PathVariable("ps") int ps, @PathVariable("pn") int pn){
    return "你的查找要求是一頁" + ps + "條數(shù)據(jù)的第" +  pn + "頁";
}

@PathVariable(“id”) 表示要從路徑中提取一個名為 “id” 的變量,并將其值綁定到 itemId 參數(shù)上。

4.get請求返回JSON數(shù)據(jù)

axios請求

//定義請求接收的數(shù)據(jù)類型
export interface ResponseData {
  status: string;
  message: string;
}
export function get4(): Promise<AxiosResponse<ResponseData>> {
  return axios.get('/hello/get4');
}

Promise<AxiosResponse> 表示一個 Promise 對象,該對象最終解決為 Axios 發(fā)起的 HTTP 請求的響應(yīng),而該響應(yīng)的數(shù)據(jù)體應(yīng)該符合 ResponseData 類型的結(jié)構(gòu)。

后端controller

//@Data注解一個類時,Lombok會自動為該類生成常見的方法,如toString()、equals(),以及所有字段的getter和setter方法。
@Data
public static class ResponseData {
    private String status;
    private String message;
}
@GetMapping("/get4")
public ResponseData get4() {
    ResponseData responseData = new ResponseData();
    responseData.setStatus("success");
    responseData.setMessage("這是一條成功的消息。");
    return responseData;
}

5.post 使用對象作為請求參數(shù)

axios請求

export function post1(person: { name: string, age: number }): Promise<AxiosResponse> {
  return axios.post(`/hello/post1`, person);
}

后端controller

@Data
public static class Person {
    private String name;
    private int age;
}
@PostMapping("/post1")
public String post1(@RequestBody Person person) {
    return "你傳的姓名: " + person.getName() + " 年齡: " + person.getAge() + "。";
}

6.post 使用Map接收JSON數(shù)據(jù)

axios請求

//JSON中,鍵和字符串值都應(yīng)該被雙引號包圍如
export function post2(data: any): Promise<AxiosResponse> {
  return axios.post(`/hello/post2`, data);
}

后端controller

@PostMapping("/post2")
public String post2(@RequestBody Map<String, String> mp) {
    AtomicReference<String> data = new AtomicReference<>("");
    mp.forEach((k, v) ->{
        data.set(data + k);
        data.set(data + ": ");
        data.set(data + v + ",");
    });
    return "你傳的鍵值對是: " + data;
}

7.put請求

axios請求

export function putExample(data: string): Promise<AxiosResponse> {
  return axios.put('/hello/putExample', {data: data});
}

后端controller

@PutMapping("/putExample")
public String putExample(@RequestBody String data) {
    return "這是PUT請求,傳入的數(shù)據(jù)是: " + data;
}

8.delete請求

axios請求

export function deleteExample(id: number): Promise<AxiosResponse> {
  return axios.delete(`/hello/deleteExample/${id}`);
}

后端controller

@DeleteMapping("/deleteExample/{id}")
public String deleteExample(@PathVariable("id") int id) {
    return "這是DELETE請求,要刪除的數(shù)據(jù)ID是: " + id;
}

三、調(diào)用傳參

import router from '@ohos.router'
import {get1, get2, get3, get4, post1, post2, putExample, deleteExample} from '../network/api/TestApi'
@Entry
@Component
struct Index {
  @State get1: string = "";
  @State get2: number = undefined;
  @State get3: number = undefined;
  @State get4: {status: string, message: string} = null;
  @State post1: string = "";
  @State post2: string = "";
  @State put: string = "";
  @State delete: string = "";
  build() {
    Column() {
      Button("get1-get請求獲取數(shù)據(jù)")
        .onClick(async () =>{
          this.get1 = (await get1()).data;
        })
      Text(this.get1)
        .fontSize(20)
      Button("get2-傳遞多個參數(shù)")
        .onClick(async () =>{
          this.get2 = (await get2(1, 3)).data;
        })
      Text(`${this.get2!=undefined?this.get2:""}`)
        .fontSize(20)
      Button("get3-路徑參數(shù)")
        .onClick(async () =>{
          this.get3 = (await get3(3, 4)).data;
        })
      Text(`${this.get3!=undefined?this.get3:""}`)
        .fontSize(20)
      Button("get4-返回JSON數(shù)據(jù)")
        .onClick(async () =>{
          this.get4 = (await get4()).data;
        })
      Text(this.get4!=null ? JSON.stringify(this.get4) : "")
        .fontSize(20)

      Button("post1-使用對象作為請求參數(shù)")
        .onClick(async () =>{
          this.post1 = (await post1({name: "張三", age: 18})).data;
        })
      Text(this.post1)
        .fontSize(20)

      Button("post2-使用Map接收JSON數(shù)據(jù)的POST請求示例")
        .onClick(async () =>{
          this.post2 = (await post2({id: "1", name: "李四", status: "call"})).data;
        })
      Text(this.post2)
        .fontSize(20)

      Button("put請求")
        .onClick(async () =>{
          this.put = (await putExample("put data")).data;
        })
      Text(this.put)
        .fontSize(20)

      Button("delete請求")
        .onClick(async () =>{
          this.delete = (await deleteExample(10)).data;
        })
      Text(this.delete)
        .fontSize(20)
      Button("對一個表單的增刪改查")
        .margin(20)
        .onClick(() =>{
          router.pushUrl({
            url: "pages/TalentTableTest"
          })
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

以上就是鴻蒙開發(fā)的OpenHarmony;使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互的技術(shù)解析,更多有關(guān)鴻蒙開發(fā)的學(xué)習(xí),可以去主頁查找,找我保存技術(shù)文檔。下面分享一張OpenHarmony學(xué)習(xí)路線圖

OpenHarmony:使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互,鴻蒙,網(wǎng)絡(luò),spring boot,交互,harmonyos,鴻蒙開發(fā),鴻蒙next,移動開發(fā)

高清完整版曲線圖,可以找我保存(附鴻蒙4.0&next版文檔)如下:

OpenHarmony:使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互,鴻蒙,網(wǎng)絡(luò),spring boot,交互,harmonyos,鴻蒙開發(fā),鴻蒙next,移動開發(fā)

OpenHarmony:使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互,鴻蒙,網(wǎng)絡(luò),spring boot,交互,harmonyos,鴻蒙開發(fā),鴻蒙next,移動開發(fā)文章來源地址http://www.zghlxwxcb.cn/news/detail-814256.html

四、總結(jié)

一、請求參數(shù)錯誤的常見情況:

  1. 參數(shù)名稱不一致
  2. 參數(shù)類型(格式)不一致
  3. 缺少必須的請求參數(shù)
  4. 請求頭信息不符合要求

二、不同請求方式與參數(shù)傳遞方式的對應(yīng)關(guān)系:

  1. RESTful風(fēng)格的API通常使用路徑變量傳遞參數(shù)。在Spring框架中,可以使用@PathVariable注解來接收這些參數(shù)。
  2. URL中使用params傳遞參數(shù)時,通常使用@RequestParam注解來接收參數(shù)。
  3. 當(dāng)客戶端通過請求體傳遞JSON數(shù)據(jù)時,可以使用@RequestBody注解來接收。
  4. @ModelAttribute用于綁定方法參數(shù)或方法返回值到模型中,通常用于將請求參數(shù)與模型屬性進行綁定。

到了這里,關(guān)于OpenHarmony:使用網(wǎng)絡(luò)組件axios與Spring Boot進行前后端交互的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Spring Boot(Vue3+ElementPlus+Axios+MyBatisPlus+Spring Boot 前后端分離)【二】

    Spring Boot(Vue3+ElementPlus+Axios+MyBatisPlus+Spring Boot 前后端分離)【二】

    ??前言 本篇博文是關(guān)于Spring Boot(Vue3+ElementPlus+Axios+MyBatisPlus+Spring Boot 前后端分離)【二】的,希望你能夠喜歡 ??個人主頁:晨犀主頁 ??個人簡介:大家好,我是晨犀,希望我的文章可以幫助到大家,您的滿意是我的動力???? ??歡迎大家:這里是CSDN,我總結(jié)知識的地方,

    2024年02月11日
    瀏覽(112)
  • Spring Boot(Vue3+ElementPlus+Axios+MyBatisPlus+Spring Boot 前后端分離)【三】

    Spring Boot(Vue3+ElementPlus+Axios+MyBatisPlus+Spring Boot 前后端分離)【三】

    ??前言 本篇博文是關(guān)于Spring Boot(Vue3+ElementPlus+Axios+MyBatisPlus+Spring Boot 前后端分離)【三】的分享,希望你能夠喜歡 ??個人主頁:晨犀主頁 ??個人簡介:大家好,我是晨犀,希望我的文章可以幫助到大家,您的滿意是我的動力???? ??歡迎大家:這里是CSDN,我總結(jié)知識的地

    2024年02月11日
    瀏覽(99)
  • Spring Boot項目使用 jasypt 加密組件進行加密(例如:數(shù)據(jù)庫、服務(wù)的Key、等等進行加密)

    Spring Boot項目使用 jasypt 加密組件進行加密(例如:數(shù)據(jù)庫、服務(wù)的Key、等等進行加密)

    ?? 簡介:java系列技術(shù)分享(??持續(xù)更新中…??) ?? 初衷:一起學(xué)習(xí)、一起進步、堅持不懈 ?? 如果文章內(nèi)容有誤與您的想法不一致,歡迎大家在評論區(qū)指正?? ?? 希望這篇文章對你有所幫助,歡迎點贊 ?? 收藏 ?留言 ?? ?? 更多文章請點擊 密碼配置項都不加密? 想啥呢? 一

    2024年02月07日
    瀏覽(34)
  • Java-web:使用Axios代替JSP進行前后端分離與數(shù)據(jù)交互

    Java-web:使用Axios代替JSP進行前后端分離與數(shù)據(jù)交互

    使用Servlet注解代替配置web.xml文件 在servlet3.0版本支持使用注解 1.創(chuàng)建一個Servlet響應(yīng)axios發(fā)送的請求 JSP代替: 還原JSP到HTML: Axios:將ajax進行封裝,簡化JS發(fā)送異步請求的代碼 Axios官網(wǎng):https://www.axios-http.cn/ 下載axios.js文件到本地然后再HTML頭文件引入或者直接引用網(wǎng)址的JS頭文

    2024年02月06日
    瀏覽(23)
  • 【Spring Boot】使用Spring Boot進行transformer的部署與開發(fā)

    Transformer是一個用于數(shù)據(jù)轉(zhuǎn)換和處理的平臺,使用Spring Boot可以方便地進行Transformer的部署與開發(fā)。 以下是使用Spring Boot進行Transformer部署與開發(fā)的步驟: 創(chuàng)建Spring Boot項目 可以使用Spring Initializr創(chuàng)建一個簡單的Spring Boot項目。在創(chuàng)建項目時,需要添加以下依賴: 編寫Transforme

    2024年02月11日
    瀏覽(32)
  • 【深入淺出 Spring Security(十三)】使用 JWT 進行前后端分離認證(附源碼)

    【深入淺出 Spring Security(十三)】使用 JWT 進行前后端分離認證(附源碼)

    JWT 全稱 Java web Token,在此所講述的是 JWT 用于身份認證,用服務(wù)器端生成的JWT去替代原始的Session認證,以提高安全性。 JWT本質(zhì)是一個Token令牌,是由三部分組成的字符串,分別是頭部(header)、載荷(payload)和簽名(signature)。頭部一般包含該 JWT 的基本信息,例如所使用的

    2024年02月12日
    瀏覽(25)
  • Spring Boot進階(73):Spring Boot如何優(yōu)雅地使用Feign進行服務(wù)間通信?

    Spring Boot進階(73):Spring Boot如何優(yōu)雅地使用Feign進行服務(wù)間通信?

    ????????在分布式系統(tǒng)中,服務(wù)間通信是非常常見的情況。而Feign就是一個開源的Java HTTP客戶端,可以幫助我們在Spring Boot應(yīng)用中快速構(gòu)建和使用HTTP客戶端,方便實現(xiàn)服務(wù)間的通信。本文將介紹如何優(yōu)雅地使用Feign進行服務(wù)間通信。 ? ? ? ? 那么,具體如何實現(xiàn)呢?這將又

    2024年02月06日
    瀏覽(23)
  • Spring Boot 如何使用 Spring Security 進行認證和授權(quán)

    Spring Boot 如何使用 Spring Security 進行認證和授權(quán)

    在 Web 應(yīng)用程序中,認證和授權(quán)是非常重要的功能。Spring Security 是一個基于 Spring 框架的強大的安全框架,它提供了完整的認證和授權(quán)解決方案,并且可以輕松地集成到 Spring Boot 應(yīng)用程序中。本文將介紹如何在 Spring Boot 中使用 Spring Security 進行認證和授權(quán),并提供示例代碼。

    2024年02月11日
    瀏覽(37)
  • Spring Boot 如何使用 JUL 進行日志記錄

    Spring Boot 如何使用 JUL 進行日志記錄

    在 Spring Boot 中,我們可以使用多種日志框架進行日志記錄。其中,JUL (Java Util Logging) 是 Java 平臺自帶的日志框架,它提供了簡單的 API 和配置,可以輕松地進行日志記錄。本文將介紹如何在 Spring Boot 中使用 JUL 進行日志記錄,并提供示例代碼。 默認情況下,Spring Boot 使用 L

    2024年02月10日
    瀏覽(39)
  • spring boot整合cache使用Ehcache 進行數(shù)據(jù)緩存

    spring boot整合cache使用Ehcache 進行數(shù)據(jù)緩存

    之前的文章 spring boot整合 cache 以redis服務(wù) 處理數(shù)據(jù)緩存 便捷開發(fā) 帶著大家通過spring boot整合了 cache 緩存 那么 我們就來說說 其他服務(wù)的緩存 而spring boot默認的緩存方案就是 cache 用simple模式 spring boot的強大在于它的整合能力 它將其他緩存技術(shù)整合 統(tǒng)一了接口 簡單說 所有的

    2024年02月19日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包