WeChat Official Account Java Development Record (7) Reply to music message

One, pom dependence

    <!-- 将xml包解析成Map -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- 将Map解析成XML包 -->
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.11.1</version>
        </dependency>

Two, entity class code

xml data packet format
Insert picture description here
Insert picture description here
note:
1. The media id parameter of the thumbnail is the id obtained through the multimedia file uploaded through the interface in the material management;
2. The MusicUrl is now opened in the browser to see if it can be played, I am downloading the music file ( mp3 format) uploaded to FastDFS, the returned path can be opened and played in the browser.
3. Note that the parameter name is the name in the xml format, the following parameter explanation MusicUrl is written as MusicURL, so it cannot be parsed, and should be written according to the name in the xml.
BUG :
It is best to write all the above parameters, because I thought that the HQMusicUrl parameter is not necessary, so if I didn't write it, the official account could not be parsed, and it kept returning "the official account failed".

Music

@Data
public class Music {
    
    
    /**
     * 音乐标题
     */
    @XStreamAlias("Title")
    private String title ;
    /**
     * 音乐描述
     */
    @XStreamAlias("Description")
    private String description;
    /**
     * 音乐连接
     */
    @XStreamAlias("MusicUrl")
    private String musicURL;
    /**
     * 高质量音乐链接,WIFI环境优先使用该链接播放音乐
     */
    @XStreamAlias("HQMusicUrl")
    private String hQMusicUrl;
    /**
     * 缩略图的媒体id,通过素材管理中的接口上传多媒体文件,得到的id
     */
    @XStreamAlias("ThumbMediaId")
    private String thumbMediaId;
}

BaseMessage

@Data
@XStreamAlias("xml")
public class BaseMessage {
    
    
    @XStreamAlias("ToUserName")
    private String toUserName;
    @XStreamAlias("FromUserName")
    private String fromUserName;
    @XStreamAlias("CreateTime")
    private String createTime;
    @XStreamAlias("MsgType")
    private String msgType;
    @XStreamAlias("MsgId")
    private String msgId;


    public BaseMessage(Map<String ,String> map){
    
    
        this.fromUserName=map.get("ToUserName");
        this.toUserName=map.get("FromUserName");
        this.createTime = System.currentTimeMillis()/1000+"";
    }
    public BaseMessage(){
    
    

    }
}

MusicMessage

@Data
@XStreamAlias("xml")
public class MusicMessage extends BaseMessage{
    
    
    @XStreamAlias("Music")
    private Music music;

    public MusicMessage(Map<String ,String> map,Music music){
    
    
        super(map);
        this.setMsgType("music");
        this.music = music;
    }

    public MusicMessage(){
    
    

    }
}

Insert picture description here

effectInsert picture description here


The thumbnail uploaded by the bug is not displayed.
to be solved.

Guess you like

Origin blog.csdn.net/DreamsArchitects/article/details/109220553