添加jar
<!--json转为对象包-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.3</version>
</dependency>
<!--解析xml-->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
实体类
公共消息实体
public class BaseMessage {
protected String ToUserName;
protected String FromUserName;
protected long CreateTime;
protected String MsgType;
private Voice Voice;
public Voice getVoice() {
return Voice;
}
public void setVoice(Voice voice) {
Voice = voice;
}
public BaseMessage() {
super();
}
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
public void setFromUserName(String fromUserName) {
FromUserName = fromUserName;
}
public long getCreateTime() {
return CreateTime;
}
public void setCreateTime(long createTime) {
CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String msgType) {
MsgType = msgType;
}
}
文本消息
public class MessageText extends BaseMessage {
private String Content;// 文本消息内容
private String MsgId;// 消息id,64位整型
public MessageText() {
}
public MessageText(String toUserName, String fromUserName, long createTime, String msgType, String content,
String msgId) {
super();
ToUserName = toUserName;
FromUserName = fromUserName;
CreateTime = createTime;
MsgType = msgType;
Content = content;
MsgId = msgId;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getMsgId() {
return MsgId;
}
public void setMsgId(String msgId) {
MsgId = msgId;
}
}
语音消息
public class Voice extends BaseMessage{
private String MediaId;
public Voice(){}
public Voice(String toUserName, String fromUserName, long createTime, String msgType,
String mediaId) {
super();
ToUserName = toUserName;
FromUserName = fromUserName;
CreateTime = createTime;
MsgType = msgType;
MediaId = mediaId;
}
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
}
消息工具类
import com.thoughtworks.xstream.XStream;
import java.util.Date;
public class TextMessageUtil {
/**
* 将发送消息封装成对应的xml格式
*/
public String messageToxml(BaseMessage message) {
XStream xstream = new XStream();
xstream.alias("xml", message.getClass());
return xstream.toXML(message);
}
/**
* 封装文本发送消息对象,封装时,需要将调换发送者和接收者的关系
* @param FromUserName
* @param ToUserName
*/
public String initMessage(String FromUserName, String ToUserName,String content) {
MessageText text = new MessageText();
text.setToUserName(FromUserName);
text.setFromUserName(ToUserName);
text.setContent(content);
text.setCreateTime(new Date().getTime());
text.setMsgType("text");
return this.messageToxml(text);
}
/**
* 封装音乐发送消息对象,封装时,需要将调换发送者和接收者的关系
* @param FromUserName
* @param ToUserName
*/
public String musicMessage(String FromUserName, String ToUserName,String mediaId) {
BaseMessage text = new BaseMessage();
text.setToUserName(FromUserName);
text.setFromUserName(ToUserName);
text.setCreateTime(new Date().getTime());
text.setMsgType("voice");
this.messageToxml(text);
Voice music=new Voice();
music.setMediaId(mediaId);
text.setVoice(music);
return this.messageToxml(text);
}
}
请求路径
@PostMapping("checkToken")
public void selectMenu(HttpServletRequest request, HttpServletResponse response){
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
//将微信请求xml转为map格式,获取所需的参数
Map<String,String> map = MessageUtil.xmlToMap(request);
String ToUserName = map.get("ToUserName");
String FromUserName = map.get("FromUserName");
String MsgType = map.get("MsgType");
String Content = map.get("Content");
System.out.println(ToUserName);
System.out.println(FromUserName);
System.out.println(MsgType);
System.out.println(Content);
String message = null;
//处理文本类型,实现输入1,回复相应的封装的内容
if("text".equals(MsgType)){
TextMessageUtil textMessage = new TextMessageUtil();
if("我爱你".equals(Content)){
message = textMessage.musicMessage(FromUserName, ToUserName,“这里填写你上传语音文件时返回的media_id”);
System.out.println(message);
}else{
message = textMessage.initMessage(FromUserName, ToUserName,"我是你爸爸,给我好好说话,说的什么乱七八糟的我听不懂");
System.out.println(message);
}
}
try {
out = response.getWriter();
out.write(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
}