⼩程序后端Java接⼝开发的详细步骤⽬录
1、搭建⼀个springboot项⽬并引⼊依赖
2、编写controller层
3、创建⼩程序项⽬
⼩程序使⽤wx.request(OBJECT)来调⽤后端接⼝。
⾸先我们来⼀个简单案例 —— helloworld实现
1、搭建⼀个springboot项⽬并引⼊依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、编写controller层
@RestController
public class HelloWorldController {
@GetMapping("/helloWorld")
public String helloWorld(Integer id){
return "helloworld"+id;
}
}
server:
port: 80
servlet:
context-path: /
tomcat:
uri-encoding: utf-8
运⾏成功
3、创建⼩程序项⽬
helloworld.js
/**
* 页⾯的初始数据
*/
data: {
result:'请求后台中.....'
},
/**
* ⽣命周期函数--监听页⾯加载
*/
onLoad: function (options) {
var that=this;
},
getData(that){
url: 'localhost:8080/helloWorld',
method:'GET',
data:{
id:666
},
header:{
'content-type':'application/json'  //默认值          },
success(res){
console.log(res.data);
console.log(that);
that.setData({
result:res.data写文章的小程序
})
}
})
},
helloworld.wxml
<text>后端返回的数据:{{result}}</text>
注意:
这⾥记得设置如下图
否则会报错:
访问后端成功如下图
到此这篇关于⼩程序后端Java接⼝开发的详细步骤的⽂章就介绍到这了,更多相关⼩程序后端Java接⼝开发内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。