服务端(java 项目),不需要引入jar。
// 服务端启动服务 @WebService // 注解
public class HelloService {
public String sayHello(String name){
System.out.println("服务端say hello");
return "hello"+name;
}
public static void main(String[] args) { // 启动服务
String address="http://172.27.109.133:8081/hello"; // ip为本机ip 打开cmd 输入ipconfig 找到ipv4即可
Object implementor=new HelloService();
Endpoint.publish(address, implementor);
}
}
CXF是apache旗下的开源框架,由Celtix + XFire这两门经典的框架合成,是一套非常流行的web service框架。
注意:CXF2.X版本和Spring4.X不兼容,要用CXF3.X
编写接口
@WebService
public interface HelloWorldInterface {
String sayHello(String text);
}
编写实现类
//@WebService在实现类的注解让CXF知道WSDL创建时所使用的接口。
@WebService(endpointInterface = "webservice.service.HelloWorldInterface")
public class ServerToJava implements HelloWorldInterface {
public String sayHello(String text) {
return "hello" + text;
}
}
导入项目jar Maven:
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<cxf.version>3.2.0</cxf.version> </properties>
<dependencies>
<!--spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency> <
groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency> <
groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency> <!--web service 以下都是cxf必备的 --> <!--org.apache.cxf.transport.servlet.CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency> <!--不加这个包会报错Unable to locate spring NamespaceHandler for XML schema namespace [http://cxf.apache.org/jaxws] -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency> <!--java实现webservice,不部署到tomcat,需要jetty包支持 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.8</version>
</dependency>
</dependencies>
配置web.xml,因为CXF是基于Servlet所以要配置对应的Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>webservice_server</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-service.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
创建Spring配置文件 ,
http://localhost:8080/webservice_server/webservice/web-publish?wsdl
规则为:主机+项目+CXFServlet的访问路径+address,出现对应的wsdl
页面即部署成功。当自己有java的webservice的服务端,即拥有接口的时候,可以直接通过接口和地址进行调用
public class ClientForCXF {
public static MyWebService getInterFace() {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(MyWebService.class);
factoryBean.setAddress("http://localhost:8080/server/web-publish");
return (MyWebService) factoryBean.create();
}
public static void main(String[] args) {
MyWebService myWebService = getInterFace();
System.out.println("client: " + myWebService.add(1, 3));
}
}
public static void main(String[] args)
ServerToJavaService javaService = new ServerToJavaService();
HelloWorldInterface c = javaService.getServerToJavaPort();
String a = c.sayHello("s"); S
ystem.out.println(a);
}
编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="crmClient"
serviceClass="webservice_client.HelloWorldInterface" <!-- 对应的接口-->
address="http://localhost:8080/webservice_server/webservice/web-publish"/> <!-- 地址 -->
</beans>
最后和使用平常的bean一样使用就可以
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-service.xml");
HelloWorldInterface c = (HelloWorldInterface) applicationContext.getBean("crmClient");
String str = c.sayHello("终于等到你");
System.out.println(str);
类似的框架还有Alibaba的开源项目Dubbo ,这是入门介绍:http://www.cnblogs.com/liyuhui-Z/p/7799615.html