SpringBoot整合CXF开发基于soap协议的服务

SpringBoot整合CXF开发基于soap协议的服务,分为服务端和客户端开发

一、服务端开发

1、新建SpringBoot工程,略


2、实体类

package com.example.demo.pojo;

import java.io.Serializable;

public class User implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long userId;

	private String username;

	private String email;

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}

3、服务接口

package com.example.demo.service;

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

import com.example.demo.pojo.User;

@WebService
public interface UserService {

	@WebMethod
	public String getName(@WebParam(name = "userId") Long userId);
	
	@WebMethod
	public User getUser(long userId);
}

4、服务实现类

package com.example.demo.service.impl;

import javax.jws.WebService;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;

@WebService(targetNamespace = "http://service.demo.example.com/", endpointInterface = "com.example.demo.service.UserService")
public class UserServiceImpl implements UserService {

	@Override
	public String getName(Long userId) {
		return "xiaogao" + userId;
	}

	@Override
	public User getUser(long userId) {
		User user = new User();
		user.setUserId(2l);
		user.setUsername("gaoyi");
		user.setEmail("315319976@qq.com");
		return user;
	}

}

5.配置类

package com.example.demo.config;

import javax.xml.ws.Endpoint;

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 com.example.demo.service.UserService;
import com.example.demo.service.impl.UserServiceImpl;

@Configuration
public class CxfConfig {

	@Bean
	public ServletRegistrationBean dispatcherServlet() {
		return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
	}

	@Bean(name = Bus.DEFAULT_BUS_ID)
	public SpringBus springBus() {
		return new SpringBus();
	}
	
	@Bean
	public UserService userService() {
		return new UserServiceImpl();
	}
	
	//此处要注意导入正取的Endpoint、EndpointImpl包
	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
		endpoint.publish("/user");
		return endpoint;	
	}
	
	
	
}
6、运行springboot 工程,并访问   http://localhost:8080/soap/user?wsdl


二、客户端开发

package com.example.demo.client;


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class ClientTest {

	public static void main(String[] args) throws Exception {

		JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
		Client client = clientFactory.createClient("http://localhost:8080/soap/user?wsdl");
		Object[] objects = client.invoke("getUser", 2l);
		System.out.println(objects[0].toString());
		
		
	}

}



  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供相关的帮助。在SpringBoot中,我们可以使用CXF来实现SOAP WebService。下面是一些基本的步骤: 1. 首先,需要在pom.xml文件中添加CXF依赖: ```xml <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.7</version> </dependency> ``` 2. 接下来,我们需要创建一个服务接口。例如: ```java @WebService public interface HelloWorldService { @WebMethod String sayHello(@WebParam(name = "name") String name); } ``` 3. 然后,我们需要创建服务实现类,实现服务接口中的方法。例如: ```java @WebService(endpointInterface = "com.example.demo.HelloWorldService") @Service public class HelloWorldServiceImpl implements HelloWorldService { @Override public String sayHello(String name) { return "Hello " + name + "!"; } } ``` 4. 然后,我们需要在application.properties文件中配置CXF服务。例如: ```properties # CXF properties cxf.path=/soap-api ``` 5. 最后,我们需要在启动类中添加注解@EnableWs和@Bean来配置CXF服务。例如: ```java @SpringBootApplication @EnableWs public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public ServletRegistrationBean<CXFServlet> cxfServlet() { return new ServletRegistrationBean<>(new CXFServlet(), "/soap-api/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), new HelloWorldServiceImpl()); endpoint.publish("/hello"); return endpoint; } } ``` 这样,我们就可以在SpringBoot整合CXF并实现SOAP WebService了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值