本文的目的是介绍针对WebService使用Service类获取Port类的一个参数问题解释的详细情况,特别关注获取service对象的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈
本文的目的是介绍针对WebService使用Service类获取Port类的一个参数问题解释的详细情况,特别关注获取service对象的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解针对WebService使用Service类获取Port类的一个参数问题解释的机会,同时也不会遗漏关于axis2实现webservice之使用services.xml文件发布WebService、Java调用WebService方法总结(3)--wsimport调用WebService、Jax-ws 开发webService ,并使用spring注入service类中遇到 空指针、Ksoap2+WebService+BaseAdapter 实现android对WebService的调用的知识。
本文目录一览:- 针对WebService使用Service类获取Port类的一个参数问题解释(获取service对象)
- axis2实现webservice之使用services.xml文件发布WebService
- Java调用WebService方法总结(3)--wsimport调用WebService
- Jax-ws 开发webService ,并使用spring注入service类中遇到 空指针
- Ksoap2+WebService+BaseAdapter 实现android对WebService的调用
针对WebService使用Service类获取Port类的一个参数问题解释(获取service对象)
最后在学习WebService时,看到对于官方的例子是这样写的.
1
2
3
4
5
6
7
|
private
static
final
QName PORT_NAME
=
new
QName(
"http://server.hw.demo/"
,
"HelloWorldPort"
);
Service service = Service.create(SERVICE_NAME);
String endpointAddress =
"http://localhost:9000/helloWorld"
;
service.addPort(PORT_NAME,SOAPBinding.soAP11HTTP_BINDING,endpointAddress);
HelloWorld hw = service.getPort(HelloWorld.
class
);
|
注意看上面的PORT_NAME的定义,是一个QName,其就有namespaceURI和一个name值,而在使用serivce获取port时直接传递了接口名参数。
这样的例子是可以运行的,这就导致了本人在编写参考例子时,直接copy了相应的代码,但是修改了各项名称,在运行时,即始终运行不起来,相应的错误为
java.net.MalformedURLException: Invalid address. Endpoint address cannot be
null
.
以上的错误直接让人找不到方向,而实际问题是,service根据所传递的信息,在只传递了接口信息时,会默认构建一个QName的信息,再从service中寻找,如果寻找不到,自然就会产生上面的错误了。
在官方的例子中,它会默认构建HelloWorldPort这样qname去寻找,而在进行service.add时,恰好添加的就是HelloWorldPort这个qname,那么就恰好寻找到了。
而我们的例子,由于做了很多处理,导致默认添加到service的port的name并不是Service.class.getName+Port的组合,那么自然就找不到相应的port了。而正确的做法,其实也很简单,就是在获取port的时候,手动地指定要获取port类的qname,如下所示:
QName userServicePortQName =
"http://cxf.java.study.m_ylf.com/"
"abcPort"
);
service.addPort(userServicePortQName,255)!important">"http://localhost:8080/userService"
);
UserService userService = service.getPort(userServicePortQName,UserService.
即在往service时添加什么样的port,那么在获取时就使用什么样的qname。再一步理解,addPort这个方法就可以理解为以键值对的方式往service里追加port,那么在获取的时候自然就要提供相应的key值了。如果不提供,就会使用默认的生成策略创建一个key值,那这个key值与addPort使用的key值不一样的话,自然就会产生上面的错误了。
|
axis2实现webservice之使用services.xml文件发布WebService
还是对教程的延伸,本来是周五要写的,但是耽搁了一下,就拖到周一了。
用Axis2实现Web Service,虽然可以将POJO类放在axis2\WEB-INF\pojo目录中直接发布成Web Service,这样做不需要进行任何配置,但这些POJO类不能在任何包中。这似乎有些不方便,为此,Axis2也允许将带包的POJO类发布成Web Service。
先实现一个POJO类,代码如下:
这个类有两个方法,这两个方法都需要发布成Web Service方法。这种方式和直接放在pojo目录中的POJO类不同。要想将MyService类发布成Web Service,需要一个services.xml文件,这个文件需要放在meta-inf目录中,该文件的内容如下:
其中<service>元素用于发布Web Service,一个<service>元素只能发布一个WebService类,name属性表示WebService名,如下面的URL可以获得这个WebService的WSDL内容:
http://localhost:8080/axis2/services/myService?wsdl (这个得等到.aar文件出来之后)
其中name属性名就是上面URL中"?"和"/"之间的部分。
<description>元素表示当前Web Service的描述,<parameter>元素用于设置WebService的参数,在这里用于设置WebService对应的类名。在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。例如,getGreeting方法有一个返回值,因此,需要使用可处理输入输出的RPcmessageReceiver类,而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。
使用这种方式发布WebService,必须打包成.aar文件,..aar文件实际上就是改变了扩展名的.jar文件。在现在建立了两个文件(这两个文件夹任意):MyService.java和services.xml。将MyService.java编译,生成MyService.class。services.xml和MyService.class文件的位置如下:
D:\ws\service\MyService.class
D:\ws\meta-inf\services.xml
在windows控制台中进入ws目录,并输入如下的命令生成.aar文件(实际上,.jar文件也可以发布webservice,但axis2官方文档中建议使用.aar文件发布webservice):
jar cvf ws.aar . jar cvf AxisTest.aar .
如下是我测试的过程
原来发现不管要.aar,之后还需要一个“.”,即.arr.,但是最坑爹的就是这里了,加了“.”之后还是不对啊,最后的问题居然是这个后面的点要空一格,不能紧挨着.arr
如下成功之后的文件夹情况
最后将ws.aar文件复制到<Tomcat安装目录>\webapps\axis2\WEB-INF\services目录中,启动Tomcat后,就可以调用这个WebService了。
已经显示webservice发布成功了,接下来就是编写客户端进行调运了,跟前面一样,也需要wsdl2java命令去生成stub类,过程我就不多说了,之前的博客里有
如下是调用客户端的代码
运行结果
由于“帅哥”是在服务器端打印的,所以客户端是看不到的。
在打包arr文件的时候,发现有一个build.xml文件,这个是ant脚本中的知识,在本题中暂时不涉及,以后再介绍。
如果想发布多个WebService,可以使用<serviceGroup>元素,如再建立一个MyService1类,代码如下:
在services.xml文件中可以使用如下的配置代码来配置MyService和MyService1类:
Java调用WebService方法总结(3)--wsimport调用WebService
wsimport是JDK自带的把WSDL转成Java的工具,可以很方便的生成调用WebService的代码。文中所使用到的软件版本:Java 1.8.0_191。
1、准备
参考Java调用WebService方法总结(1)--准备工作
2、调用
2.1、根据wsdl生成代码
执行命令:
%JAVA_HOME%/bin/wsimport -keep -extension -encoding utf-8 -d d:/temp -p com.inspur.ws.wsimport http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl
-d 指定生成文件的目录 -p 指定包名
生成的代码如下:
2.2、用生成的代码调用WebService
package com.inspur.ws;
import com.inspur.ws.a.TraditionalSimplifiedWebService;
/**
*
* 用wsimport生成的代码调用WebService
*
*/
public class WsImport {
public static void main(String[] args) {
TraditionalSimplifiedWebService service = new TraditionalSimplifiedWebService();
String s = service.getTraditionalSimplifiedWebServiceSoap().toTraditionalChinese("小学");
System.out.println(s);
s = service.getTraditionalSimplifiedWebServiceSoap12().toTraditionalChinese("大学");
System.out.println(s);
}
}
Jax-ws 开发webService ,并使用spring注入service类中遇到 空指针
一般情况下,使用eclipse自带的 jax-ws 生成webservice 会自动生成2个类:
ContractConnector.java
package com.wonders.webservice.contract; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.wonders.webservice.contract.entity.bo.ContractWs; import com.wonders.webservice.contract.service.ContractWsService; public class ContractConnector { public ContractConnector() { System.out.println("初始化connecter"); } private static ContractWsService contractWsService; public void setContractWsService(ContractWsService contractWsService) { this.contractWsService = contractWsService; } public String saveContract(String contractXML,String secret){ if(secret==null || !"abcd".equals(secret)){ return "验证码错误!"; } try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); ContractWs contractWs = new ContractWs(); //存到临时表中去 Document document = DocumentHelper.parseText(contractXML); Element root = document.getRootElement(); /**必填区域**/ //1.合同名称 String contractName = root.elementText("contractName"); if(!isEmpty(contractName)){ contractWs.setContractName(contractName); }else{ return "contractName(合同名称)不能为空"; } //2.合同编号 String contractNo = root.elementText("contractNo"); if(!isEmpty(contractNo)){ contractWs.setContractNo(contractNo); }else{ return "contractNo(合同编号)不能为空"; } //3.合同价 String contractPrice = root.elementText("contractPrice"); if(!isEmpty(contractPrice)){ try { contractWs.setContractPrice(getFormattedMoney(contractPrice)); } catch (Exception e) { return "contractPrice(合同价)只能输入数字"; } }else{ return "contractPrice(合同价)不能为空"; } /** 项目要插id,是否根据projectName和projectNo查询后插入 //4.项目名称 String projectName = root.elementText("projectName"); if(!isEmpty(projectName)){ contract.setProjectName(projectName); }else{ return "projectName(项目名称)不能为空"; } //5.项目编号 String projectNo = root.elementText("projectNo"); if(!isEmpty(projectNo)){ contract.setProjectNo(projectNo); }else{ return "projectNo(项目编号)不能为空"; }**/ /**非必填**/ //6.审定价(投资监理)(万元) String investSupervisorPrice = root.elementText("investSupervisorPrice"); if(!isEmpty(investSupervisorPrice)){ try { contractWs.setFinalPrice(getFormattedMoney(investSupervisorPrice)); } catch (Exception e) { return "investSupervisorPrice(审定价(投资监理)只能输入数字)"; } } //7.审定价(审价单位) String verifyPrice = root.elementText("verifyPrice"); if(!isEmpty(verifyPrice)){ try { contractWs.setVerifyPrice(getFormattedMoney(verifyPrice)); } catch (Exception e) { return "verifyPrice(审定价(审价单位)只能输入数字)"; } } //7.审定价(国家审计) String nationVerifyPrice = root.elementText("nationVerifyPrice"); if(!isEmpty(verifyPrice)){ try { contractWs.setNationVerifyPrice(getFormattedMoney(nationVerifyPrice)); } catch (Exception e) { return "nationVerifyPrice(审定价(国家审计)只能输入数字)"; } } //8.甲方出资 //9.甲方执行 //10.对方单位 //11.支付方式 String payType = root.elementText("payType"); if(!isEmpty(payType)){ contractWs.setPayType(payType); } //12.合同签约时间 String contractSignedDate = root.elementText("contractSignedDate"); if(!isEmpty(contractSignedDate)){ try { sdf.parse(contractSignedDate); contractWs.setContractSignedDate(contractSignedDate); } catch (ParseException e) { return "contractSignedDate(合同签约时间)数据错误"; } } //13.合同审批通过时间 String contractPassedDate = root.elementText("contractPassedDate"); if(!isEmpty(contractPassedDate)){ try { sdf.parse(contractPassedDate); contractWs.setContractPassedDate(contractPassedDate); } catch (ParseException e) { return "contractPassedDate(合同审批通过时间)数据错误"; } } //14.合同开始时间 String contractStartDate = root.elementText("contractStartDate"); if(!isEmpty(contractStartDate)){ try { sdf.parse(contractStartDate); contractWs.setContractStartDate(contractStartDate); } catch (ParseException e) { return "contractStartDate(合同开始时间)数据错误"; } } //15.合同结束时间 String contractEndDate = root.elementText("contractStartDate"); if(!isEmpty(contractEndDate)){ try { sdf.parse(contractEndDate); contractWs.setContractEndDate(contractEndDate); } catch (ParseException e) { return "contractEndDate(合同结束时间)数据错误"; } } //16.合同状态 String contractStatus = root.elementText("contractStatus"); if(!isEmpty(contractStatus)){ if(contractStatus.equals("1") || contractStatus.equals("2") || contractStatus.equals("3")){ contractWs.setContractStatus(contractStatus); }else{ return "contractStatus(合同状态)数据错误"; } } //17.合同类型 String contractType = root.elementText("contractType"); if(!isEmpty(contractType)){ try { String types[] = contractType.split("-"); if(types.length==2){ int type1 = Integer.valueOf(types[0]); int type2 = Integer.valueOf(types[1]); if(type1 == 1){ if(type2>=1 && type2<=8){ contractWs.setContractType(contractType); }else{ return "contractType(合同类型)数据错误"; } }else if(type1==2){ if(type2>=1 && type2<=3){ contractWs.setContractType(contractType); }else{ return "contractType(合同类型)数据错误"; } }else if(type1==3){ if(type2==1){ contractWs.setContractType(contractType); }else{ return "contractType(合同类型)数据错误"; } } }else{ return "contractType(合同类型)数据错误"; } } catch (Exception e) { return "contractType(合同类型)数据错误"; } } //18.采购方式 String procurementType = root.elementText("procurementType"); if(!isEmpty(procurementType)){ try { int inviteBidType = Integer.valueOf(procurementType); if(inviteBidType>=1 && inviteBidType<=3){ contractWs.setInviteBidType(procurementType); }else{ return "procurementType(采购方式)数据错误"; } } catch (NumberFormatException e) { return "procurementType(采购方式)数据错误"; } } //19.合同附件 //20.合同内容 String contractContent = root.elementText("contractContent"); if(!isEmpty(contractContent)){ contractWs.setContractContent(contractContent); } //21.备注信息 String remark = root.elementText("remark"); if(!isEmpty(remark)){ contractWs.setRemark("remark"); } contractWs.setRemoved("0"); contractWs.setCreateDate(sdf.format(new Date())); contractWsService.saveContractWs(contractWs); } catch (DocumentException e) { return "解析错误!"; } return "保存成功"; } public boolean isEmpty(String str){ if(str ==null || "".equals(str)) return true; return false; } //得到保留6位小数后的字符串 public String getFormattedMoney(String money){ if(money==null || "".equals(money)){ money = "0"; } money = money.trim(); Double result = 0d; try { result = Double.valueOf(money); } catch (NumberFormatException e) { result = 0d; } DecimalFormat df = new DecimalFormat("#0.000000"); return df.format(result); } }
ContractConnectorDelegate.java
package com.wonders.webservice.contract; @javax.jws.WebService(targetNamespace = "http://contract.webservice.wonders.com/",serviceName = "ContractConnectorService",portName = "ContractConnectorPort",wsdlLocation = "WEB-INF/wsdl/ContractConnectorService.wsdl") public class ContractConnectorDelegate { com.wonders.webservice.contract.ContractConnector contractConnector = new com.wonders.webservice.contract.ContractConnector(); public String saveContract(String contractXML,String secret) { return contractConnector.saveContract(contractXML,secret); } }
ContractConnector中
ContractWsService该类中包含了存取数据及对hibernate 的使用,所以使用spring注入,但webservice调用时,一直抱空指针。
由于ContractConnectorDelegate类中 需要new ContractConnector类 ,所以无法注入service
故最后将service修改为static 后,程序启动就注入,这样new 了之后,该service 存在。
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
Ksoap2+WebService+BaseAdapter 实现android对WebService的调用
package com.nantian.app; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class Tel3Activity extends Activity implements OnClickListener,OnItemClickListener{ /** Called when the activity is first created. */ private Button submit; private ListView list; private ShowAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); adapter = new ShowAdapter(this); submit = (Button) findViewById(R.id.submit); list = (ListView) findViewById(R.id.list); list.setonItemClickListener(this); submit.setonClickListener(this); } @Override public void onClick(View v) { // Todo Auto-generated method stub //Toast.makeText(this,"===",Toast.LENGTH_LONG).show(); String[] str = {"11","22","33"}; ArrayAdapter a = new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,str); list.setAdapter(adapter); } @Override public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) { // Todo Auto-generated method stub Toast.makeText(this,"hi,thank for u click!",Toast.LENGTH_LONG).show(); } }
2.BaseAdapter
package com.nantian.app; import java.io.IOException; import java.util.Vector; import org.ksoap2.soapEnvelope; import org.ksoap2.serialization.soapObject; import org.ksoap2.serialization.soapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ShowAdapter extends BaseAdapter { private Context c; private LayoutInflater inflater; private TextView test ; Object result = null; SoapObject soapObject = null; Vector<SoapObject> o; /** * 调用WebService */ public ShowAdapter(Context context) { super(); this.c = context; this.inflater = LayoutInflater.from(context); SoapObject request = new SoapObject("http://service.workSituation.wsCommon.zcpt.nantian.com","queryWcbSlcminByNm"); //request.addProperty("ennm","永昌退水闸"); SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelop.bodyOut = request; HttpTransportSE ht = new HttpTransportSE("http://10.64.8.27/CommonService/services/SluiceSituationService?wsdl"); try { ht.call(null,envelop); if(envelop.getResponse()!=null){ o = (Vector<SoapObject>) envelop.getResponse(); //soapObject = (SoapObject) envelop.getResponse(); //result = (Object) soapObject.getProperty("ennm"); } } catch (IOException e) { // Todo Auto-generated catch block e.printstacktrace(); } catch (XmlPullParserException e) { // Todo Auto-generated catch block e.printstacktrace(); } } public ShowAdapter() { super(); } @Override public int getCount() { // Todo Auto-generated method stub return o.size(); } @Override public Object getItem(int position) { // Todo Auto-generated method stub return 0; } @Override public long getItemId(int position) { // Todo Auto-generated method stub return 0; } @Override public View getView(int position,View convertView,ViewGroup parent) { // Todo Auto-generated method stub Model model = new Model(); if(convertView == null){ inflater = LayoutInflater.from(c); convertView = inflater.inflate(R.layout.item,null); model.test = (TextView) convertView.findViewById(R.id.item0); model.ennm = (TextView) convertView.findViewById(R.id.item1); model.ennmcd = (TextView) convertView.findViewById(R.id.item2); model.bldt = (TextView) convertView.findViewById(R.id.item3); }else{ model = (Model) convertView.getTag(); } if(position%2==0){ convertView.setBackgroundColor(Color.BLUE); } model.test.setText("-----"+o.get(position).getProperty("ennm")+"站点基本信息"+"-----"); model.ennm.setText("站名:"+o.get(position).getProperty("ennm")); model.ennmcd.setText("站号:"+o.get(position).getProperty("ennmcd")); model.bldt.setText("其他信息:"+o.get(position).getProperty("gatsiz")); //model.ennm.setText("站名:"+soapObject.getProperty("ennm").toString()); //model.ennmcd.setText("站号:"+soapObject.getProperty("ennmcd").toString()); //model.bldt.setText("统计日期:"+soapObject.getProperty("bldt").toString()); //model.bldt.setText("统计日期:"+"2012-1-1"); //model.ennm.setText("xyz"); convertView.setTag(model); return convertView; } public class Model{ TextView test,ennm,bldt,ennmcd; } }
3.main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示数据" /> <Button android:id="@+id/submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="提交数据" android:layout_gravity="right" /> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
4.item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/item0" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/item1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/item2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/item3" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/item0" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/item1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/item2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/item3" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
今天关于针对WebService使用Service类获取Port类的一个参数问题解释和获取service对象的讲解已经结束,谢谢您的阅读,如果想了解更多关于axis2实现webservice之使用services.xml文件发布WebService、Java调用WebService方法总结(3)--wsimport调用WebService、Jax-ws 开发webService ,并使用spring注入service类中遇到 空指针、Ksoap2+WebService+BaseAdapter 实现android对WebService的调用的相关知识,请在本站搜索。
本文标签: