当前位置: 移动技术网 > IT编程>开发语言>Java > Spring boot webService使用方法解析

Spring boot webService使用方法解析

2020年09月22日  | 移动技术网IT编程  | 我要评论
以前一家公司,项目用到webservice,不过后来没待多久,当时也要弄别的也就没有研究,这次也遇到过这样一个使用场景,需要对接别人的一个人脸识别服务,在什么都没有的情况下,对方只给了一个wsdl的地

以前一家公司,项目用到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://www.lhsxpumps.com/_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可以看到调用成功.

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网