微信小程序与后台整合(Java)

为了方便演示 这里只做一个简单的提交数据.在Java控制台输出数据

一 新建微信小程序

wxml页面

<view bindtap="clickme">点击</view>

js页面

微信小程序不能直接与后台整合,这里使用ngrok 内网转发实现 

这里是ngrok的使用

https://blog.csdn.net/qq_38474916/article/details/87805684

这里的name age 要和后台保持一致 并且类型一致

/user为请求方法


Page({
 clickme:function(){
   wx.request({
     url: 'https://af23785a.ngrok.io/user', // 
     data: {
      name: "路虎",
      age: 1
     },
     header: {
       'content-type': 'application/json' // 默认值
     },
     success(res) {
       console.log(res.data)
     }
   })
 }
})

打开debug模式 app.json中

 "debug":true

打开不校验合法域名、web-view(业务域名)、TLS 版本以及 HTTPS 证书 

  

二.Java 后台 

使用Spring boot 作为后台核心代码

bean类

package com.example.demo.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="car1")
public class Car {
  private  String name;
  private Integer age;
public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}
public Integer getAge() {
	return age;
}
public void setAge(Integer age) {
	this.age = age;
}
@Override
public String toString() {
	return "Car [name=" + name + ", age=" + age + "]";
}
  
}

Controller类

package com.example.demo.controller;

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

import com.example.demo.bean.Car;


@RestController
public class HelloController {

@RequestMapping("/car")
public String car(Car car) {
	System.out.println(car.getName()+car.getAge());
	return "成功";
}
}

猜你喜欢

转载自blog.csdn.net/qq_38474916/article/details/87803533
今日推荐