环境说明
SpringBoot版本:2.5.6
JDK版本:Jdk8_update261
ApacheCXF版本:3.4.4
依赖引入
在pom文件中引入如下依赖。
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.4</version>
</dependency>
接口服务
package com.purvar.webservice;
import com.alibaba.fastjson.JSON;
import com.purvar.controller.agile.ManufactureController;
import com.purvar.domain.ManufactureVO;
import com.purvar.service.IManufactureService;
import com.purvar.vo.Result;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.util.JSONUtil;
import io.swagger.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import javax.jws.WebService;
/**
* PLM公共WebService服务<br>
* @author namelessmyth
* @version 1.0
* @date 2022/12/9/0009
*/
@WebService(serviceName = "PlmWebService", targetNamespace = "http://www.purvar.com")
public class PlmWebService {
@Autowired
private IManufactureService manufactureService;
public String saveManu(@WebParam(name = "input") String input) throws Exception {
Result result = null;
try {
ManufactureVO vo = JSON.parseObject(input, ManufactureVO.class);
result = manufactureService.save(vo);
} catch (Exception e) {
result = new Result(e);
}
return JSON.toJSONString(result);
}
}
服务配置
package com.purvar.webservice;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.Bus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* CXF-Webservice配置类<br>
*
* @author namelessmyth
* @version 1.0
* @date 2022/12/9/0009
*/
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Bean
public Endpoint endpointPlmWebService() {
EndpointImpl endpoint = new EndpointImpl(bus, new PlmWebService());
//WebService服务路径
endpoint.publish("/PlmWebService");
return endpoint;
}
}
如果项目使用了Shiro还需要在shiro中将servies加入地址过滤,否则会被登录拦截器拦截。
filterChainDefinitionMap.put("/services/**", "anon");
然后启动SpringBoot项目。在浏览器中输入WebService的地址:http://localhost:8081/services/PlmWebService?wsdl会出现如下界面
客户端调用
使用SoapUI工具调用WebService服务