Spring boot webService使用方法解析

以前一家公司,项目用到webservice,不过后来没待多久,当时也要弄别的也就没有研究,

这次也遇到过这样一个使用场景,需要对接别人的一个人脸识别服务,在什么都没有的情况下,对方只给了一个wsdl的地址过来,全程都靠自己去研究了.

先就webservice 讲下自己的理解把,感觉有点像websockt ,它可以实现一个服务端, 然后在客户端去调用服务端去完成服务端的操作.

这里使用spring-boot

1.先创建spring-boot项目,引入jar包

2.创建一个对象.

<!-- web Services -->
    <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.2.7</version>
    </dependency>

创建一个服务端接口

package com.sunzy.mywebservice;

import lombok.Getter;
import lombok.Setter;

/**auth : szy
 *time : 2020-01-03
 **/
@Getter
@Setter
public class Person {
  private Integer id;
  private String name;
  private String niceName;
  private Integer age;
  private Double height;
}

服务端的实现方法

package com.sunzy.mywebservice.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.sunzy.mywebservice.Person;
import com.sunzy.mywebservice.service.TestApiService;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.List;

/**auth : szy
 *time : 2020-01-03
 **/
@Component
@WebService(name = "testApiService",
    targetNamespace = "http://service.mywebservice.sunzy.com",
    endpointInterface = "com.sunzy.mywebservice.service.TestApiService",
    portName = "10000")
public class TestApiServiceImpl implements TestApiService {
  @Override
  public Person insertPersonInfo(String person) {
    System.out.println("服务端接口到了请求:person="+person);
    List<Person> list = JSONArray.parseArray(person, Person.class);
    //TODO 逻辑处理
    return list.get(0);
  }
}

配置文件,将服务进行开放出去,给外部使用.

到这里已经完成了,运行项目.访问地址:http://localhost:8080/ws/testApiService?wsdl

能输出下面,表示服务端部署成功了.

那么下面是客户端如何去访问

可以新建一个项目,这里采用本项目去调用.用idea 去解析wsdl,生成对应的代码.

选择项目

通过wsdl,生成java代码.

上面填生成的地址,下面试填写包名,重点 这里上面的地址是要有效能访问到的,不然程序是读取不到东西的,更不要说解析了

生成代码完成后,可以看到代码:

调用方法,这里就自己写一个控制器,模仿下客户端去调用.

package com.sunzy.mywebservice.controller;/**
 * @title: Hello
 * @projectName mywebservice
 * @description: TODO
 * @author :szy
 * @date 2020/1/3-15:44
 */

import com.sunzy.mywebservice.Person;
import com.sunzy.mywebservice.config.TestApiServiceImplService;
import com.sunzy.mywebservice.service.TestApiService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.util.Map;

/**auth : szy
 *time : 2020-01-03
 **/
@RestController
@RequestMapping("/adminWebservice")
public class Hello {

  // 获取单位信息
  @GetMapping(value="/sync")
  public String sync(@RequestParam(value="data") String data) throws Exception{

    // 接口地址
           String address = "http://localhost:8080/ws/testApiService?wsdl";
           // 代理工厂
           JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
           // 设置代理地址
           jaxWsProxyFactoryBean.setAddress(address);
           // 设置接口类型
           jaxWsProxyFactoryBean.setServiceClass(TestApiService.class);
           // 创建一个代理接口实现
          TestApiService us = (TestApiService) jaxWsProxyFactoryBean.create();

           // 数据准备
           String userId = "[{\"name\":\"JACK\"},{\"name\":\"TOM\"}]";
           // 调用代理接口的方法调用并返回结果
           Person person = us.insertPersonInfo(userId);
           System.out.println("返回结果:" + person.toString());

    return "index";
  }

