基于SpringBoot、Cxf实现webservice

创建项目

createproject

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.6</version>
</dependency>

创建服务接口及实现类

IStudentService

package com.jiay.webservice.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(targetNamespace = "http://service.webservice.jiay.com")
public interface IStudentService {
    @WebMethod
    public String getName(String name);
}

StudentServiceImpl

package com.jiay.webservice.service;

import org.springframework.stereotype.Service;

import javax.jws.WebService;

@WebService(serviceName = "studentService",targetNamespace = "http://service.webservice.jiay.com",
        endpointInterface = "com.jiay.webservice.service.IStudentService")
@Service
public class StudentServiceImpl implements IStudentService {
    @Override
    public String getName(String name) {
        return "hi," + name;
    }
}

添加配置类

WebServiceConfig

package com.jiay.webservice.config;

import com.jiay.webservice.service.IStudentService;
import org.apache.cxf.Bus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.cxf.jaxws.EndpointImpl;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    private Bus bus;
    private IStudentService studentService;

    @Autowired
    private void dependenceInject(Bus bus, IStudentService studentService) {
        this.bus = bus;
        this.studentService = studentService;
    }
    @Bean
    public Endpoint endpointStudentService() {
        EndpointImpl endpoint = new EndpointImpl(bus, studentService);
        //接口发布
        endpoint.publish("/studentService");
        return endpoint;
    }

}

启动验证

浏览器内访问验证,出现可用的服务列表表示成功
run
客户端工具验证
WebServiceStudio,webservice开源调试工具,地址
verification

欢迎来访

  • 有问题欢迎留言或加交流qq:825121848
  • 转载请注明出处
  • 请小编喝茶~
Last Updated: 4/16/2022, 11:05:56 AM