firstGet(): Promise{
return axios.get('http://192.168.211.1:8090/test/1');
}
getAaddB(a: number, b: number): Promise{
return axios.get('http://192.168.211.1:8090/test/2', {
params: {
a: a,
b: b
}
})
}
这两个函数是使用axios库发起HTTP GET请求的函数,用于与服务器进行通信
package com.example.demospring.controller;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/test")
@RestController
public class test1 {
@GetMapping("/1")
public String test11(){
return "这是axios发送get请求从后端获取的数据";
}
@GetMapping("/2")
public int AB(@RequestParam int a, @RequestParam int b){
return a + b;
}
}
test1()方法:
前端axios封装一个简单的网络请求 在src/main/ets/network/AxiosRequest.ets里
import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from ‘@ohos/axios’ // 公共请求前缀 axios.defaults.baseURL = “http://192.168.211.1:8090” // 添加请求拦截器… // 添加响应拦截器… // 导出 export default axios; export {AxiosResponse}
axios请求
export function get1(): Promise {
return axios.get('/hello/get1');
}
后端controller
@GetMapping("/get1")
public String get1(){
return "这是你拿到的数据";
}
axios请求
export function get2(a: number, b: number): Promise {
return axios.get('/hello/get2', {
//params字段包含了将要发送到后端的参数。
params: {
a: a,
b: b
}
});
}
后端controller
@GetMapping("/get2")
//使用@RequestParam注解从URL中获取参数a和b。
public String get2(@RequestParam int a, @RequestParam int b){
return "你传的两个数是" + a + " " + b;
}
@RequestParam 注解允许你自定义请求参数的名称,并提供其他选项,如设置默认值或将参数标记为必需
axios请求
export function get3(ps: number, pn: number): Promise {
//注意要用``(反引号)
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 + "条数据的第" + pn + "页";
}
@PathVariable(“id”) 表示要从路径中提取一个名为 “id” 的变量,并将其值绑定到 itemId 参数上。
axios请求
//定义请求接收的数据类型
export interface ResponseData {
status: string;
message: string;
}
export function get4(): Promise> {
return axios.get('/hello/get4');
}
Promise
后端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;
}
axios请求
export function post1(person: { name: string, age: number }): Promise {
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() + "。";
}
axios请求
//JSON中,键和字符串值都应该被双引号包围如
export function post2(data: any): Promise {
return axios.post(`/hello/post2`, data);
}
后端controller
@PostMapping("/post2")
public String post2(@RequestBody Map mp) {
AtomicReference data = new AtomicReference<>("");
mp.forEach((k, v) ->{
data.set(data + k);
data.set(data + ": ");
data.set(data + v + ",");
});
return "你传的键值对是: " + data;
}
axios请求
export function putExample(data: string): Promise {
return axios.put('/hello/putExample', {data: data});
}
后端controller
@PutMapping("/putExample")
public String putExample(@RequestBody String data) {
return "这是PUT请求,传入的数据是: " + data;
}
axios请求
export function deleteExample(id: number): Promise {
return axios.delete(`/hello/deleteExample/${id}`);
}
后端controller
@DeleteMapping("/deleteExample/{id}")
public String deleteExample(@PathVariable("id") int id) {
return "这是DELETE请求,要删除的数据ID是: " + id;
}
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请求获取数据")
.onClick(async () =>{
this.get1 = (await get1()).data;
})
Text(this.get1)
.fontSize(20)
Button("get2-传递多个参数")
.onClick(async () =>{
this.get2 = (await get2(1, 3)).data;
})
Text(`${this.get2!=undefined?this.get2:""}`)
.fontSize(20)
Button("get3-路径参数")
.onClick(async () =>{
this.get3 = (await get3(3, 4)).data;
})
Text(`${this.get3!=undefined?this.get3:""}`)
.fontSize(20)
Button("get4-返回JSON数据")
.onClick(async () =>{
this.get4 = (await get4()).data;
})
Text(this.get4!=null ? JSON.stringify(this.get4) : "")
.fontSize(20)
Button("post1-使用对象作为请求参数")
.onClick(async () =>{
this.post1 = (await post1({name: "张三", age: 18})).data;
})
Text(this.post1)
.fontSize(20)
Button("post2-使用Map接收JSON数据的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)
}
}
以上就是鸿蒙开发的OpenHarmony;使用网络组件axios与Spring Boot进行前后端交互的技术解析,更多有关鸿蒙开发的学习,可以去主页查找,找我保存技术文档。下面分享一张OpenHarmony学习路线图:
高清完整版曲线图,可以找我保存(附鸿蒙4.0&next版文档)如下: