在开发java WebSerivce的时候,有时候会想到XFire,AXIS.其实也可以用JAVA自带的JAX-WS也可以做到的。
比如,你可以写个简单的例子
package com.java.ws.app;
import javax.jws.WebService;
@WebService
public interface Calculator {
public int add(int a, int b);
public int multi(int a, int b);
}
然后实现这个接口。代码如下所示:
package com.java.ws.app;
import javax.jws.WebService;
@WebService(endpointInterface = "com.java.ws.app.Calculator", serviceName = "CaculatorSemsService", targetNamespace="http://com.test.app/")
public class CalculatorImpl implements Calculator {
public int add(int a, int b) {
return a + b;
}
public int multi(int a, int b) {
return a * b;
}
public int minus(int a, int b) {
return a - b;
}
}
实现完这个接口以后,要开启这个WebService, 代码如下所示:
package com.java.ws.app;
import javax.xml.ws.Endpoint;
public class Server {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8088/calculator",
new CalculatorImpl());
}
在浏览器中输入http://localhost:8088/calculator?wsdl,如果出现XML格式的字串,说明配置正确,截图如下所示:
接下来就可以使用客户端,就可以访问这个webService了,当然,客户端要包含接口申明。跟之前的写法保持一致。
package com.java.ws.app;
import javax.jws.WebService;
@WebService
public interface Calculator {
public int add(int a, int b);
public int multi(int a, int b);
}
书写客户端访问webservice的代码,如下所示:
package com.ws.app;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.java.ws.app.Calculator;
public class TestClient {
public static void main(String[] args) {
URL url = null;
try {
url = new URL("http://localhost:8087/calculator?wsdl");
} catch (MalformedURLException e) {
e.printStackTrace();
}
QName qName = new QName("http://com.test.app/", "CaculatorSemsService");
//根据targetNamespace,serverName来查找服务
Service service = Service.create(url, qName);
Calculator port = service.getPort(Calculator.class);
System.out.println("client22 :\t" + port.add(1, 2));
}
}
但有时,我们觉得书写这个比较麻烦,JAVA提拱了wsimport用于生成webservice访问客户端的代码。
命令如下所示:
根据wsdl生成代码和文件
wsimport -keep http://localhost:8088/calculator?wsdl
这样就可以生成访问客户端的代码,截图如下所示:
接下来,我们只要书写几行代码,即可,如下所示:
package client;
import app.test.com.CaculatorSemsService;
import app.test.com.Calculator;
public class TestClient {
public static void main(String[] args) {
CaculatorSemsService service = new CaculatorSemsService();
Calculator calculator = service.getCalculatorImplPort();
System.out.println(calculator.add(1, 2));
}
}
通过wsgen生成wsdl
如下所示:
package net.oseye;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class Hello {
@WebMethod
public String SayHello(String name){
return String.format("Hello,%s",name);
}
}
然后在当前项目根目录下创建bin, wsdl文件夹在命令行中输入:
wsgen -cp ./bin -r ./wsdl -s ./src ./bin -wsdl net.oseye.Hello
这样会生成一堆文件,截图如下所示:
将wsdl文件夹拷贝到tomcat webapps指定的项目目录下,即可完成和tomcat服务器的整合。
也可以通过XML配置文件,进行整合,代码如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans 2012/04/11 by yeyuelong -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" >
<!-- Import Apache CXF Bean Definition -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- implementor :接口类的实现类 address: 要和注释里面神秘的服务名对应, -->
<jaxws:endpoint
id="semsWebservice"
address="/SemsService"
implementor="cn.ac.iscas.gz.sems.web.webservice.internal.SemsWebserviceImpl" />
<jaxws:endpoint
id="semsCommandsWebservice"
address="/SemsCommandsService"
implementor="cn.ac.iscas.gz.sems.web.webservice.internal.SemsCommandsWebserviceImpl" />
</beans>
记得要引入CXF框架
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
<version>2.4.6</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-activation_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-annotation_1.0_spec</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ws-metadata_2.0_spec</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>6.1.26</version>
</dependency>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.1.0.RELEASE</version>
</dependency> -->
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>stax2-api</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.ws.xmlschema</groupId>
<artifactId>xmlschema-core</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>net.sf.morph</groupId>
<artifactId>morph</artifactId>
<version>1.1.1</version>
</dependency>