注意:java
一、自定義菜單最多包括3個一級菜單,每一個一級菜單最多包含5個二級菜單。spring
二、一級菜單最多4個漢字,二級菜單最多7個漢字,多出來的部分將會以「...」代替。json
三、測試時能夠嘗試取消關注公衆帳號後再次關注,則能夠看到建立後的效果。api
按鈕類型:數組
一、click:點擊推事件用戶點擊click類型按鈕後,微信服務器會經過消息接口推送消息類型爲event的結構給開發者(參考消息接口指南),而且帶上按鈕中開發者填寫的key值,開發者能夠經過自定義的key值與用戶進行交互;
二、view:跳轉URL用戶點擊view類型按鈕後,微信客戶端將會打開開發者在按鈕中填寫的網頁URL,可與網頁受權獲取用戶基本信息接口結合,得到用戶基本信息。服務器
建立菜單接口:微信
http請求方式:POST(請使用https協議) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKENapp
click和view類型按鈕的請求數據格式以下:jsp
{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"name":"菜單",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"miniprogram",
"name":"wxa",
"url":"http://mp.weixin.qq.com",
"appid":"wx286b93c14bbf93aa",
"pagepath":"pages/lunar/index"
},
{
"type":"click",
"name":"贊一下咱們",
"key":"V1001_GOOD"
}]
}]
}ide
進行開發:
(一):封裝按鈕對象
一:基類
- public class Button {
-
- private String name;//全部一級菜單、二級菜單都共有一個相同的屬性,那就是name
-
-
- public String getName() {
- return name;
- }
-
-
- public void setName(String name) {
- this.name = name;
- }
-
-
- }
二:子菜單類
- /**
- * 描述: 子菜單項 :沒有子菜單的菜單項,有多是二級菜單項,也有多是不含二級菜單的一級菜單。
- */
- public class CommonButton extends Button {
-
- private String type;
- private String key;
- private String url;
-
-
- public String getType() {
- return type;
- }
-
-
- public void setType(String type) {
- this.type = type;
- }
-
-
- public String getKey() {
- return key;
- }
-
-
- public void setKey(String key) {
- this.key = key;
- }
-
-
- public String getUrl() {
- return url;
- }
-
-
- public void setUrl(String url) {
- this.url = url;
- }
-
-
- }
三:父菜單類
- /**
- * 描述: 父菜單項 :包含有二級菜單項的一級菜單。這類菜單項包含有二個屬性:name和sub_button,而sub_button以是一個子菜單項數組
- */
- public class ComplexButton extends Button {
- private Button[] sub_button;
-
-
- public Button[] getSub_button() {
- return sub_button;
- }
-
-
- public void setSub_button(Button[] sub_button) {
- this.sub_button = sub_button;
- }
- }
(二):封裝生成菜單的方法
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.mote.weixin.entry.Menu;
-
-
- public class MenuUtils {
-
- private static ObjectMapper MAPPER = new ObjectMapper();
-
-
- public static int createMenu(Menu menu, String accessToken) throws Exception {
- int result = 0;
- // 拼裝建立菜單的url
- String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accessToken+"";
- // 將菜單對象轉換成json字符串
- String jsonMenu = MAPPER.writeValueAsString(menu);
- // 調用接口建立菜單,CommonUtils是上一篇中介紹的內容
- String resq = CommonUtils.Post_Json(url, jsonMenu);
-
- JsonNode tree = MAPPER.readTree(resq);
- if(tree.get("errcode").toString().equals("0")){
- System.out.println("菜單建立成功!");
- }else{
- System.out.println("菜單建立失敗!");
- }
-
- return result;
- }
- }
(三):組裝數據,生成菜單
- import org.springframework.stereotype.Controller;
-
-
- import com.mote.weixin.entry.Button;
- import com.mote.weixin.entry.CommonButton;
- import com.mote.weixin.entry.ComplexButton;
- import com.mote.weixin.entry.Menu;
- import com.mote.weixin.utils.CommonUtils;
- import com.mote.weixin.utils.MenuUtils;
-
-
- @Controller
- public class MenuController {
-
- public static void main(String[] args) {
-
-
- try {
- // 調用接口獲取access_token,CommonUtils是上一篇中的內容
- String accessToken = CommonUtils.getAccessToken();
- // 調用接口建立菜單
- MenuUtils.createMenu(getMenu(), accessToken);
- } catch (Exception e) {
- System.out.println("菜單建立失敗");
- e.printStackTrace();
- }
-
-
- }
-
-
- /**
- * 組裝菜單數據
- *
- * @return
- */
- private static Menu getMenu() {
-
-
- CommonButton btn11 = new CommonButton();
- btn11.setName("校園導航");
- btn11.setType("view");
- btn11.setUrl("http://02d73f21.ngrok.io/wx_coges/navi.jsp");
-
-
- CommonButton btn21 = new CommonButton();
- btn21.setName("學校風采");
- btn21.setType("view");
- btn21.setKey("21");
- btn21.setUrl("https://www.baidu.com/");
-
-
- CommonButton btn31 = new CommonButton();
- btn31.setName("健身服務");
- btn31.setType("click");
- btn31.setKey("31");
-
-
- CommonButton btn32 = new CommonButton();
- btn32.setName("愛情諮詢");
- btn32.setType("click");
- btn32.setKey("32");
-
-
- /**
- * 微信: mainBtn2,mainBtn3底部的三個一級菜單
- */
-
-
- ComplexButton mainBtn3 = new ComplexButton();
- mainBtn3.setName("更多服務");
- mainBtn3.setSub_button(new CommonButton[] { btn31, btn32});
-
-
- /**
- * 封裝整個菜單
- */
- Menu menu = new Menu();
- menu.setButton(new Button[] { btn11, btn21, mainBtn3 });
-
-
- return menu;
- }
-
-
- }
ok了,直接執行main方法就能夠生成菜單了,試試吧

原文做者:祈澈姑娘