1.新建项目
包目录结构
2.导入pom文件的依赖
<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>
3.写一个发布接口
package org.example.service;
import javax.jws.WebService;
/**
* @program:webservice
* @description:对外发布服务的接口
* @author:ww
* @create:2021-11-17 13:11
*/
@WebService
public interface HelloService {
//对外发布服务的接口
public String hello(String name);
}
4.编写实现类
package org.example.service.serviceImpl;
import org.example.service.HelloService;
/**
* @program:webservice
* @description:具体实现
* @author:ww
* @create:2021-11-17 13:13
*/
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return name+"Welcome to here";
}
}
5.编写发布的入口
package org.example;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.example.service.serviceImpl.HelloServiceImpl;
/**
* @program:webservice
* @description:
* @author:ww
* @create:2021-11-17 13:15
*/
public class Server {
public static void main(String[] args) {
//发布服务的工厂
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
//设置服务的地址
factoryBean.setAddress("http://localhost:8000/hello");
//设置服务类
factoryBean.setServiceBean(new HelloServiceImpl());
//发布服务
factoryBean.create();
System.out.println("发布服务成功,端口8000");
}
}
6.运行结果
7.访问服务
8.访问wsdl说明书