施樂榮 發表於 2019-11-11 10:52:00

微信小程序开发——后端Java(一)

<h2>一、前言</h2>
<p>最近接触了小程序的开发,后端选择Java,因为小程序的代码运行在腾讯的服务器上,而我们自己编写的Java代码运行在我们自己部署的服务器上,所以一开始不是很明白小程序如何与后台进行通信的,然后查找资料发现结合了官方提供的api后好像和我们普通的web前后端通信也没有多大的区别,有想法后就写了这个测试程序。</p>
<p>&nbsp;</p>
<h2>二、微信小程序开发基础</h2>
<h3>1、不校验域名安全性</h3>
<p>大家在刚开始开发的时候一般都没有自己的服务器及域名,所以大家在本地编写的时候,在“详细”下的“项目设置”里面将“不校验域名安全性”勾选。</p>
<p><img src="https://img2018.cnblogs.com/i-beta/1832356/201911/1832356-20191111103131346-322044474.png" alt=""></p>
<h3>&nbsp;2、了解wx.request(OBJECT)</h3>
<p>可先阅读<span style="color: rgba(51, 153, 102, 1)"><span style="color: rgba(51, 153, 102, 1)">官方文档</span></span></p>
<p><strong>OBJECT参数说明:</strong></p>
<p>&nbsp;<img src="https://img2018.cnblogs.com/i-beta/1832356/201911/1832356-20191111105902943-1322695083.png" alt=""></p>
<p><strong>&nbsp;success返回参数说明:</strong></p>
<p><img src="https://img2018.cnblogs.com/i-beta/1832356/201911/1832356-20191111105922614-347761894.png" alt=""></p>
<p><strong>data 数据说明:</strong></p>
<p>最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:</p>
<ul>
<li>对于&nbsp;<code>header['content-type']</code>&nbsp;为&nbsp;<code>application/json</code>&nbsp;的数据,会对数据进行 JSON 序列化</li>
<li>对于&nbsp;<code>header['content-type']</code>&nbsp;为&nbsp;<code>application/x-www-form-urlencoded</code>&nbsp;的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&amp;encodeURIComponent(k)=encodeURIComponent(v)...)</li>
</ul>
<h3>3、.json文件不能为空</h3>
<p>必须加上{}</p>
<p>&nbsp;</p>
<h2>三、Java后端编写</h2>
<p><span style="color: rgba(153, 51, 0, 1)"><strong>主要框架springboot,开发工具idea,服务器阿里云服务器。</strong></span></p>
<h3>1、创建一个maven项目,导入相关依赖:&nbsp;</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 统一版本控制 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">parent</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-parent<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>2.2.1.RELEASE<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">relativePath</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> lookup parent from repository </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">parent</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> freemarker渲染页面 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-freemarker<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

      <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> spring boot 核心 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-web<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

      <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> springboot整合jsp </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> tomcat 的支持. </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-web<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">exclusions</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">exclusion</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                  <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                  <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-tomcat<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">exclusion</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">exclusions</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.apache.tomcat.embed<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>tomcat-embed-jasper<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h3>2、创建application.yml文件,修改一些配置参数等</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">server:
port: 8083
ssl:
    key-store: classoath:xxxxxx.pfx
    key-store-password: xxxxxx
    key-store-type: xxxxxx</span></pre>
</div>
<p><span style="color: rgba(153, 51, 0, 1)"><strong>在实际项目中可能涉及数据库,还要整合mybatis,在文章中,仅仅做测试就不做使用数据库的测试</strong></span></p>
<h3>3、首先修改springboot的启动程序:</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.wx.m3;

