一、service
依赖
<!-- 要进行jaxws 服务开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 内置jetty web服务器 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return name+"hello";
}
}
一定要记得加注解(好像还要和client在同一包下)
@WebService
public interface HelloService {
/**
* 接口方法
*/
public String sayHello(String name);
}
public class Server {
public static void main(String[] args) {
//发布服务工厂
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
//设置webservice服务地址
factoryBean.setAddress("http://localhost:8080/ws/hello");
//设置服务类
factoryBean.setServiceBean(new HelloServiceImpl());
//添加日志输出、输出拦截器、观察soap请求,soap响应
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
//发布服务
factoryBean.create();
System.out.println("发布服务成功,端口8080");
}
}
二、client
代码如下(示例):
<dependencies>
<!-- 要进行jaxws 服务开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 内置jetty web服务器 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
@WebService
public interface HelloService {
public String sayHello(String name);
}
public class Client {
public static void main(String[] args) {
//服务接口访问地址http://localhost:8080/ws/hello
//创建代理工厂
JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();
//设置远程访问服务地址
factoryBean.setAddress("http://localhost:8080/ws/hello");
//对接口生成代理对象
HelloService helloService=factoryBean.create(HelloService.class);
//代理对象
System.out.println(helloService.getClass());
//远程访问服务方法
String content=helloService.sayHello("jet");
System.out.println(content);
}
}