使用springboot+jaxb2+CXF快速开发WebServices接口

使用springboot+jaxb2+CXF快速开发WebServices接口

pom文件

  1. xjc包引入
<!--引入cxf --> 
<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.1</version>
</dependency>
<!-- 引入xjc 命令行程序xjc用于编译DTD或Schema生成与XML数据结构对应的JavaBeans类。-->
<dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.3.2</version>
</dependency>
  1. jaxb插件引入
<!-- jaxb插件 这里配置了两个xsd, 代码生成位置在xsd中namespace指定 是包名的反序-->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>schema1</id>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                <!-- 扫描地址-->
                <schemaDirectory>${project.basedir}/src/main/resources/schema</schemaDirectory>
                <!-- 扫描文件 -->
                <schemaFiles>test1.xsd</schemaFiles>
                <!-- 代码输出位置 具体地址= pom 中 outputDirectory + xsd namespace -->
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                <clearOutputDir>false</clearOutputDir>
            </configuration>
        </execution>
        <execution>
            <id>schema2</id>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                <schemaDirectory>${project.basedir}/src/main/resources/schema</schemaDirectory>
                <schemaFiles>test2.xsd</schemaFiles>
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                <clearOutputDir>false</clearOutputDir>
            </configuration>
        </execution>
    </executions>
</plugin>

xsd 编写

  1. 新建文件
    在这里插入图片描述

  2. xsd 内容

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tns="http://www.Lemon.com/webservice/entity/test1"
           targetNamespace="http://www.Lemon.com/webservice/entity/test1"
           elementFormDefault="qualified">

    <xs:element name="getCountryRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="country" type="tns:country"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="country">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="population" type="xs:int"/>
            <xs:element name="capital" type="xs:string"/>
            <xs:element name="currency" type="tns:currency"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="currency">
        <xs:restriction base="xs:string">
            <xs:enumeration value="GBP"/>
            <xs:enumeration value="EUR"/>
            <xs:enumeration value="PLN"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

生成代码

  1. 执行插件 我使用的的是idea 其他maven执行请自行百度
    maven执行

    1. 执行结束 生成的代码
      在这里插入图片描述

编写WebServices接口

  1. 配置cxf服务发布
@WebService(name = "TestService", 
            targetNamespace = "http://www.lemon.com/webservice/entity/test1")
public interface TestService {

    @WebMethod
    @WebResult(name = "Response")
    GetCountryResponse GetCountry(@WebParam(name = "Request") GetCountryRequest request);

}
  1. 配置cxf服务发布
public class TestServiceImpl implements TestService {
    @Override
    public GetCountryResponse GetCountry(GetCountryRequest request) {
        GetCountryResponse response = new GetCountryResponse();
        Country country = new Country();
        country.setName("China");
        response.setCountry(country);
        return response;
    }
}
  1. CXF配置文件
@Configuration
public class CxfConfig {

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

   
    @Bean
    public TestService testService(){
        return new TestServiceImpl();
    }
    /** JAX-WS AppointmentRegistration **/
    @Bean
    public Endpoint endpoint1() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), testService());
        //暴露服务路径
        endpoint.publish("/test1");
        return endpoint;
    }
}
  1. 配置cxf服务发布

    CXF默认的服务路径是"/services/**"。如果想改变它的url。在application.properties中修改

    #默认
    cxf.path=/Services
    
  2. 启动springboot项目,访问服务

    http://localhost:8082/services/test1?wsdl
    在这里插入图片描述

    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://chis.lemon.com/webservice/interfaceservice" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://www.lemon.com/webservice/entity/test1" name="TestServiceImplService" targetNamespace="http://chis.lemon.com/webservice/interfaceservice">
    <wsdl:import location="http://localhost:8082/services/test1?wsdl=TestService.wsdl" namespace="http://www.lemon.com/webservice/entity/test1"> </wsdl:import>
    <wsdl:binding name="TestServiceImplServiceSoapBinding" type="ns1:TestService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="GetCountry">
    <soap:operation soapAction="" style="document"/>
    <wsdl:input name="GetCountry">
    <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="GetCountryResponse">
    <soap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="TestServiceImplService">
    <wsdl:port binding="tns:TestServiceImplServiceSoapBinding" name="ODPatientCardServicePort">
    <soap:address location="http://localhost:8082/services/test1"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>
    
  3. SoapUI测试接口

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值