  // 动态调用 外部调用
  @GetMapping(value="/dontest")
  public String dontest(@RequestParam(value="data") String data) throws Exception{

    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://127.0.0.1:8080/ws/testApiService?wsdl");

    Object[] objects = new Object[0];

    try {
      // invoke("方法名",参数1,参数2,参数3....);
      // 数据准备
      String userId = "[{\"name\":\"JACK\"},{\"name\":\"TOM\"}]";
      objects = client.invoke("insertPersonInfo", userId);

      System.out.println("返回数据:" + objects[0]);

    } catch (java.lang.Exception e) {
      e.printStackTrace();
    }

    return "index";
  }

  // 动态调用 外部调用(外部模拟客服端调用服务端)
  @GetMapping(value="/dontest2")
  public String dontest2(@RequestParam(value="data") String data) throws Exception{

    //调用服务端
    TestApiServiceImplService serviceImplService = new TestApiServiceImplService();

    com.sunzy.mywebservice.config.TestApiService apiService = serviceImplService.get10000();
    String userId = "[{\"name\":\"小红\"},{\"name\":\"小蓝\"}]";

    com.sunzy.mywebservice.config.Person x = apiService.insertPersonInfo(userId);

    //服务端返回的数据
    System.out.println("返回数据:" + x.toString());

    return "index";
  }
}

