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

代码地址

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供相关的步骤和代码示例。 首先,我们需要在Spring Boot项目中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 接着,我们需要创建一个WebService,可以在Spring Boot项目中创建一个新的类,例如: ```java import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class MyWebService { @WebMethod public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 此时,我们已经创建了一个简单的WebService,其中包含一个`sayHello`方法,用于返回传入的参数加上一句问候语。 然后,我们需要在Spring Boot项目中添加一个配置类,用于配置WebService的相关信息,例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter; @Configuration public class WebServiceConfig { @Bean public SimpleJaxWsServiceExporter simpleJaxWsServiceExporter() { SimpleJaxWsServiceExporter exporter = new SimpleJaxWsServiceExporter(); exporter.setBaseAddress("http://localhost:8080/services/"); return exporter; } } ``` 在上述配置中,我们使用了`SimpleJaxWsServiceExporter`类,它可以自动将`@WebService`注解的类发布为WebService,并且可以使用`setBaseAddress`方法设置WebService访问地址。 最后,我们可以使用Java客户端来访问我们创建的WebService,例如: ```java import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class MyWebServiceClient { public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:8080/services/MyWebService?wsdl"); QName qname = new QName("http://webservice.springboot.example.com/", "MyWebServiceService"); Service service = Service.create(url, qname); MyWebService myWebService = service.getPort(MyWebService.class); String result = myWebService.sayHello("World"); System.out.println(result); } } ``` 在上述代码中,我们使用了Java标准库中的`javax.xml.ws.Service`类来访问我们创建的WebService,并且使用了`MyWebService`接口来调用`sayHello`方法。 以上就是使用Spring Boot和WebService搭建WebService服务端及使用Java客户端的简单示例,希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值