使用springboot+jaxb2+CXF快速开发WebServices接口
pom文件
- 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>
- 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 编写
-
新建文件
-
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>
生成代码
-
执行插件 我使用的的是idea 其他maven执行请自行百度
- 执行结束 生成的代码
- 执行结束 生成的代码
编写WebServices接口
- 配置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);
}
- 配置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;
}
}
- 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;
}
}
-
配置cxf服务发布
CXF默认的服务路径是"/services/**"。如果想改变它的url。在application.properties中修改
#默认 cxf.path=/Services
-
启动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>
-
SoapUI测试接口