当前位置: 代码迷 >> Web前端 >> webservice wsgen与 wsimport使用实例
  详细解决方案

webservice wsgen与 wsimport使用实例

热度:704   发布时间:2012-06-26 10:04:14.0
webservice wsgen与 wsimport应用实例

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if !mso]> <object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </object> <style> st1\:*{behavior:url(#ieooui) } </style> <![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

1.????? 一创建个 HelloWorld 的类

package com.tcs.jws;

?

import javax.jws.WebMethod ;

import javax.jws.WebParam;

import javax.jws.WebResult;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

?

@WebService (targetNamespace= "http://www.tcs.com" )

@SOAPBinding (style= SOAPBinding .Style. RPC )

public class HelloWorld {

?? @WebMethod (action= "toSayHello" ,operationName= "toSayHello" ,exclude= false )

?? @WebResult (name= "returnWord" )

?? public String sayHello( @WebParam (name= "userName" ) String userName)

?? {

??? return "Hello: " +userName;

??? ??

?? }

??

?? @WebMethod

?? public int getSum( int i, int j)

?? {

??? ?? return i+j;

?? }

}

?

2.???????? wsgen 生成 WSDL 文件

?

D:\workspace\wsgen -wsimport >wsgen -cp ./bin -r ./wsdl -s ./src -d ./bin -wsdl com.tcs.jws.HelloWorld

?

#-cp 定义 classpath

# -r 生成 bean wsdl 文件的存放目录

# -s 生成发布 Web Service 的源代码文件的存放目录(如果方法有抛出异常,则会生成该异常的描述类源文件)

# -d 生成发布 Web Service 的编译过的二进制类文件的存放目录(该异常的描述类的 class 文件)

?

?

3.???????? 写个 PublishService ,这里因为没有用 web 容器,所以用以下类来发布 wsdl

package com.tcs.jws;

?

import javax.xml.ws.Endpoint;

?

public class Publish Service {

?

??? /**

??? ? * @param args

??? ? */

??? public static void main(String[] args) {

?????? Endpoint.publish ( "http://localhost:8999/pafirc/hws" , new HelloWorld());

?

??? }

?

}

?

4.???????? ie 访问 : http://localhost:8999/pafirc/hws?wsdl 看是否成功

?

5. wsimport 生成各户端代码

D:\workspace\wsgen -wsimport >wsimport -d ./bin -s ./src -p com.tcs.jws.client.ref? http://localhost:8999/pafirc/hws?wsdl

?

#-d 生成客户端执行类的 class 文件的存放目录

#-s 生成客户端执行类的源文件的存放目录

#-p 定义生成类的包名

?

?

生成如下二个类

?

package com.tcs.jws.client.ref;

?

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebResult;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

?

?

/**

? * This class was generated by the JAX - WS RI.

? * JAX - WS RI 2.1.1 in JDK 6

? * Generated source version: 2.1

? *

? */

@WebService (name = "HelloWorld" , targetNamespace = "http://www.tcs.com" )

@SOAPBinding (style = SOAPBinding .Style. RPC )

public interface HelloWorld {

?

?

??? /**

???? *

???? * @param userName

???? * @return

???? *???? returns java.lang.String

???? */

??? @WebMethod (action = "toSayHello" )

??? @WebResult (name = "returnWord" , partName = "returnWord" )

??? public String toSayHello(

??????? @WebParam (name = "userName" , partName = "userName" )

??????? String userName);

?

??? /**

???? *

???? * @param arg1

???? * @param arg0

???? * @return

???? *???? returns int

???? */

??? @WebMethod

??? @WebResult (partName = "return" )

?? ? public int getSum(

??????? @WebParam (name = "arg0" , partName = "arg0" )

??????? int arg0,

??????? @WebParam (name = "arg1" , partName = "arg1" )

??????? int arg1);

?

}

?

?

?

package com.tcs.jws.client.ref;

?

import java.net.MalformedURLException;

import java.net.URL;

import javax.xml.namespace.QName;

import javax.xml.ws.Service;

import javax.xml.ws.WebEndpoint;

import javax.xml.ws.WebServiceClient;

import javax.xml.ws.WebServiceFeature;

?

?

/**

? * This class was generated by the JAX - WS RI.

? * JAX - WS RI 2.1.1 in JDK 6

? * Generated source version: 2.1

? *

? */

@WebServiceClient (name = "HelloWorldService" , targetNamespace = "http://www.tcs.com" , wsdlLocation = "http://localhost:8999/pafirc/hws?wsdl" )

public class HelloWorldService

??? extends Service

{

?

??? private final static URL HELLOWORLDSERVICE_WSDL_LOCATION ;

?

??? static {

??????? URL url = null ;

??????? try {

??????????? url = new URL( "http://localhost:8999/pafirc/hws?wsdl" );

??????? } catch (MalformedURLException e) {

??????????? e.printStackTrace();

??????? }

???? ??? HELLOWORLDSERVICE_WSDL_LOCATION = url;

??? }

?

??? public HelloWorldService(URL wsdlLocation, QName serviceName) {

??????? super (wsdlLocation, serviceName);

??? }

?

??? public HelloWorldService() {

??????? super ( HELLOWORLDSERVICE_WSDL_LOCATION , new QName( "http://www.tcs.com" , "HelloWorldService" ));

??? }

?

??? /**

???? *

???? * @return

???? *???? returns HelloWorld

???? */

??? @WebEndpoint (name = "HelloWorldPort" )

??? public HelloWorld getHelloWorldPort() {

??????? return (HelloWorld) super .getPort( new QName( "http://www.tcs.com" , "HelloWorldPort" ), HelloWorld. class );

??? }

?

??? /**

???? *

???? * @param features

???? *???? A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy .? Supported features not in the <code> features </code> parameter will have their default values.

???? * @return

???? *???? returns HelloWorld

???? */

??? @WebEndpoint (name = "HelloWorldPort" )

??? public HelloWorld getHelloWorldPort(WebServiceFeature... features) {

??????? return (HelloWorld) super .getPort( new QName( "http://www.tcs.com" , "HelloWorldPort" ), HelloWorld. class , features);

??? }

?

}

?

?

5.???????? 写个客户端测试代码 ClientRun

package com.tcs.jws.client.ref;

?

public class ClientRun {

?

??? /**

??? ? * @param args

??? ? */

??? public static void main(String[] args) {

?????? HelloWorldService hws = new HelloWorldService();

?????? HelloWorld hw=hws.getHelloWorldPort();

?????? System. out .println(hw.getSum(8, 1));

?????? System. out .println(hw.toSayHello( "john" ));

?

??? }

?

}

?

  相关解决方案