随着webapi的出现,webservice也慢慢被webapi取代,但不排除有些应用任然在使用webservice。
如果您还不了解怎么搭建spring MVC项目,请参考:javaWeb开发实战:spring MVC+MyBatis实现网页登录验证
本章重点讲怎么给spring MVC添加webservice服务
1.引入相关的jar包
<cxf.version>3.3.0</cxf.version>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
2.添加服务接口(MyWebServiceIF.java):本列中定义了两个接口
package com.My.serviceWebService;
import javax.activation.DataHandler;
import javax.jws.WebService;
@WebService
public interface MyWebServiceIF {
public String getUsr(String username);
public String uploadFile(DataHandler handler, String fileName);
}
3.实现接口类:MyWebService.java
package com.My.serviceWebService;
import com.My.dao.USERSDao;
import com.My.entity.USERS;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;
import javax.activation.DataHandler;
import javax.annotation.Resource;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class MyWebService implements MyWebServiceIF {
@Resource
public USERSDao userdao;
@Override
public String getUsr(String username){
List<USERS> ls = userdao.getListBySql("select * from Users where username='"+username+"'");
JSONArray json = JSONArray.fromObject(ls);
return json.toString();
}
@Override
public String uploadFile(DataHandler handler, String fileName){
if (fileName != null && !"".equals(fileName)) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String fpath = "D:/webserviceFile/"+sdf.format(d)+"/"+fileName;
System.out.print(fpath);
File file = new File(fpath);
if(!file.exists()) {
try {
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if (handler != null) {
InputStream is = null;
FileOutputStream fos = null;
try {
is = handler.getInputStream();
fos = new FileOutputStream(file);
byte[] buff = new byte[1024 * 8];
int len = 0;
while ((len = is.read(buff)) > 0) {
fos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
return "fileNotFound";
} catch (Exception e) {
return "upload File failure";
} finally {
try {
if (fos != null) {
fos.flush();
fos.close();
}
if (is != null) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return "file absolute path:" + file.getAbsolutePath();
} else {
return "handler is null";
}
} else {
return "fileName is null";
}
}
}
3.配置Spring-cxf.xml
<?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:jaxws="http://cxf.apache.org/jaxws"
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 resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint
id="reportEntity"
implementor="com.My.serviceWebService.MyWebService"
address="/MyServiceService"></jaxws:endpoint>
</beans>
4.web.xml中添加相关配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/spring-mybatis.xml
classpath:spring/spring-cxf.xml
</param-value>
</context-param>
<!-- CXF servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
4.文件结构如下:

5.启动项目在浏览其中输入web.xml中配置对应的地址可以看到对应服务


6.生成webservice测试客户端
右键根目录->添加框架支持->webserviceClient


如果没有找到,说明之前添加过,把相关代码删掉。然后在文件--》项目结构--》模块,把webserviceClient删了,然后就可以重新添加。
这个提示不用理会

这里把5.中的wsdl地址复制过来,填写生成文件的目录

生成如下文件

这里修改一下HelloWorldClient.java可直接进行测试:
package example;
import webserviceClient.MyWebServiceIF;
import webserviceClient.MyWebServiceService;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import java.io.File;
import java.io.IOException;
public class HelloWorldClient {
public static void main(String[] argv) {
MyWebServiceIF service = new MyWebServiceService().getMyWebServicePort();
//接口1//******************************************************************
String u = service.getUsr("admin");
System.out.print(u);
//接口2//******************************************************************
String path = "D:/webserviceFile/test.txt";
File tempFile = new File(path);
String fileName = tempFile.getName();
//String path = System.getProperty("user.dir") + "\\files\\" + fileName;
//System.out.println(path);
//这样就相当于构造了一个带文件路径的File了
DataHandler handler = new DataHandler(new FileDataSource(path));
byte[] b = new byte[0];
try {
b = new byte[handler.getInputStream().available()];
handler.getInputStream().read(b);
} catch (IOException e) {
e.printStackTrace();
}
String result = service.uploadFile(b, fileName);
System.out.println(result);
}
}

1275

被折叠的 条评论
为什么被折叠?