通过postman可以看到调用成功.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Spring boot 整合CXF开发web service示例

    前言 说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是一些特有或相对老旧的系统依然使用了传统的soap web service,例如银行.航空公司的机票查询接口等. 目前就遇到了这种情况,需要在系统中查询第三方提供的soap web service接口,也就是说要将它整合进现有的系统当中. spring整合CXF本来十分简单,但是因为使用了Spring boot,不想用以前xml一堆配置的方式,那么能否按照Spring boot的

  • spring boot 开发soap webservice的实现代码

    介绍 spring boot web模块提供了RestController实现restful,第一次看到这个名字的时候以为还有SoapController,很可惜没有,对于soap webservice提供了另外一个模块spring-boot-starter-web-services支持.本文介绍如何在spring boot中开发soap webservice接口,以及接口如何同时支持soap和restful两种协议. soap webservice Web service是一个平台独立的,低耦

  • SpringBoot使用CXF集成WebService的方法

    1.写在前面 WebService 对我来说既熟悉又陌生,已经将近六七年没有看到过他了, 具体的介绍我就不多少了, 想了解的百度百科下说的很详细. 之所以突然研究WebService是接到一个需求要去给 XX 项目做一个适配层,他们原有系统是使用webservice做的,所以-- 那我们就来看看,这一个古老的技术如何和如今最流行的框架SpringBoot进行结合. 2.SpringBoot 集成WebService 2.1 导入依赖 compile('org.springframework.bo

  • springboot整合cxf发布webservice以及调用的方法

    webservice性能不高,但是现在好多公司还是在用,恰好今天在开发的时候对接项目组需要使用到webservice下面来说下简单的案例应用 首先老规矩:引入jar包 <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.11</version> &

  • Spring Boot 实现Restful webservice服务端示例代码

    1.Spring Boot configurations application.yml spring: profiles: active: dev mvc: favicon: enabled: false datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/wit_neptune?createDatabaseIfNotExist=true&useUnicode=true&

  • 详解Spring boot+CXF开发WebService Demo

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的webservice的配置过程,仅供参考. 一.本次开发除了用到spring boot基础jar包外,还用到了cxf相关jar包: <!-- cxf支持 --> <dependency> <groupId>org.apache.cxf</groupId> <

  • Spring boot webService使用方法解析

    以前一家公司,项目用到webservice,不过后来没待多久,当时也要弄别的也就没有研究, 这次也遇到过这样一个使用场景,需要对接别人的一个人脸识别服务,在什么都没有的情况下,对方只给了一个wsdl的地址过来,全程都靠自己去研究了. 先就webservice 讲下自己的理解把,感觉有点像websockt ,它可以实现一个服务端, 然后在客户端去调用服务端去完成服务端的操作. 这里使用spring-boot 1.先创建spring-boot项目,引入jar包 2.创建一个对象. <!-- web

  • Spring Boot webflux使用方法解析

    1.同步阻塞IO模型 当容器中只有三个线程接收请求,当有四个请求过来的时候,就会Block住,得不到及时的响应 2.异步非阻塞式IO模型 Spring Boot webflux是异步非阻塞式IO模型,容器线程将耗时的任务(IO密集型任务)交给work线程来处理 3.webflux应用场景 4.webflux与springmvc异同点 5.webflux使用建议 1).如果当前项目比较稳定,没必要切换.如果要切换最好切换整套技术栈 2).如果只是个人对新技术感兴趣,可以在一些简单小型项目中使用研究

  • Spring Boot启动过程完全解析(二)

    上篇给大家介绍了Spring Boot启动过程完全解析(一),大家可以点击参考下 该说refreshContext(context)了,首先是判断context是否是AbstractApplicationContext派生类的实例,之后调用了强转为AbstractApplicationContext类型并调用它的refresh方法.由于AnnotationConfigEmbeddedWebApplicationContext继承自EmbeddedWebApplicationContext,所以会

  • Spring Boot启动过程完全解析(一)

    之前在排查一个线上问题时,不得不仔细跑了很多遍Spring Boot的代码,于是整理一下,我用的是1.4.3.RELEASE. 首先,普通的入口,这没什么好说的,我就随便贴贴代码了: SpringApplication.run(Application.class, args); --> public static ConfigurableApplicationContext run(Object source, String... args) { return run(new Object[]

  • Spring Boot启动过程全面解析(三)

    我已经很精简了,两篇(Spring Boot启动过程(一).spring Boot启动过程(二))依然没写完,接着来. refreshContext之后的方法是afterRefresh,这名字起的真...好.afterRefresh方法内只调用了callRunners一个方法,这个方法从上下文中获取了所有的ApplicationRunner和CommandLineRunner接口的实现类,并执行这些实现类的run方法.例如Spring Batch的JobLauncherCommandLineRu

  • spring boot aop 记录方法执行时间代码示例

    本文研究的主要是spring boot aop 记录方法执行时间的实现代码,具体如下. 为了性能调优,需要先统计出来每个方法的执行时间,直接在方法前后log输出太麻烦,可以用AOP来加入时间统计 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency&

  • spring boot 全局异常处理方法汇总

    这篇文章主要介绍了spring boot 全局异常处理方法汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 import cn.sisyphe.framework.web.exception.DataException; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.co

  • Maven项目改为spring boot项目的方法图解

    目录树 •新建Maven项目及步骤 •修改方法 •启动测试 新建Maven项目及步骤 我这里是从创建开始讲,使用的工具是Idea2017版本.如果是已经创建了Maven,想改为spring boot项目的请直接跳到[修改方法] 1.点击右上角的File,出来的列表选择New Object: 2.选择Maven,勾选Create from archetype,选择quickstart 3.输入GroupId与ArtifactId,Version版本号自己看着怎么顺眼怎么改:其中GroupId为包名

  • Spring Boot 整合 Druid过程解析

    这篇文章主要介绍了Spring Boot 整合 Druid过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 概述 Druid 是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和 SQL 解析器组成.该项目主要是为了扩展 JDBC 的一些限制,可以让程序员实现一些特殊的需求,比如向密钥服务请求凭证.统计 SQL 信息.SQL 性能收集.SQL 注入检查.SQL 翻译等,程序员可以通过定制来实现自己需要的功能. Druid 是

  • Spring boot整合log4j2过程解析

    这篇文章主要介绍了Spring boot整合log4j2过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 以前整合过log4j2,但是今天再次整合发现都忘记了,而且也没有记下来 1.pom.xml中 (1)把spring-boot-starter-web包下面的spring-boot-starter-logging排除 <dependency> <groupId>org.springframework.boot</gr

随机推荐