小程序怎么与后端Java接口交互实现HelloWorld

第一步:后端简单建个SpringBoot项目,提供一个 helloWorld接口;

版本选用 2.2.6.RELEASE

package com.java1234.controller;


import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;


/**
* @author java1234_小锋
* @site www.java1234.com
* @company 南通小锋网络科技有限公司
* @create 2021-07-04 17:43
*/
@RestController
public class HelloWorldController {

@GetMapping("
/helloWorld"
)
public String helloWorld(Integer id){
return "
helloWorld "
+id;

}
}

application.yml

server:
port: 80
servlet:
context-path: /
tomcat:
uri-encoding: utf-8

浏览器访问:http://localhost/helloWorld?id=1

页面显示:

helloWorld 1

第二步:新建一个helloWorld 微信小程序,请求后端

helloWorld.js

通过微信小程序API wx.request调用后端接口

// pages/helloWorld.js

小程序与后端Java接口交互实现HelloWorld教程


Page({

/**
* 页面的初始数据
*/
data: {
result:"
请求后台中..."

},

/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that=this;

this.getData(that);

},

getData(that){
wx.request({
url: "
http://localhost/helloWorld"
,
method:"
GET"
,
data:{
id:100
},
header: {
"
content-type"
: "
application/json"
// 默认值
},
success(res){

console.log(res.data);

console.log(that)
that.setData({
result:res.data
})
}
})
},


/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {

},

/**
* 生命周期函数--监听页面显示
*/
onShow: function () {

},

/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {

},

/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {

},

/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {

}
})

helloWorld.wxml

<
!--pages/helloWorld.wxml-->

<
text>
返回值:{{result}}<
/text>

运行报错了:

VM8 asdebug.js:1 Cannot send network request to localhost.(env: Windows,mp,1.05.2105170;
lib: 2.18.0)

这里我们需要设置下:

详情->
本地设置->
勾选 “不校验合法域名、web-view (业务域名)、TLS版本以及HITPS证书”

勾选后,重新编译,运行OK;

扩展下,如果是域名调用,比如 http://localhost 改成 http://www.java1234.com

报错:

如若已在管理后台更新域名配置,请刷新项目配置后重新编译项目,操作路径:“详情-域名信息”VM8 asdebug.js:1 http://www.java1234.com 不在以下 request 合法域名列表中

微信小程序对于域名调用会有一些限制,还需要配置,比如仅支持https,•域名不能使用 IP 地址(小程序的局域网 IP 除外)或 localhost;

服务器域名请在 「小程序后台-开发-开发设置-服务器域名」 中进行配置:



小程序作为移动端新媒体工具,其灵活的交互方式和便捷的API,让开发者更容易与用户联系,实现功能。本文将介绍如何使用小程序与后端Java接口交互实现HelloWorld。
一、前置条件:开发tools和环境配置
在进行本文接下来的教程之前,需要您已经完成小程序的开发工具安装,同时配置好Java后端环境。
二、小程序与Java后端交互的方式
小程序与Java后端交互的方式有多种,如RESTful、SOAP、RPC等,本文将以RESTful接口实现为例介绍。
三、搭建Java后端服务
首先,我们需要在Java后端搭建一个服务,搭建过程需要用到SpringMvc框架和Maven工具,具体步骤可以参考官方文档。
四、编写Java后端接口
在Java后端服务搭建好后,我们需要编写一个简单的接口,用于接收小程序发来的请求并返回HelloWorld。接口代码如下:
@RestController
public class HelloWorldController {
@RequestMapping(value=\"/hello\", method= RequestMethod.GET)
public String hello() {
return \"Hello World!\";
}
}
五、小程序调用后端接口
接下来,我们需要在小程序中调用Java后端接口,这需要使用小程序的wx.request() API。在小程序中,我们可以使用如下代码调用后端接口,并打印后端返回值。
wx.request({
method: 'GET',
url: 'http://localhost:8080/hello',
success: function (res) {
console.log(res.data)
}
})
六、测试结果
接下来,我们可以运行小程序并查看结果了,如果一切正常,小程序将会输出“Hello World!”。
七、总结
通过本篇文章的介绍,我们学习了如何使用小程序与后端Java接口交互实现HelloWorld。RESTful接口实现是一种简单而有效的方式,其它方式也具有相似的实现方式。我们相信在对API、框架有一定了解的情况下,您也可以轻松地实现更复杂的功能。