1. webservice 的服务端
- 配置类
@Configuration
public class CxfConfig {
@Autowired
CatalogServiceImpl catalogService;
@Autowired
private Bus bus;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, catalogService);
endpoint.publish("/addCatalog");
return endpoint;
}
}
webservice的接口:
/**
* @Auther tjk
* @Date 2022 06 15
**/
@WebService(targetNamespace = "http://service.com")
public interface CatalogService {
/**
* 目录下发
*/
@WebMethod(action = "creatFolder")
public AvicFilePathModel creatFolder(@WebParam(name = "model") AvicFilePathModel model) ;
}
这个action 后面调用的时候会用到
webserivice的实现类:
/**
* @Auther tjk
* @Date 2022 06 14
* 目录下发的webservice
**/
@WebService(serviceName = "CatalogServiceImpl",
targetNamespace = "http://service.com",
endpointInterface = "com.webservice.CatalogService")
@Service
public class CatalogServiceImpl implements CatalogService {
@Value("${avic.file.store.path}\\")
private String path;
@Autowired
private AvicFilePathService avicFilePathService;
//创建文件夹
@WebMethod
@WebResult(name = "creatFolder")
public AvicFilePathModel creatFolder(@WebParam(name = "model") AvicFilePathModel model) {
//判断工业网的数据库中是否存在该目录地址
AvicFilePathModel avicModel = avicFilePathService.queryAvicFilePath(model.getPath());
//不存在
if(avicModel==null){
model.setRecDate(new Date());
model.setSequenceNbr(model.getSequenceNbr().replace(",",""));
AvicFilePathModel withModel = avicFilePathService.createWithModel(model);
//在本地创建目录
File dir = new File(String.format("%s%s%s", path, model.getPath().replace(":", ""), File.separator));
if (!dir.exists()) {
dir.mkdirs();
}
return withModel;
}
return avicModel;
}
}
2. webservice的客户端
HttpClient配置类
/**
* @Auther tjk
* @Date 2022 06 21
**/
@Component
public class HttpClientCallSoapUtil {
static int socketTimeout = 30000;// 请求超时时间
static int connectTimeout = 30000;// 传输超时时间
static Logger logger = Logger.getLogger(HttpClientCallSoapUtil.class);
/**
* 使用SOAP1.2发送消息
*
* @param postUrl
* @param soapXml
* @param soapAction
* @return
*/
public static String doPostSoap1_2(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type",
"application/soap+xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml,
Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
logger.info("response:" + retStr);
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
logger.error("exception in doPostSoap1_2", e);
}
return retStr;
}
}
@Component
public class CatalogServiceImpl {
/**
* 调用目录下发服务
*/
@Autowired
HttpClientCallSoapUtil httpClientCallSoapUtil;
/**
* 调用目录下发
* @param model
* @param ftlFile
* @param postUrl
* @param soapAction
* @throws IOException
*/
public void catalogCreate(AvicFilePathModel model, String ftlFile,String postUrl, String soapAction) throws IOException {
Writer w = null;
try {
//获取xmlTemplate文件夹的当前路径
URL url = Thread.currentThread().getContextClassLoader().getResource("templates");
String path = url.getPath();
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setDirectoryForTemplateLoading(new File(path));
//解决写入到xml文件出现乱码问题
Template template = configuration.getTemplate(ftlFile,"utf-8");
Map<String, Object> responseMap = new HashMap<String, Object>();
responseMap.put("id", model.getId());
responseMap.put("agencyCode", model.getAgencyCode());
responseMap.put("pathName", model.getPathName());
responseMap.put("path", model.getPath());
responseMap.put("pathDesc",model.getPathDesc());
responseMap.put("sequenceNbr",model.getSequenceNbr());
//responseMap.put("recDate",new Date());
responseMap.put("recUserId", model.getRecUserId());
File file = new File("E:\\xml\\"+"xmltemp.xml");
FileOutputStream f = new FileOutputStream(file);
w = new OutputStreamWriter(f,"utf-8");
template.process(responseMap, w);
String s = FreeMarkerTemplateUtils.processTemplateIntoString(template, responseMap);
String orderSoapXml = template.toString();
System.out.println(orderSoapXml);
//采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务
httpClientCallSoapUtil.doPostSoap1_2(postUrl, s, soapAction);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
w.close();
}
}
}
ftl文件
这个ftl文件需要通过saopUI软件自己copy
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.avic.amos.yeejoin.com">
<soapenv:Header/>
<soapenv:Body>
<ser:transferFile>
<!--Optional:-->
<fileName>${fileName}</fileName>
<file>${file}</file>
<path>${path}</path>
</ser:transferFile>
</soapenv:Body>
</soapenv:Envelope>
注:ftl文件需要放到resources/templates目录下
控制类:url也是需要用soapui copy
catalogService.catalogCreate(model,"catalog.ftl","http://127.0.0.1:9000/indust/services/addCatalog","creatFolder");
这个调用的url 也需要自己通过soapui查找