Springboot开发Webservice服务端和客户端

环境说明

Java JDK 1.8、Spring boot 2.1.6、Apache CXF 3.1.6

POM依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.6</version>
    </dependency>
</dependencies>

服务端

webService接口

使用@WebService声明式注解声明这是一个webService接口,并设置:

  • name:服务名称
  • targetNamespace:命名空间,通常是接口的包名倒序

注解@WebMethod是声明接口方法,可以通过operationName为接口方法设置别名,默认是方法名。

此外他还有一个参数exclude,为true时则忽略该方法作为webService接口方法,默认为false。

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

@WebService(name = "TestService",
            targetNamespace = "http://server.dandelion.com")
public interface TestService {
    @WebMethod(operationName="getData")
    @WebResult(targetNamespace = "http://server.dandelion.com")
    String execute(@WebParam(name = "action") String action, @WebParam(name = "msg") String msg);
}

创建了webService接口TestService,其中方法execute有两个参数分别为action和msg,用注解@WebParam进行声明。

接口实现

创建接口实现类TestServiceImpl实现TestService接口,其中endpointInterface为webService接口的路径。

import javax.jws.WebService;

@WebService(serviceName = "TestService"
        targetNamespace = "http://server.dandelion.com",
        endpointInterface = "com.dandelion.server.TestService")
public class TestServiceImpl implements TestService{

    @Override
    public String execute(String action, String msg) {
        System.out.println(String.format("action: %s", action));
        System.out.println(String.format("msg: %s", msg));
        return "<root><code>1</code><msg>同步成功</msg></root>";
    }
}
注册接口

创建webService的配置类WebServiceConfig,将接口服务注入容器并进行发布。

import com.dandelion.server.TestServiceImpl;
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 javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    @Bean
    public ServletRegistrationBean<CXFServlet> disServlet() {
        ServletRegistrationBean<CXFServlet> servletRegistrationBean = new ServletRegistrationBean<>();
        servletRegistrationBean.setServlet(new CXFServlet());
        servletRegistrationBean.addUrlMappings("/webService/*");
        return servletRegistrationBean;
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint(SpringBus springBus) {
        EndpointImpl endpoint = new EndpointImpl(springBus, new TestServiceImpl());
        endpoint.publish("/testService");
        return endpoint;
    }
}

项目启动效果:

配置完成后启动项目,访问http://localhost:服务端口/webService,如下图所示说明创建webService服务成功。

WSDL文件:访问http://localhost:服务端口/webService/testService?wsdl

SoapUI测试

客户端

JAX动态调用WebService工具类
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class WsClientUtil {

    /**
     * 调用webservice服务
     * @param wsdUrl 服务地址
     * @param operationName 方法名称
     * @param params 参数
     * @return 服务响应结果
     */
    public static String callWebService(String wsdUrl, String operationName, Object... params){
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);
        try {
            Object[] objects = client.invoke(operationName, params);
            return objects[0].toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

API测试 

@RestController
@RequestMapping(value = "/client")
public class TestClientController {

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    @RequestMapping(value = "test")
    public String test(){
        //在获取连接之前 还原上下文
        Thread.currentThread().setContextClassLoader(classLoader);
        // 服务地址
        String url = "http://localhost:9010/webService/testService?wsdl";
        // 方法名称
        String methodName = "getData";
        // 参数1
        String action = "同步用户信息";
        // 参数2
        String msg = "<user><id>10012334223</id><name>蒲公英不是梦</name><sex>1</sex></user>";
        try {
            // 发起请求
            return WsClientUtil.callWebService(url, methodName, action, msg);
        } catch (Exception e) {
            return String.format("webService调用异常:%s", e.getMessage());
        }
    }
}

效果

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供相关的步骤和代码示例。 首先,我们需要在Spring Boot项目中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 接着,我们需要创建一个WebService,可以在Spring Boot项目中创建一个新的类,例如: ```java import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class MyWebService { @WebMethod public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 此时,我们已经创建了一个简单的WebService,其中包含一个`sayHello`方法,用于返回传入的参数加上一句问候语。 然后,我们需要在Spring Boot项目中添加一个配置类,用于配置WebService的相关信息,例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter; @Configuration public class WebServiceConfig { @Bean public SimpleJaxWsServiceExporter simpleJaxWsServiceExporter() { SimpleJaxWsServiceExporter exporter = new SimpleJaxWsServiceExporter(); exporter.setBaseAddress("http://localhost:8080/services/"); return exporter; } } ``` 在上述配置中,我们使用了`SimpleJaxWsServiceExporter`类,它可以自动将`@WebService`注解的类发布为WebService,并且可以使用`setBaseAddress`方法设置WebService的访问地址。 最后,我们可以使用Java客户端来访问我们创建的WebService,例如: ```java import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class MyWebServiceClient { public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:8080/services/MyWebService?wsdl"); QName qname = new QName("http://webservice.springboot.example.com/", "MyWebServiceService"); Service service = Service.create(url, qname); MyWebService myWebService = service.getPort(MyWebService.class); String result = myWebService.sayHello("World"); System.out.println(result); } } ``` 在上述代码中,我们使用了Java标准库中的`javax.xml.ws.Service`类来访问我们创建的WebService,并且使用了`MyWebService`接口来调用`sayHello`方法。 以上就是使用Spring BootWebService搭建WebService服务端及使用Java客户端的简单示例,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值