SpringBoot整合WebService 【客户端、服务端】

快速入门

服务端

1. 导入依赖

        <!--webservice start-->
        <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.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.5.6</version>
        </dependency>
        <!--webservice end-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2. 添加配置类

package com.demo1.webservice.conf;

import com.demo1.webservice.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Slf4j
@Configuration
public class WebServiceConfiguration {

    @Autowired
    private UserService userService;

    @Autowired
    private SpringBus springBus;

    /**
     * 发布服务
     *
     * @return
     */
    @Bean
    public Endpoint userServiceEndpoint() {

        System.out.println("服务发布...");

        // 这里指定的端口不能跟应用的端口冲突, 单独指定
        String path = "http://127.0.0.1:9090/xx/user";

        EndpointImpl endpoint = new EndpointImpl(springBus, userService);

        // 发布服务
        endpoint.publish(path);

        System.out.println("服务成功...");
        System.out.printf("在线的wsdl:%s?wsdl%n", path);

        return endpoint;
    }
}

3. 实体类、接口、实现类

package com.demo1.webservice.po;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class User {
    private String username;
    private String password;
}
package com.demo1.webservice.service;

import com.demo1.webservice.po.User;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.List;
 
@WebService(
        name = "UserService", // 暴露服务名称
        targetNamespace = "http://service.webservice.com"// 命名空间,一般是接口的包名倒序
)
public interface UserService {
 
    @WebMethod
    List<User> getList(User user);
}
package com.demo1.webservice.service.impl;

import com.demo1.webservice.po.User;
import com.demo1.webservice.service.UserService;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;
 
@Service
@WebService(serviceName = "UserService", // 与接口中指定的name一致, 都可以不写
        targetNamespace = "http://service.webservice.com", // 与接口中的命名空间一致,一般是接口的包名倒,都可以不用写
        endpointInterface = "com.demo1.webservice.service.UserService" // 接口类全路径
)
public class UserServiceImpl implements UserService {


    @Override
    public List<User> getList(User user) {
        user.setUsername("zhangsan");
        user.setPassword("123");
        
        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        return users;
    }
}

4. 启动服务

整体项目结构:

ff2d981499de47688155557236564436.png

启动服务:

package com.demo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

}

服务成功...
在线的wsdl:http://127.0.0.1:9090/xx/user?wsdl

 

客户端

 

 

 

  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值