前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WebService: SpringBoot集成WebService实践一

WebService: SpringBoot集成WebService实践一

作者头像
Freedom123
发布2024-03-29 13:38:05
1370
发布2024-03-29 13:38:05
举报
文章被收录于专栏:DevOpsDevOps

简介

在springboot-webservice项目中新建3个模块,webservice-server、webservice-client、webservice-common。

webservice-common项目引入项目依赖,webservice-server和webservice-client项目引入webservice-common项目。

一、服务端编码

创建SpringBoot工程。

1. 编写接口和接口实现类

接口上@WebService注解表明这个接口是一个服务接口,targetNamespace属性是服务的命名空间,name是服务的名称,当客户端调用这个服务时,就是通过服务地址,命名空间和服务名称来确定这个服务。

@WebMethod注解表明这个方法是服务方法,operationName属性制定这个服务方法名称,这个名称必须和服务实现类中的服务方法名称一致,否则,客户端调用会找不到这个服务方法。

代码语言:javascript
复制
package chapter15.jaxws.spittr.service.interfaces;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
 
import chapter15.jaxws.spittr.domain.Spitter;
 
@WebService(targetNamespace = "http://service.spittr.jaxws.chapter15/", name = "SpitterService")
public interface SpitterService {
	@WebMethod(operationName="findByUsername2")
  public abstract Spitter findByUsername(String username);
	
}
2. 编写服务实现类:

@WebService注解表明这是一个服务类,serviceName属性设置这个服务类的服务名称,@SOAPBing(style=Style.RPC)这个注解不能少,防止jdk版本问题而导致的异常。@Component让Spring将其装配成一个组件,因为只有被@WebService注解的组件,才会被SimpleJaxWsServiceExporter发现并导出为服务类。@WebMethod(operationName=“findByUsername2”)表明这是服务操作,operationName设置这个操作名称,前面的SpitterService接口中的@WebMethod(operationName=“findByUsername2”)必须和这个操作名称一致。

代码语言:javascript
复制
package chapter15.jaxws.spittr.service;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import chapter15.jaxws.spittr.domain.Spitter;
import chapter15.jaxws.spittr.service.interfaces.SpitterService;
 
@Component
@WebService(serviceName="SpitterService")
//防止jdk版本问题
@SOAPBinding(style=Style.RPC)
public class SpitterServiceEndPoint{	
	@Autowired
	private SpitterService spitterService;
	
	@WebMethod(operationName="findByUsername2")
	public Spitter findByUsername(String username) {
		Spitter st=spitterService.findByUsername(username);
		return st;
	}
 
}
3. 编写配置类

只有被@WebService注解的组件,才会被SimpleJaxWsServiceExporter发现并导出为服务类。setBaseAddress设置发布的服务的地址和端口号,端口号不能已经被占用,否则报错。

代码语言:javascript
复制
package chapter15.jaxws.spittr.config;
 
 
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter;
 
 
@Configuration
@ComponentScan(basePackages={"chapter15.jaxws.spittr"})
public class RootConfig {
	
	 @Bean
	  public SimpleJaxWsServiceExporter jaxWsExporter(){
		  SimpleJaxWsServiceExporter s=new SimpleJaxWsServiceExporter();
		  s.setBaseAddress("http://localhost:8088/");
		  return s;
	  }
}

如果发布成功在地址栏输入http://localhost:8088/SpitterService?wsdl会出现下面结果:表明服务已经发布成功,这是一个xml文档,message节点表示findByUsername2操作输入输出结果和参数类型;portType节点表示服务可用的操作,本例只有一个操作就是findByUsername2;binding元素的transport指明传输协议,这里是http协议operation 指明要暴露给外界调用的操作。use属性指定输入输出的编码方式,这里没有指定编码。Service元素指定了服务名称和服务的访问路径。

二、客户端编写

创建SpringBoot工程。

1. 编写配置类
代码语言:javascript
复制
package chapter15.spittr.config;
 
import java.net.MalformedURLException;
import java.net.URL;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean;
 
import chapter15.jaxws.spittr.service.interfaces.SpitterService;
 
@Configuration
@ComponentScan(basePackages={"chapter15.spittr"})
 
public class RootConfig {
	@Bean
	  public JaxWsPortProxyFactoryBean getSpitterService() throws MalformedURLException{
		  JaxWsPortProxyFactoryBean rmiProxy=new JaxWsPortProxyFactoryBean();
		  URL url=new URL("http://localhost:8088/SpitterService?wsdl");
		  rmiProxy.setWsdlDocumentUrl(url);
	      rmiProxy.setServiceInterface(SpitterService.class);
	      rmiProxy.setServiceName("SpitterService");
	      rmiProxy.setPortName("SpitterServiceEndPointPort");
	      rmiProxy.setNamespaceUri("http://service.spittr.jaxws.chapter15/");
		  return rmiProxy;
	  }
}

我们可以看到,为 JaxWsPortProxyFactoryBean 设置几个属性就可以工作了。 wsdlDocumentUrl 属性标识了远程 Web 服务定义文件的位置。 JaxWsPortProxyFactory bean 将使用这个位置上可用的 WSDL 来为服务创建代理。由 JaxWsPortProxyFactoryBean 所生成的代理实现了 serviceInterface 属性所指定的 SpitterService 接口。剩下的三个属性的值通常可以通过查看服务的 WSDL 来确定,即在上图中在浏览器输入http://localhost:8088/SpitterService?wsdl展示的xml文档。serviceName属性标识远程服务的服务名称,portName属性标识端口,nameSpaceUri标识命名空间。

2. 编写测试类
代码语言:javascript
复制
package chapter15.spittr.test;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import chapter15.jaxws.spittr.domain.Spitter;
import chapter15.jaxws.spittr.service.interfaces.SpitterService;
import chapter15.spittr.config.RootConfig;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=RootConfig.class)
public class JaxwsTest {
 
	@Autowired
	SpitterService spitterService;
	
	@Test
	public void getSpitter(){
		String name="habuma";
		Spitter spitter=spitterService.findByUsername(name);
		System.out.println(spitter.getUsername()+","+spitter.getPassword());
	}
	
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 一、服务端编码
    • 1. 编写接口和接口实现类
      • 2. 编写服务实现类:
        • 3. 编写配置类
        • 二、客户端编写
          • 1. 编写配置类
            • 2. 编写测试类
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档