当前位置 博文首页 > iloki的博客:Webservice 传输json、文件上传下载
1.所需jar
implementation group: 'org.apache.cxf', name: 'cxf-rt-frontend-jaxws', version: '3.3.4'
implementation group: 'org.apache.cxf', name: 'cxf-rt-transports-http', version: '3.3.4'
implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.75'
implementation 'cn.hutool:hutool-all:5.6.5'
2.工程结构
3.cxf配置类
package com.example.webservice.demo.config;
import com.example.webservice.demo.service.IWebService;
import com.example.webservice.demo.service.impl.IWebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class CxfConfig {
@Bean
public ServletRegistrationBean createDispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/demo/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public IWebService demoService() {
return new IWebServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api");
return endpoint;
}
}
4.Util类
package com.example.webservice.util;
import cn.hutool.log.StaticLog;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;
@Component
public class Util {
private static String serverPort ;
private static final String URL = "/demo/api?wsdl";
@Value("${server.port}")
private String port;
@PostConstruct
public void setServerPort(){
this.serverPort= port;
StaticLog.info("port:"+this.serverPort);
}
/**
* 获取类路径
* @param className 类名
* @return
*/
public static String getClassPath(String className) {
String classUrl = null;
try {
Properties properties = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = Util.class.getClassLoader().getResourceAsStream("config/config.properties");
// 使用properties对象加载输入流
properties.load(in);
//获取key对应的value值
classUrl = properties.getProperty(className);
} catch (Exception e) {
e.printStackTrace();
}
return classUrl;
}
/**
* 通过反射调用方法
* @param className
* @param methodName
* @param params
* @return
* @throws Exception
*/
public static Object invokeMethod(String className, String methodName, Map params) {
Object object = null;
//根据类名获取类路径
try {
String classUrl = Util.getClassPath(className);
Class<?> ownerClass = Class.forName(classUrl);
Object owner = ownerClass.newInstance();
Method method = ownerClass.getMethod(methodName, Map.class);
object = method.invoke(owner, params);
}catch (Exception e){
e.printStackTrace();
}
return object;
}
/**
* 创建动态客户端
* @return
*/
public static Client getClient(){
//创建动态客户端
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
String url = Util.getUrl()+URL;
StaticLog.info(url);
Client client = factory.createClient(url);
// 需要密码的情况需要加上用户名和密码
//client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(2000); //连接超时
httpClientPolicy.setAllowChunking(false); //取消块编码
httpClientPolicy.setReceiveTimeout(120000); //响应超时
conduit.setClient(httpClientPolicy);
//client.getOutInterceptors().addAll(interceptors);//设置拦截器
return client;
}
/**
* 获取当前服务的ip和端口
* @return
*/
public static String getUrl() {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "http://"+address.getHostAddress() +":"+Util.serverPort;
}
}
5.接口类
package com.example.webservice.demo.service;
import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.ws.soap.MTOM;
import java.io.FileNotFoundException;
import java.io.IOException;
@WebService(name = "IWebService", targetNamespace = "http://service.demo.webservice.example.com")
@MTOM
public interface IWebService {
/**
* 普通字符串接口
*
* @param user
* @return
*/
@WebMethod
String sayHello(@WebParam(name = "user") String user);
/**
* json数据传输
*
* @param className
* @param methodName
* @param jsonData
* @return
*/
@WebMethod
String sendJsonData(@WebParam(name = "className") String className, @WebParam(name =