First, the directory structure
2.pom introduced
<!-- io操作的工具类库 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency>
package com.grand.weichat; import java.io.IOException; import java.nio.charset.Charset; import org.apache.commons.io.IOUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.io.Resource; import org.springframework.test.context.junit4.SpringRunner; import com.grand.weichat.util.base.WXUtilsCenter; import cn.hutool.http.HttpUtil; import net.sf.json.JSONObject; @RunWith(SpringRunner.class) @SpringBootTest public class Menu { @Value("${conf.appId}") for appId String Private; // custom menu generation @Value ( "conf.appSecret $ {}") Private String appsecret; @value ( "the CLASSPATH: static / menu.json") Private Resource menujson; / ** * delete all custom menus * / @Test public void deleteAllMenu ( ) { String token = WXUtilsCenter.getAccessToken (for appId, appsecret); String URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + token; String Result = HttpUtil.get (URL); the JSONObject JSON JSONObject.fromObject = (Result); IF ((Integer) json.get ( "The errcode") == 0) { System.out.println ( "delete menu successfully"); } {the else the System. out.println ( "delete the menu failed"); } } @Test public void testMenu(){ the try { // get the custom menu. 1 json data of the system directory. String areaData = IOUtils.toString (menujson.getInputStream (), Charset.forName ( "UTF-. 8")); System.out.println ( areaData); . 2 // get access_token initiation request to generate a custom menu String token = WXUtilsCenter.getAccessToken (for appId, appsecret); String URL = "https://api.weixin.qq.com/cgi-bin/menu/ the access_token = the Create "+ token +" ";? String the Result = HttpUtil.post (url, areaData.toString ()); JSONObject json = JSONObject.fromObject (the Result); IF ((Integer) json.get (" The errcode ") = 0 =) { System.out.println ( "settings menu successfully"); } the else { System.out.println ( "settings menu failed"); } } catch (IOException e) { e.printStackTrace(); } } }