javaweb作为服务端,怎么配置服务器,能让小程序访问到
发布于 6 年前 作者 hzou 16073 次浏览 来自 问答

javaweb作为服务端,容器是tomcat6,怎么配置服务器,能让小程序访问到

10 回复
[@Controller](/user/Controller)
//比如:影院列表:/cinema/queryCinema.action
[@RequestMapping](/user/RequestMapping)("/cinema")
public class CinemaController {
    [@Autowired](/user/Autowired)
    private CinemaService cinemaService;
     
    [@RequestMapping](/user/RequestMapping)("/queryCinema")
    public [@ResponseBody](/user/ResponseBody) Map<String, Cinema[]> queryCinema(Cinema cinema) throws Exception {
        List<Cinema> cinemaList=cinemaService.findCinemaList(cinema);
        Cinema[] cl = (Cinema[]) cinemaList.toArray(new Cinema[cinemaList.size()]);
        Map<String, Cinema[]> map = new HashMap<String, Cinema[]>();
        map.put("cinemas", cl);
        return map;
    }

这是后台controller代码   用注解@ResponseBody直接可以返回json格式的数据

小程序可以直接访问这个链接访问到数据

 如果正式发布了,怎么办呢

不用的

把最下面的打钩就不检测域名

 小程序的代码是什么

我没用wx.socket,把Javaweb工程发布到Tomcat,直接一个链接访问Tomcat你的项目,http://localhost:8080/wxTicket/cinema/queryCinema.action?lat=37.5285652317&lng=121.3655765023

 我写成这样,不对

var connectionbol=false;
//input.js
Page({
  data: {
  
  },
  onLoad:function(){
     wx.connectSocket({
       url: ‘ws://192.168.0.220:8080/WebScada/getPowerCurveFromApp.action’,
       data: {
       
       },
       header: {
         ‘content-type’: ‘application/json’
       },
       method: ‘POST’,
       success: function (msg) {
         console.log(msg);

       }
      
     })
     wx.onSocketOpen(function () {
       connectionbol=true;
      })
     wx.onSocketMessage(function () {
       data: “”
     });
  },
  myquery:function(){
    if (connectionbol){
      wx.sendSocketMessage({
        data: {
          “paramid”: “M_20161017003”,
          “thetype”: “t_moniterCenter”,
          “starttime”: “2016-11-15”,
          “datatype”: “realtime”
        }
      });
    }
  }
})

用wx:request

wx.request({

                    url: 'http://localhost:8080/wxTicket/cinema/queryCinema.action',

                    data: {

                      lat:37.5285652317,

                      lng:121.3655765023

                    },

                    header: {

                        'Content-Type': 'application/json'

                    },

                    success: function(res) {

                         console.log(res);

                     }

                })

 原来是这样,我以为是要用https开头,还要设置服务器域名,视频上是这样讲的

 正式发布后,还能用’ws://192.168.0.220:8080/WebScada/getPowerCurveFromApp.action’,这种地址吗

 是用wx.socket,是吗

回到顶部