<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.4version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demoname>
<description>测试webservice接口description>
<properties>
<java.version>11java.version>
<cxf.version>3.3.1cxf.version>
<service.web>2.4.4service.web>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-web-servicesartifactId>
<version>${service.web}version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxwsartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-httpartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package com.example.demo.webservice.test;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(name = "TestWebService", // 暴露服务名称
targetNamespace = "http://test.webservice.demo.example.com") // 命名空间,一般是接口的包名倒序
public interface TestWebService {
@WebMethod
public String getWebService(@WebParam(name = "kunnr") String kunnr,
@WebParam(name = "zdata") String zdata,
@WebParam(name = "zisty") String zisty);
}
package com.example.demo.webservice.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
@Service("testWebService")
@WebService(serviceName = "TestWebService", // 与接口中指定的name一致
targetNamespace = "http://test.webservice.demo.example.com", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.example.demo.webservice.test.TestWebService" // 接口地址
)
public class TestWebServiceImpl implements TestWebService{
private Logger logger = LoggerFactory.getLogger(TestWebServiceImpl.class);
@Override
public String getWebService(String kunnr, String zdata, String zisty) {
logger.debug("测试请求参数:{}",kunnr,zdata,zisty);
return "webservice接口实现了,哈哈哈......";
}
}
package com.example.demo.webservice.config;
import com.example.demo.webservice.test.TestWebService;
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 WebServiceConfiguration {
/*
* 注意方法名不能是dispatcherServlet,与默认spring mvc 冲突
*/
@Bean
public ServletRegistrationBean servletTest() {
return new ServletRegistrationBean(new CXFServlet(),"/test/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint getEndpoint(TestWebService testWebService){
EndpointImpl endpoint = new EndpointImpl(springBus(), testWebService);
endpoint.publish("/web");
System.out.println("testWebService接口发布成功 ============= >");
return endpoint;
}
}
package com.xs1h.shoporder.webservice.service.record;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestWebService {
public static void main(String[] args) throws IOException {
//第一步:创建服务地址
URL url = new URL("http://localhost:5454/test/web");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
String soapXML = getEnvelope();
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
if(200 == responseCode){//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
/**
* 打印结果
*/
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
os.close();
}
public static String getEnvelope(){
StringBuilder sb = new StringBuilder();
sb.append("\n" );
sb.append(" ");
sb.append("" );
sb.append("" );
sb.append("777777 ");
sb.append("20210402 ");
sb.append("Y ");
sb.append("");
sb.append("");
sb.append("");
return sb.toString();
}
}
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://test.webservice.demo.example.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TestWebService" targetNamespace="http://test.webservice.demo.example.com">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://test.webservice.demo.example.com" elementFormDefault="unqualified" targetNamespace="http://test.webservice.demo.example.com" version="1.0">
<xs:element name="getWebService" type="tns:getWebService"/>
<xs:element name="getWebServiceResponse" type="tns:getWebServiceResponse"/>
<xs:complexType name="getWebService">
<xs:sequence>
<xs:element minOccurs="0" name="kunnr" type="xs:string"/>
<xs:element minOccurs="0" name="zdata" type="xs:string"/>
<xs:element minOccurs="0" name="zisty" type="xs:string"/>
xs:sequence>
xs:complexType>
<xs:complexType name="getWebServiceResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
xs:sequence>
xs:complexType>
xs:schema>
wsdl:types>
<wsdl:message name="getWebService">
<wsdl:part element="tns:getWebService" name="parameters"> wsdl:part>
wsdl:message>
<wsdl:message name="getWebServiceResponse">
<wsdl:part element="tns:getWebServiceResponse" name="parameters"> wsdl:part>
wsdl:message>
<wsdl:portType name="TestWebService">
<wsdl:operation name="getWebService">
<wsdl:input message="tns:getWebService" name="getWebService"> wsdl:input>
<wsdl:output message="tns:getWebServiceResponse" name="getWebServiceResponse"> wsdl:output>
wsdl:operation>
wsdl:portType>
<wsdl:binding name="TestWebServiceSoapBinding" type="tns:TestWebService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getWebService">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getWebService">
<soap:body use="literal"/>
wsdl:input>
<wsdl:output name="getWebServiceResponse">
...
wsdl:output>
wsdl:operation>
wsdl:binding>
<wsdl:service name="TestWebService">
<wsdl:port binding="tns:TestWebServiceSoapBinding" name="TestWebServiceImplPort">
<soap:address location="http://localhost:5454/test/web"/>
wsdl:port>
wsdl:service>
wsdl:definitions>
1、不知道调用参数xml文件内容是什么。
可以用工具解析出参数xml文件的内容,然后自行往请求xml文件中添加参数,我用的是soapUI工具自动生成的请求参数xml,然后自己在代码中用springbulider拼接在一起>
** 2、调用接口时地址不知道**
你调用的webservice接口的地址是大致组成是【http://ip:port/ServletRegistrationBean中的地址/publish方法中的地址】,注意里面包括项目名称
以上便是我写webservice接口遇到的问题总结,希望对大家有帮助