WebService: SpringBoot集成WebService实践一

WebService: SpringBoot集成WebService实践一

简介在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代码运行次数:0运行复制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代码运行次数:0运行复制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代码运行次数:0运行复制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代码运行次数:0运行复制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代码运行次数:0运行复制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());

}

}

相关推荐

什么手游装备外形炫酷 2025好玩的装备炫酷游戏推荐
必发365手机版下载

什么手游装备外形炫酷 2025好玩的装备炫酷游戏推荐

📅 09-17 ⭐ 2468
汉字:「拚」 字形演变 字源演变
365bet足球即时比分网

汉字:「拚」 字形演变 字源演变

📅 07-04 ⭐ 8185
itunes正在验证iphone恢复需要多久
365bet足球即时比分网

itunes正在验证iphone恢复需要多久

📅 07-16 ⭐ 660
流沙蝮虫攻略教程
365bet足球即时比分网

流沙蝮虫攻略教程

📅 08-27 ⭐ 2113
蓝牙核心规范6.1正式发布,隐私性和能效实现新提升
必发365手机版下载

蓝牙核心规范6.1正式发布,隐私性和能效实现新提升

📅 09-01 ⭐ 6855
大众点评推一键投诉,告别好评换饮料、差评骚扰时代
推荐阅读 ❤️