SpringBoot结合CXF发布webService服务详细流程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39470733/article/details/86492112

什么都不多说直接撸代码:

1.创建SpringBoot web项目,并引入cxf依赖:

     <properties>
        <start-class>WebServiceApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>
        <!-- CXF webservice -->
        <dependency>
            <groupId>cloud-weatherws</groupId>
            <artifactId>cloud-common-weatherws</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.编写服务类接口 及实现:

接口:

@WebService
public interface GetWeatherService {

    @WebMethod(operationName = "getWeatherByCity")
    @WebResult(name = "String")
    String getWeatherByCity(@WebParam(name = "cityCode") String cityCode, @WebParam(name = "cityName") String cityName);
}


接口实现:

@Service
@WebService(targetNamespace = "http://com.newchinalife.esb/",endpointInterface = "com.newchinalife.esb.webservice.weather.service.GetWeatherService")
public class GetWeatherServiceImpl implements GetWeatherService {


    @Override
    public String getWeatherByCity(String cityCode, String cityName) {

        return "上海:020;天气:多云转晴";
    }
}

3.webservice服务配置:

@Configuration
public class WebServiceConfig {

    @Autowired
    private GetWeatherService getWeatherService;


    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), getWeatherService);
        endpoint.publish("/getWeatherService");
        return endpoint;
    }
}

4.启动项目访问http://localhost:8080/services/getWeatherService?wsdl即可:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://com.newchinalife.esb/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.weather.webservice.esb.newchinalife.com/" name="GetWeatherServiceImplService" targetNamespace="http://com.newchinalife.esb/">
<wsdl:import location="http://localhost:8080/services/getWeatherService?wsdl=GetWeatherService.wsdl" namespace="http://service.weather.webservice.esb.newchinalife.com/"> </wsdl:import>
<wsdl:binding name="GetWeatherServiceImplServiceSoapBinding" type="ns1:GetWeatherService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getWeatherByCity">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getWeatherByCity">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getWeatherByCityResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="GetWeatherServiceImplService">
<wsdl:port binding="tns:GetWeatherServiceImplServiceSoapBinding" name="GetWeatherServiceImplPort">
<soap:address location="http://localhost:8080/services/getWeatherService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

简单实现webservice服务的发布,如有好的建议或者不足之处欢迎入群讨论或留言,谢谢!!

猜你喜欢

转载自blog.csdn.net/qq_39470733/article/details/86492112