SpringBoot集成WebService服务端及postman访问测试

本文介绍如何使用Apache CXF搭建WebService服务,并通过Spring Boot进行配置。详细展示了pom文件中的依赖配置、配置类的编写方法以及服务接口与实现类的设计。最后通过Postman进行了访问测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码地址

https://gitee.com/chrisfzh/dailytest

代码结构

代码及说明

 pom文件中添加的依赖项

        <!--WebServcie-->
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.4.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.4.4</version>
        </dependency>

配置类

package com.chrisf.webservice.config;

import com.chrisf.webservice.service.DemoService;
import com.chrisf.webservice.service.impl.DemoServiceImpl;
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 {

//此处的方法名注意不能叫dispatcherServlet(),此处加个1是为了提醒一下,这个方法名与框架的方法名重复了
    @Bean
    public ServletRegistrationBean dispatcherServlet1(){
        return new ServletRegistrationBean(new CXFServlet(), "/demo/*");
    }

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

    @Bean
    public DemoService demoService(){
        return new DemoServiceImpl();
    }


//第一个接口
    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
        endpoint.publish("/api");
        return endpoint;
    }

//第二个接口
    @Bean
    public Endpoint endpoint2(){
        EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
        endpoint.publish("/api2");
        return endpoint;
    }
}

此处需要注意的就是上面注释中写的,不能用 dispatcherServlet()这个方法名,会跟框架的方法重名,启动会报如下错误:

Parameter 0 of method errorPageCustomizer in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.

 服务接口

package com.chrisf.webservice.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(name = "DemoService",targetNamespace = "http://service.webservice.chrisf.com")
public interface DemoService {


    public String sayHello(@WebParam(name = "user", //访问接口时的参数标签名
                    targetNamespace = "http://service.webservice.chrisf.com")
                     String user);
}

实现类

package com.chrisf.webservice.service.impl;

import com.chrisf.webservice.service.DemoService;

import javax.jws.WebService;
import java.util.Date;

@WebService(serviceName = "DemoService",targetNamespace = "http://service.webservice.chrisf.com",endpointInterface = "com.chrisf.webservice.service.DemoService")
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String user) {
        return user + ", 现在时间:" + "(" + new Date() + ")";
    }
}

postman访问测试

 地址:http://localhost:8080/demo/api?wsdl

请求方式:post

请求头:Context-Type:text/html

请求文本:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <sayHello xmlns="http://service.webservice.chrisf.com">
      <user>
        张三
      </user>
    </sayHello>
  </soap:Body>
</soap:Envelope>

 注意:user对应@WebParam中的name属性,xmlns对应的是targetNamespace属性,sayHello对应的是方法名

 

可以看到,已经返回了我们想要的结果,同时也可以测试一下第二个接口,地址为http://localhost:8080/demo/api2?wsdl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值