引入POM依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.5</version>
</dependency>
发送WebService请求工具类
package repalce.your.package;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class WebServiceUtil {
public static String sendWsdl(String wsdlUrl, String namespaceURI, String localPart, Object... params) throws Exception{
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(wsdlUrl);
QName qName = new QName(namespaceURI, localPart);
Object[] objects = client.invoke(qName, params);
String str = (String) objects[0];
return str;
}
public static JSONObject getJsonObject(String wsdlUrl, String namespaceURI, String localPart, Object... params) throws Exception {
String jsonStr = sendWsdl(wsdlUrl, namespaceURI, localPart, params);
return JSONObject.parseObject(jsonStr);
}
public static JSONArray getJsonArray(String wsdlUrl, String namespaceURI, String localPart, Object... params) throws Exception {
String jsonStr = sendWsdl(wsdlUrl, namespaceURI, localPart, params);
return JSONArray.parseArray(jsonStr);
}
}
WebService配置类
package repalce.your.package;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import repalce.your.package.ApiService;
@Configuration
public class WebServiceConfig {
@Autowired
private ApiService apiService;
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), apiService);
endpoint.publish("/ws/api");
return endpoint;
}
}
WebService接口类
package repalce.your.package;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(
name = "ApiService",
targetNamespace = "http://package.your.repalce"
)
public interface ApiService {
@WebMethod
public String HelloWorld(@WebParam String param1, @WebParam String param2);
}
WebService接口实现类
package repalce.your.package;
import java.util.HashMap;
import java.util.Map;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import repalce.your.package.ApiService;
@Component
@WebService(
serviceName = "ApiService",
targetNamespace = "http://package.your.repalce",
endpointInterface = "repalce.your.package.ApiService"
)
public class ApiServiceImpl implements ApiService {
@Override
public String HelloWorld(String param1, String param2) {
return "Hello,World!";
}
}