nomousewch的专栏

通过JDK自带API发布webservice

首先,webservice是通过接口进行发布的

package com.nomouse.webservice;import javax.jws.WebService;@WebServicepublic interface HelloWorld {String sayHi(String text);} 然后是实现

package com.nomouse.webservice;import javax.jws.WebService;@WebService(endpointInterface = "com.nomouse.webservice.HelloWorld")public class HelloWorldImpl implements HelloWorld {public String sayHi(String text) {System.out.println("sayHi called");return "Hello " + text;}} 最后是发布

package com.nomouse.webservice;import javax.xml.ws.Endpoint;public class Test {public static void main(String[] args) {Endpoint.publish(":8080/hello", new HelloWorldImpl());}} 这样,我们就在这个地址:8080/hello 发布了一个webservice,我们通过浏览器访问:8080/hello?wsdl,可以获取到一个xml文件,那么webservice发布就成功了。

java中调用其他系统发布的webservice

以我们上面发布的webservice为例,首先我们要获知webservice发布的地址,既上面的:8080/hello?wsdl,,然后我们用JDK自带的wsimport工具生成webservice客户端代码,在cmd命令行下输入wsimport -keep :8080/hello?wsdl,然后我们可以看到在当前cmd目录下生成一个com目录,把目录连同里边的java文件导入到客户端项目中。

然后我们开始编写客户端代码如下:

package com.nomouse.webservice;public class Client {public static void main(String[] args) {HelloWorldImplService service = new HelloWorldImplService();HelloWorld client = service.getHelloWorldImplPort();String result = client.sayHi("nomouse");System.out.println(result);}} 控制台输出:Hello nomouse,说明我们调用成功。

用cxf +spring发布webservice

用JDK原生方法生成webservice固然简单,但是不好与我们自己的web系统集成,在项目中,我们更多地使用一些成熟的java webservice框架进行webservice发布,在这里我用cxf框架结合spring进行webservice发布,使用maven做项目构建,jetty作为web服务器。

第一步,用pom文件构建项目结构,项目名为webservice

<?xml version="1.0" encoding="UTF-8"?><project xmlns="" xmlns:xsi=""xsi:schemaLocation=" http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.springframework.samples</groupId><artifactId>mvc-basic</artifactId><name>mvc-basic</name><packaging>war</packaging><version>1.0.0-SNAPSHOT</version><properties><org.springframework.version>3.0.5.RELEASE</org.springframework.version><org.slf4j.version>1.6.1</org.slf4j.version></properties><dependencies><!– Spring –><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${org.springframework.version}</version><exclusions><!– Exclude Commons Logging in favor of SLF4j –><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions></dependency><!– Logging –><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${org.slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${org.slf4j.version}</version><scope>runtime</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${org.slf4j.version}</version><scope>runtime</scope></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version><scope>runtime</scope></dependency><!– cxf webservice –><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>2.6.0</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>2.6.0</version></dependency><!– Test –><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.7</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.5</source><target>1.5</target><showWarnings>true</showWarnings></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><configuration><warName>webservice</warName></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>install</id><phase>install</phase><goals><goal>sources</goal></goals></execution></executions></plugin><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId></plugin></plugins></build></project> 第二步,src\main\webapp\WEB-INF下修改web.xml文件,并加入cxf-servlet.xml文件

我们人生中最大的懒惰,就是当我们明知自己拥有作出选择的能力,

nomousewch的专栏

相关文章:

你感兴趣的文章:

标签云: