发布webService服务并使用soap UI测试
发布简单的webService服务
- 新建一个Maven项目
- pom引入 webService需要的依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.9</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.9</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.9</version>
</dependency>
- 定义一个接口,并使用@WebService 注解,自定义当前接口的targetNamespace。定义自己需要对外提供的方法,并在方法的请求参数中,使用@WebParam注解对参数进行绑定,确保后续调用方生成的代码参数与该参数相同;
@WebService(endpointInterface = "com.ds.webServiceDemo.UserService",targetNamespace="http://cems.ds/userService")
public interface UserService {
/**
* 用户登录
* @param userAccount 账号
* @param password 密码
* @return
*/
public Result login(@WebParam(name = "userAccount")String userAccount, @WebParam(name = "password")String password);
/**
* 获取用户详细信息
* @param userId 用户ID
* @return
*/
public Result getUserDetalis(@WebParam(name = "userId")String userId);
}
- 定义一个实现类,实现接口,并在实现类上使用@WebService和@BindingType注解,@WebService注解需要指定endpointInterface,包含接口的所有包;BindingType
@WebService
public class UserServiceImpl implements UserService {
@WebMethod
@Override
public Result login(String userAccount, String password) {
/**
* 进行业务处理
*/
return null;
}
@WebMethod
@Override
public Result getUserDetalis(String userId) {
/**
* 进行业务处理
*/
return null;
}
}
- 定义一个入口类,使用main方法发布该webService接口;
public class ServiceMain {
public static void main( String[] args ){
System.out.println("webService服务正在启动");
String serverIp = "127.0.0.1";//使用时,ip可以通过配置文件获取
String serverPort = "8080";//使用时,端口可以通过配置文件获取
String userServiceUrl = "http://"+serverIp+":"+serverPort+"/userService";
Endpoint.publish(userServiceUrl, new UserServiceImpl());
System.out.println("userService发布地址:" + userServiceUrl);
System.out.println("webService服务启动成功");
}
}
- 使用浏览器访问发布的webService地址,http://127.0.0.1:8080/userService?wsdl,访问效果如下所示;
同时发布多个webService接口
- 与发布简单的webService服务步骤相同,再创建一个接口
@WebService(endpointInterface = "com.ds.webServiceDemo.FileService",targetNamespace="http://cems.ds/fileService")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface FileService {
/**
* 上传文件
* @param fileName 文件名
* @param file 文件信息,二进制文件
* @return
*/
public Result uploadFile(@WebParam(name = "fileName")String fileName, @WebParam(name = "file")DataHandler file);
/**
* 下载文件
* @param filePath 需要下载的文件路径,也可以是文件标识
* @return
*/
public DataHandler downloadFile(@WebParam(name = "filePath")String filePath)throws FileNotFoundException ;
}
- 定义一个实现类,实现FileService
@WebService
public class FileServiceImpl implements FileService {
@WebMethod
@Override
public Result uploadFile(String fileName, DataHandler handler) {
Result result = new Result();
//就可以将文件存放到本地了
File file = new File("文件存放路径" + fileName);
try {
FileUtils.copyInputStreamToFile(handler.getDataSource().getInputStream(), file);
result.setFlag(0);
result.setData("文件上传成功");
} catch (IOException e) {
e.printStackTrace();
result.setFlag(-1);
result.setData("文件上传出现异常");
}
return result;
}
@WebMethod
@Override
public DataHandler downloadFile(String filePath) throws FileNotFoundException {
File downloadFile = new File(filePath);
if (!downloadFile.exists()) {
System.out.println("需要下载的文件不存在");
throw new FileNotFoundException(filePath + " does not exist");
}
DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));
return dataHandler;
}
}
- 使用入口类进行发布
public class ServiceMain {
public static void main( String[] args ){
System.out.println("webService服务正在启动");
String serverIp = "127.0.0.1";
String serverPort = "8080";
String userServiceUrl = "http://"+serverIp+":"+serverPort+"/userService";
String fileServiceUrl = "http://"+serverIp+":"+serverPort+"/fileService";
Endpoint.publish(userServiceUrl, new UserServiceImpl());
System.out.println("userService发布地址:" + userServiceUrl);
Endpoint.publish(fileServiceUrl, new FileServiceImpl());
System.out.println("fileService发布地址:" + fileServiceUrl);
System.out.println("webService服务启动成功");
}
}
- 使用浏览器访问发布的webService地址,http://127.0.0.1:8080/fileService?wsdl,访问效果如下所示;
使用SoapUI测试webService接口
下载SoapUI并安装
可以从官网下载,也可以在我网盘中进行下载,安装使用时,电脑需要安装jdk
链接:https://pan.baidu.com/s/19bh1UNVH4Tfd7fDg8M83bA
提取码:jz63
打开WSDL文件
打开SoapUI后,在Projects上鼠标右击,选择New SOAP Project
在Initial WSDL中输入发布的webService接口的wsdl文件访问URL,然后点击OK按钮
测试文件上传
在fileName标签中输入上传的文件名,在file中,鼠标右击,选择Insert file as Base64,然后选择本地文件,点击打开即可完成文件的选择,点击Submit request to specified endpoint URL进行测试