String json = "{\"2\":\"efg\",\"1\":\"abc\"}"; JSONObject json_test = JSONObject.fromObject(json);
<dependency>
<groupId>net.sf.json-lib </groupId>
<artifactId>json-lib </artifactId>
<version>2.4 </version>
<classifier>jdk15 </classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils </groupId>
<artifactId>commons-beanutils </artifactId>
<version>1.8.0 </version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections </groupId>
<artifactId>commons-collections </artifactId>
<version>3.2.1 </version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
<dependency>
<groupId>net.sf.ezmorph </groupId>
<artifactId>ezmorph </artifactId>
<version>1.0.6 </version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang </groupId>
<artifactId>commons-lang </artifactId>
<version>2.5 </version>
</dependency>
public String diliverMessage( @RequestBody String message) throws Exception{
System. out .println(message) ;
try {
if (message != null) {
//完善用户画像
UserImage userImage = new UserImage() ;
//先获取cid bid组成主键
// net.sf.json.JSONObject json_test = net.sf.json.JSONObject.fromObject("{\"BID\":\"111\",\"CID\":\"222\",\"Content\":\"ok\",\"MsgTypeId\":\"1\"}");
net.sf.json.JSONObject json_test = net.sf.json.JSONObject. fromObject(message) ;
JSONObject jsonMessage =JSONObject. parseObject(json_test.toString()) ;
String CID = jsonMessage.get( "CID").toString() ;
String BID = jsonMessage.get( "BID").toString() ;
//主键生成方式cid+bid
userImage.setId(CID + BID) ;
userImage.setCid(CID) ;
//在获取消息类型msgTypeId 消息内容Content做判断
Integer MsgTypeId = Integer. parseInt(jsonMessage.get( "MsgTypeId").toString()) ;
JSONObject content= null;
if (MsgTypeId != 1){
content= JSONObject. parseObject(jsonMessage.get( "Content").toString()) ;
}
// 目前为止UserImage 仅仅有id和cid属性 其余都是null
//文本消息则调用接口
if (MsgTypeId == 1) {
String content1=jsonMessage.get( "Content").toString() ;
//得到返回值并且增加了熔断所以需要对结果进行判断
HystrixUtils hystrixUtils= new HystrixUtils(content1) ;
//得到结果
String retValue1 = hystrixUtils.execute() ;
if ( "error".equalsIgnoreCase(retValue1)){
//logger.error("failed",new Exception("分类系统获取消息失败").fillInStackTrace());
return "0" ;
}
//System.out.println(retValue1+"...");
JSONObject dataObject=JSONObject. parseObject(retValue1) ;
//分类系统获取消息成功
if ( "ok".equalsIgnoreCase(dataObject.get( "msg").toString())){
String retValue=dataObject.get( "data").toString() ;
JSONArray array = JSONObject. parseArray(retValue) ;
RetValue newValue = null;
for ( int i = 0 ; i < array.size() ; i++) {
//解析
newValue = JSONObject. parseObject(array.get(i).toString() , RetValue. class) ;
//执行操作
analyzeRetValue(userImage , newValue) ;
}
}
} else if (MsgTypeId == 20) {
/* 得到模板消息 关于手机号*/
//从Content中获取手机号
String usermobile = content.get( "usermobile").toString() ;
analyzePhone(userImage , usermobile) ;
} else if (MsgTypeId == 22) {
/* 是模板消息 更新车型 贷款 置换 本地*/
//获得数组
String demanditem = content.getString( "demanditem") ;
analyzeNotPhone(userImage , demanditem) ;
}
}
return "1" ;
} catch (Exception e){
e.printStackTrace() ;
logger.error( "failed!" ,e.fillInStackTrace()) ;
}
return "0" ;
}