栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring boot webService使用方法解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot webService使用方法解析

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

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

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

这里使用spring-boot

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

2.创建一个对象.


    
      org.springframework.boot
      spring-boot-starter-web-services
    
 
    
      org.apache.cxf
      cxf-spring-boot-starter-jaxws
      3.2.7
    

创建一个服务端接口

package com.sunzy.mywebservice;


import lombok.Getter;
import lombok.Setter;


@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;


@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 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;

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;


@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可以看到调用成功.

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131632.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号