来一个HelloWorld,SpringBoot发布WebService可简单啦。
1、搭建项目
2、配置pom.xml
3、建services服务包
4、登陆接口类
5、登陆接口实现类
6、创建CXF配置类
7、Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决
8、访问webservice
9、访问wsdl
步骤 1 搭建项目
请参照这个教程搭建一个SpringBoot项目,注意,项目名字换成webService
步骤 2 配置pom.xml
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version> </dependency>
加上这两个jar包。
步骤 3 建services服务包
步骤 4 登陆接口类
设置一个登陆接口类
package com.webservice.demo.services; import javax.jws.WebService; import java.util.Map; @WebService(name = "LoginService", // 暴露服务名称 targetNamespace = "http://java18.cn" // 命名空间 ) public interface LoginService { Map<String,Object> userLogin(); }
步骤 5 登陆接口实现类
package com.webservice.demo.services.impl; import com.webservice.demo.services.LoginService; import javax.jws.WebService; import java.util.HashMap; import java.util.Map; @WebService(serviceName = "LoginService", // 与接口中指定的name一致 targetNamespace = "http://java18.cn", // 与接口中的命名空间一致 endpointInterface = "com.webservice.demo.services.LoginService"// 接口地址 ) public class LoginServiceImpl implements LoginService { @Override public Map<String, Object> userLogin() { Map<String, Object> resultMap = new HashMap<>(); resultMap.put("errCode",00000); resultMap.put("errMsg",null); return resultMap; } }
步骤 6 创建CXF配置类
package com.webservice.demo.config; import com.webservice.demo.services.LoginService; import com.webservice.demo.services.impl.LoginServiceImpl; 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 dispatcherServlet() { return new ServletRegistrationBean(new CXFServlet(),"/webservice/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public LoginService loginService() { return new LoginServiceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), loginService()); endpoint.publish("/api"); return endpoint; } }
骤 7 Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决
现在直接启动会报错的。
解决方法如下
这个方法名字换一下就好了。
步骤 8 访问webservice
启动项目,访问http://localhost:8080/webservice/api
步骤 9 访问wsdl
http://localhost:8080/webservice/api?wsdl
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://java18.cn" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="LoginService" targetNamespace="http://java18.cn"> <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://java18.cn" elementFormDefault="unqualified" targetNamespace="http://java18.cn" version="1.0"> <xs:element name="userLogin" type="tns:userLogin"/> <xs:element name="userLoginResponse" type="tns:userLoginResponse"/> <xs:complexType name="userLogin"> <xs:sequence/> </xs:complexType> <xs:complexType name="userLoginResponse"> <xs:sequence> <xs:element name="_return"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" minOccurs="0" name="entry"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="key" type="xs:string"/> <xs:element minOccurs="0" name="value" type="xs:anyType"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="userLoginResponse"> <wsdl:part element="tns:userLoginResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="userLogin"> <wsdl:part element="tns:userLogin" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:portType name="LoginService"> <wsdl:operation name="userLogin"> <wsdl:input message="tns:userLogin" name="userLogin"> </wsdl:input> <wsdl:output message="tns:userLoginResponse" name="userLoginResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="LoginServiceSoapBinding" type="tns:LoginService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="userLogin"> <soap:operation soapAction="" style="document"/> <wsdl:input name="userLogin"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="userLoginResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="LoginService"> <wsdl:port binding="tns:LoginServiceSoapBinding" name="LoginServiceImplPort"> <soap:address location="http://localhost:8080/webservice/api"/> </wsdl:port> </wsdl:service> </wsdl:definitions>