spring-cloud 集成webService接口,造成其他controller接口无法访问

在Spring Boot应用中,集成WebService后,通过CxfConfig配置并验证了接口正常工作,但其他Controller接口出现无法访问的问题。原因是Spring Boot默认的dispatcherServlet未被注册,可能因为手动配置ServletRegistrationBean导致。解决方法是需要在启动类中重新配置一个dispatcherServlet,确保Webservice与其他Controller接口共存。
摘要由CSDN通过智能技术生成

pom导入cxf

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.4</version>
</dependency>

 

1.新建webservice类

@WebService(name = "LeakageService", // 暴露服务名称
        targetNamespace = "http://webService.webchat.huaxin.com"// 命名空间,一般是接口的包名倒序
)
public interface LeakageService {
    String doSave(String id,String receiveCode);
}

2.新建webservice业务代码实现

@WebService(
        targetNamespace = "http://webService.webchat.huaxin.com", //wsdl命名空间
        serviceName = "LeakageService",                 //portType名称 客户端生成代码时 为接口名称
        endpointInterface = "com.huaxin.webchat.webService.LeakageService")//指定发布
public class LeakageServiceImpl implements LeakageService {
 public String doSave(String id, String receiveCode)
 {
   ....//具体业务代码 省略
 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先,需要准备好以下环境和工具: - JDK 1.8 - Maven - Spring Boot - Spring Web Services 接下来,可以按照以下步骤来编写一个简单的 SOAP Web Service 接口: 1. 创建一个 Spring Boot 项目,并添加 Spring Web Services 的依赖。 ```xml <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>3.0.10.RELEASE</version> </dependency> ``` 2. 定义一个 XML Schema 文件,用于描述接口的数据结构和消息格式。比如,可以创建一个名为 `person.xsd` 的文件,内容如下: ```xml <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/person" xmlns:tns="http://www.example.com/person" elementFormDefault="qualified"> <xs:element name="GetPersonRequest"> <xs:complexType> <xs:sequence> <xs:element name="id" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="GetPersonResponse"> <xs:complexType> <xs:sequence> <xs:element name="person" type="tns:Person"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="Person"> <xs:sequence> <xs:element name="id" type="xs:int"/> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:schema> ``` 3. 创建一个 Java 类,用于处理 SOAP 请求和响应。比如,可以创建一个名为 `PersonEndpoint` 的类,内容如下: ```java @Endpoint public class PersonEndpoint { private static final String NAMESPACE_URI = "http://www.example.com/person"; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetPersonRequest") @ResponsePayload public GetPersonResponse getPerson(@RequestPayload GetPersonRequest request) { Person person = new Person(); person.setId(request.getId()); person.setName("张三"); person.setAge(18); GetPersonResponse response = new GetPersonResponse(); response.setPerson(person); return response; } } ``` 4. 在 Spring Boot 的配置文件中,配置 XML Schema 文件和 Java 类的位置。比如,可以在 `application.properties` 文件中添加以下配置: ``` spring.ws.servlet.initTransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl spring.ws.servlet.initXsdMappings=/person.xsd spring.ws.servlet.initEndpointMappings=/person=cn.edu.nju.saas.learn.webservice.PersonEndpoint ``` 其中,`spring.ws.servlet.initXsdMappings` 指定了 XML Schema 文件的位置,`spring.ws.servlet.initEndpointMappings` 指定了 Java 类的位置。 5. 启动项目,并使用 SOAP 工具测试接口。比如,可以使用 SOAPUI 工具创建一个名为 `GetPerson` 的请求,请求内容如下: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:per="http://www.example.com/person"> <soapenv:Header/> <soapenv:Body> <per:GetPersonRequest> <per:id>123</per:id> </per:GetPersonRequest> </soapenv:Body> </soapenv:Envelope> ``` 发送请求后,会得到以下响应: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:per="http://www.example.com/person"> <soapenv:Header/> <soapenv:Body> <per:GetPersonResponse> <per:person> <per:id>123</per:id> <per:name>张三</per:name> <per:age>18</per:age> </per:person> </per:GetPersonResponse> </soapenv:Body> </soapenv:Envelope> ``` 至此,一个简单的 SOAP Web Service 接口就完成了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值