Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use axis to develop webservice

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use axis to develop webservice, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Preparation:

Description: it may be necessary to understand SOAP (simple object access Protocol), WSDL (web Services description language), XML (Extensible markup language), axis (Apache Extensible Interactive system) and other related knowledge.

1. To download the jar package of axis, please go to the official website of apache or download it on Baidu yourself, and then import the jar package. If you are not clear, just import all the jar packages. (I use the version of axis1 because I use the version of axis1 in the project)

2.webservice client generation tool genclient.bat (a brief description of this tool will be given later)

The local environment is not described in the previous introduction, so now list the local environment:

System: XP myeclipse:6.5 tomcat:5.X JDK:1.5

There are three ways to generate webservice, such as XFire, Jax-WS, axis, etc., and there are three ways to call webservi using axis: dynamic calling interface, using dynamic proxy Dynamic Proxy, and using stub generated Stubs from Service WSDL description generated from WSDL. Because I use axis in my work, the way to call webservice uses dynamic calling interface and stub generated Stubs from Service WSDL description generated from WSDL, so this section only introduces these two ways of calling webservice with axis. The follow-up expansion will be discussed as needed.

Start:

* step: server-side development

(1) develop java files

The code is as follows:

Interface file: IBbossGroupInfoService.java

Package com.asiainfo.b2p.soap.interfaces; import java.rmi.RemoteException Public interface IBbossGroupInfoService {/ / query group basic information according to group number / * group number GROUP_NUM group name GROUP_NAME company address CO_ADDR group status GROUP_STATE group account opening time * START_TIME fax FAX enterprise website WEB_URL document type CARD_TYPE document number CARD_CODE * company zip code CO_POSTCODE contact number TEL_NUM legal representative name PERSON_NAME development type DEVELOP_TYPE * area code REGION_ID regionId * @ throws Exception * / public abstract String groupInfo (long groupNo) throws Exception RemoteException / / query the group ordering history public abstract String groupSubHistoryInfo (long groupNo) throws Exception;} according to the group number

Implementation class: IBbossGroupInfoServiceImpl.java

Package com.asiainfo.b2p.soap.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.asiainfo.b2p.soap.interfaces.IBbossGroupInfoService / * @ Product esop_dev * @ FileName IBbossGroupInfoServiceImpl.java * @ History TODO query group basic information, group user information, group ordering history * @ Version 1.0.0 * @ Date Nov 22, 2011 * @ Author pantaipeng * @ Content initial version * / public class IBbossGroupInfoServiceImpl implements IBbossGroupInfoService {private static transient Log log = LogFactory .getLog (IBbossGroupInfoServiceImpl.class) / query group basic information according to group number public String groupInfo (long groupNo) throws Exception, RemoteException {log.info ("> > group number =" + groupNo); / / method body I omitted, for testing I only added a print statement System.out.println ("I am a public method to query group basic information based on group number") String xml= "public_groupInfo"; / / for testing, I also casually wrote a string return xml;} / / query the group ordering history public String groupSubHistoryInfo (long groupNo) throws Exception {log.info ("> > group number =" + groupNo) according to the group number. / / method body I omitted, to test I only added a print sentence System.out.println ("I am a public method, the function is to query the group ordering history according to the group number"); String xml= "public_groupSubHistoryInfo"; / / for testing, I also casually wrote a string return xml;}}.

Description: in order to be closer to the actual development, this is the program running on this machine, the function is to query the relevant information through a number into a string, I deleted the part of the method body, if you need to use this code, directly copy and modify the package name can be used directly.

(2) wsdd file

