webservice介面編寫
淺談webservice介面,
我接收的是別人傳給我的xml檔案,接收完之後解析為map ,然後再獲取資料,對資料進行增刪改查
1.action內
public String jzshBaseinfo(String xml){
String result="104";
String msg="";
System.out.println("片區代維同步介面傳送xml:::::【"+xml+"】");
//此處相當於service層
BasestationInfo bs=new BasestationInfoImpl();
try {
//得到xml資訊
int rs=bs.createsite(xml);//此方法是接收到的xml後要去service層做的操作
if(rs==1){
result="0";
msg="呼叫成功";
}
} catch (Exception e) {
String error=e.getMessage();
if(error.split("#").length==2){
result=error.split("#")[0];
msg=error.split("#")[1];
}else{
msg=error;
}
e.printStackTrace();
}
String resultXml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<root>\n" +
" <result>"+result+"</result>\n" +
" <msg>"+msg+"</msg>\n" +
"</root>";
return resultXml;
}
2.
public int createsite(String xml) throws Exception {
ListOrderedMap omap=new ListOrderedMap();
try {
omap=this.clXmlToMap(xml);
//更新jzsh基站資料
BasestationInfoDao bsDao=new BasestationInfoDaoImpl();
String type=Tools.vs(omap.get("type"));
System.out.println(type+"-------測試型別");
//如果是3G BSC和BTSID欄位對應基站表BSC和BTSID欄位
if(type.equals("3G")){
bsDao.update(Tools.vs(omap.get("basestation_name")),Tools.vs(omap.get("enodebid")),Tools.vs(omap.get("bsc")),Tools.vs(omap.get("btsid")),Tools.vs(omap.get("type")),Tools.vs(omap.get("district")),Tools.vs(omap.get("maintenance")),Tools.vs(omap.get("site")));
}else{
//如果是4G enodebID 對應 基站表BTSID
bsDao.updateTG(Tools.vs(omap.get("basestation_name")),Tools.vs(omap.get("enodebid")),Tools.vs(omap.get("type")),Tools.vs(omap.get("district")),Tools.vs(omap.get("maintenance")),Tools.vs(omap.get("site")),Tools.vs(omap.get("bsc")),Tools.vs(omap.get("btsid")));
}
}catch (Exception e) {
e.printStackTrace();
String msg=e.getMessage();
if(msg.split("#").length==2){
throw new RuntimeException(msg);
}else{
throw new RuntimeException("104#"+msg);
}
}
return 1;
}
//基站三化將xml解析為一個基站map
private ListOrderedMap clXmlToMap(String xml) {
ListOrderedMap omap=new ListOrderedMap();
if(xml.length()<5){
throw new RuntimeException("101#XML內容錯誤");
}
String basestation_name=Tools.xmlForValue(xml, "<basestation_name>", "</basestation_name>");
String enodebid=Tools.xmlForValue(xml, "<enodebid>", "</enodebid>");
String bsc=Tools.xmlForValue(xml, "<bsc>", "</bsc>");
String btsid=Tools.xmlForValue(xml, "<btsid>", "</btsid>");
String type=Tools.xmlForValue(xml, "<type>", "</type>");
String district=Tools.xmlForValue(xml, "<district>", "</district>");
String maintenance=Tools.xmlForValue(xml, "<maintenance>", "</maintenance>");
String site=Tools.xmlForValue(xml, "<site>", "</site>");
//String site=Tools.xmlForValue(xml, "<site>", "</site>");
System.out.println(xml+"----測試");
if(basestation_name!=null&&basestation_name!=""){
omap.put("basestation_name", basestation_name);
}else{
throw new RuntimeException("102#basestation_name不能為空");
}
if(enodebid!=null&&enodebid!=""){
omap.put("enodebid", enodebid);
}else{
omap.put("enodebid", null);
}
if(bsc!=null&&bsc!=""){
omap.put("bsc", bsc);
}else{
omap.put("bsc", null);
}
if(btsid!=null&&btsid!=""){
omap.put("btsid", btsid);
}else{
omap.put("btsid", null);
}
if(type!=null&&type!=""){
omap.put("type", type);
}else{
throw new RuntimeException("102#type不能為空");
}
if(district!=null&&district!=""){
omap.put("district", district);
}else{
throw new RuntimeException("102#district不能為空");
}
if(maintenance!=null&&maintenance!=""){
omap.put("maintenance", maintenance);
}else{
throw new RuntimeException("102#maintenance不能為空");
}
if(site!=null&&site!=""){
omap.put("site", site);
}else{
omap.put("site", null);
}
return omap;
}
3.做測試
public static void main(String[] args) {
/*String xml= " <?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<root><basestation_name>ZHF_PG移動平谷上鎮</basestation_name><enodebid>12" +
"</enodebid><bsc>12</bsc><btsid>2</btsid><type>4G</type><district></district>" +
"<maintenance>電信維護11111111區</maintenance><site></site></root>";*/
String xml= " <?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<root><basestation_name>WN02341HHC_CY中軸路B號綜合樓</basestation_name><enodebid>112826" +
"</enodebid><bsc>1</bsc><btsid>1</btsid><type>3G</type><district>南郊區</district>" +
"<maintenance>電信維護16區</maintenance><site>221</site></root>";
try {
//測試地址
//String endPoint = "http://192.168.2.155:8088/JzshPort/services/BasestationInfo?WSDL";
Service service = new Service();
Call call = (Call) service.createCall();
//call.setOperationName(new QName("http://www.JzshPort.com","jzshBaseinfo"));
call.setTargetEndpointAddress(new URL(endPoint));
call.setOperationName(new QName("http://www.JzshPort.com","jzshBaseinfo"));
//createsite
System.out.println(call+"--------");
String result = (String) call.invoke(new Object[] {xml});
System.out.println("SSSSSSSS"+result);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}