@ComponentScan(basePackages</span>= "com.wx.m3")<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加扫包@ComponentScan(basePackages= "")</span>
<span style="color: rgba(0, 0, 0, 1)">@EnableAutoConfiguration
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> M3Application {

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
      SpringApplication.run(M3Application.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args);
    }

}</span></pre>
</div>
<p><span style="color: rgba(0, 0, 0, 1)"><strong>启动项目时直接右击run即可</strong></span></p>
<h3>&nbsp;4、在写一个测试的controller进行微信小程序与java后端实现通信,controller代码如下:</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@RestController
@SpringBootApplication
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> IndexController {

    @RequestMapping(</span>"getUser"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Map&lt;String,Object&gt;<span style="color: rgba(0, 0, 0, 1)"> getUser(){
      System.out.println(</span>"微信小程序正在调用..."<span style="color: rgba(0, 0, 0, 1)">);
      Map</span>&lt;String,Object&gt; map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)">();
      List</span>&lt;String&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
      list.add(</span>"Amy"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"Cathy"<span style="color: rgba(0, 0, 0, 1)">);
      map.put(</span>"list"<span style="color: rgba(0, 0, 0, 1)">,list);
      System.out.println(</span>"微信小程序调用完成..."<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> map;
    }

    @RequestMapping(</span>""<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getTest(){
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Hello world"<span style="color: rgba(0, 0, 0, 1)">;
    }

}</span></pre>
</div>
<p><strong>至此简易的后端框架基本完成</strong></p>
<p><strong>  说明:@RestController与Controller注解的区别</strong></p>
<p><strong>      @RestController相当于它能实现将后端得到的数据在前端页面(网页)中以json串的形式传递。</strong></p>
<p><strong>      微信小程序与后台数据之间的数据传递就是以<span style="color: rgba(255, 0, 0, 1)">json报文的形式传递。这也是选择springboot框架开发小程序后台的主要原因之一,可以方便进行小程序后套开发</span></strong></p>
<p>&nbsp;</p>
<h2>四、小程序发起网络请求</h2>
<p>下面以一个简单的按钮请求数据为例:</p>
<h3><span style="color: rgba(0, 0, 0, 1)">hello.wxml文件:</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)">bindtap</span><span style="color: rgba(0, 0, 255, 1)">='houduanButton1'</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>点击发起请求<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">wx:for</span><span style="color: rgba(0, 0, 255, 1)">="{{list}}"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
    姓名:{{item}}
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h3><span>hello.js文件:</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">Page({
data:{
    list: </span>''<span style="color: rgba(0, 0, 0, 1)">
},
houduanButton1:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> that = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    wx.request({
      url: </span>'http://localhost:8083/getUser'<span style="color: rgba(0, 0, 0, 1)">,
      method: </span>'GET'<span style="color: rgba(0, 0, 0, 1)">,
      header: {
      </span>'content-type':'application/json'<span style="color: rgba(0, 0, 0, 1)">
      },
      success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res){
      console.log(res.data);
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> list =<span style="color: rgba(0, 0, 0, 1)"> res.data.list;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(list == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
          </span><span style="color: rgba(0, 0, 255, 1)">var</span> toastText = '数据获取失败'<span style="color: rgba(0, 0, 0, 1)">;
          wx.showToast({
            title: toastText,
            icon: </span>''<span style="color: rgba(0, 0, 0, 1)">,
            duration: </span>2000<span style="color: rgba(0, 0, 0, 1)">
          });
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
          that.setData({
            list: list
          })
      }
      }
    })
}
})<br></span></pre>
</div>
<h3>app.json:</h3>
<p>将hello放到第一行,则首先进入hello.wxml</p>
<p><img src="https://img2018.cnblogs.com/i-beta/1832356/201911/1832356-20191111105033852-889021395.png" alt=""></p>
<p>&nbsp;</p>
<p><span><strong>测试结果如下所示</strong>:</span></p>
<p><span>  点击按钮显示姓名</span></p>
<p><span><img src="https://img2018.cnblogs.com/i-beta/1832356/201911/1832356-20191111105118262-738363919.png" alt=""></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/csyzlm/p/11833511.html
頁: [1]
查看完整版本: 微信小程序开发——后端Java(一)