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

一到破伤风 2024-07-16 10:03:01 阅读 84

快速入门

服务端

1. 导入依赖

<code> <!--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

启动服务:

<code>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

 

客户端

 

 

 



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。