You only need to add a few lines to the wsdd file (if you already have a wsdd file, you can copy one in the demo of the downloaded apache if you don't have it):

This file is placed under the WEB-INF of the project. The name attribute of the tag is the name of the published webservice, which can be named yourself. The value attribute of * * tags specifies the full path to the implementation class, including the class name. Other places are fine by default.

(3) configure web.xml file

AxisServlet Apache-Axis Servlet org.apache.axis.transport.http.AxisServlet AxisServlet / services/*

Just specify the handling class and mapping. If you configure it like this, and then write webservice later, this file will no longer need to be configured.

Step 2: client development

(1)。 Dynamic call interface

The code is as follows:

The steps for package test.webserviceclient; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.encoding.XMLType; public class Test {/ * the webservice client generated through the genClient.bat file to invoke the webservice service are as follows: * 1. Create service object * 2. Create url object * 3. Create a call object, * 4. Call the webservice method * / public static void test1 () {try {/ / 1. Create a service object, and create org.apache.axis.client.Service service = new org.apache.axis.client.Service (); / / 2 through the class that comes with axis. Create a url object String wsdlUrl = "URL URL url of the http://10.10.146.82:8080/esop_dev/services/GroupInfoService?wsdl";// request service = new URL (wsdlUrl); / / pass in the wsdlUrl address through the constructor of the URL class to create the URL object / / 2. Create the caller object call of the service method, set the property of the call object Call call = (Call) service.createCall (); call.setTargetEndpointAddress (url); / / set the requested URL property String serviceName = "groupInfo"; call.setOperationName (serviceName) for the call object / set the calling method name property call.addParameter ("groupNo", XMLType.XSD_LONG, ParameterMode.IN) for the call object; / / set the parameter name, parameter type, and parameter mode call.setReturnType (XMLType.SOAP_STRING) for the call object; / / set the return value type of the calling method / / 4. Call webservice long groupNo = 2100000014L through the invoke method; String res = (String) call.invoke (new Object [] {groupNo}); / / call the service method System.out.println (res);} catch (MalformedURLException e) {e.printStackTrace ();} catch (ServiceException e) {e.printStackTrace () } catch (RemoteException e) {e.printStackTrace ()}} public static void main (String [] args) {test1 ();}}

The comments in the code are very detailed, so I won't introduce them.

(2)。 Use the stub generated Stubs from Service WSDL description generated from WSDL

* * step: configure genclient.bat file

Right-click "Edit" to open the bat file as follows (for ease of explanation, I'll write the meaning of each line directly at the end):

Set Axis_Lib=E:\ esop\ lib-specify the lib directory of the project set Java_Cmd=java-Djava.ext.dirs=%Axis_Lib%-default set Output_Path=E:\ esop\ src-specify the src directory set Package=com.asiainfo.maks.webservices.pbosswebclient of the project-specify the location of the four generated files,% Java_Cmd% org.apache.axis.wsdl.WSDL2Java-o% OutputPath%-p % Package%-default http://10.10.146.82:8080/esop_dev/services/GroupInfoService?wsdl-specifies the path to the wsdl file

Step 2: generate four client classes with genclient.bat

Double-click the compiled bat file, and the four java classes generated after execution are as follows:

Step 3: write the test class to call webservice

Package test.webserviceclient; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.encoding.XMLType Public class Test {/ * the steps for the webservice client generated through the genClient.bat file to invoke the webservice service are as follows: * the four files created are: * IBbossGroupInfoServiceImpl defines the web service interface, * IBbossGroupInfoServiceImplService defines the method for the user to obtain the web service interface, * the concrete implementation of the IBbossGroupInfoServiceImplServiceLocator interface IBbossGroupInfoServiceImplService * the GroupInfoServiceSoapBindingStub web service client pile interacts with the server through this class. This class implements the IBbossGroupInfoServiceImpl interface * 1. Create service object * 2. Create url object * 3. Create a client object * 4. Call the method * / public static void test1 () {/ / 1 of webservice. Create a service object IBbossGroupInfoServiceImplService service = new IBbossGroupInfoServiceImplServiceLocator (); try {/ / 2. Create a URL object, pass the address of wsdl to the constructor of URL to create String wsdlUrl = "http://10.10.146.82:8080/esop_dev/services/GroupInfoService?wsdl"; URL url = new URL (wsdlUrl); / / 3 create a client object IBbossGroupInfoServiceImpl client = service .getGroupInfoService (url) / / 4 call webservice's method long groupNo = 2100000014L; String res = client.groupInfo (groupNo); System.out.println (res);} catch (MalformedURLException e) {e.printStackTrace ();} catch (ServiceException e) {e.printStackTrace () } catch (RemoteException e) {e.printStackTrace ();}} public static void main (String [] args) {test1 ();}} these are all the contents of the article "how to develop webservice with axis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report