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

WebService使用

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

WebService使用

最近工作中总是不可避免的使用WebService来对接功能,经过自己一番摸索,总结出了一些使用方法,做一下记录:

记录了两个SpringBoot版本使用WebService的一些问题和用法,SpringBoot版本如下:

1.5.6.RELEASE(老版) 2.6.0(新版)

第一步:导入依赖


    org.apache.cxf
    cxf-spring-boot-starter-jaxws
    3.2.4

第二步:定义WebService
直接在业务层的Service层进行操作就行了,自己定义或者复用都可以

接口类:

package com.xrj.webtest.service;//包名

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

@WebService(targetNamespace = "http://service.webtest.xrj.com/")//命名空间:包名的反转
public interface DataWebservice {
    @WebMethod//对应wsdl文件里面的operation,外部调用的方法
    @WebResult String receiveMsg(@WebParam(name = "action") String action, @WebParam(name = "message") String message);//@WebResult 接口返回值 @WebParam 外部调用需要携带的参数
}

实现类:

package com.xrj.webtest.service.impl;

import com.xrj.webtest.service.DataWebservice;
import org.springframework.stereotype.Service;

import javax.jws.WebService;

@WebService(serviceName = "DataService",//服务名,自己定义
        targetNamespace = "http://service.webservicedemo.xrj.com/",//同接口类的命名空间
        endpointInterface = "com.xrj.webtest.service.DataWebservice")//接口类相对路径
@Service
public class DataWebserviceImpl implements DataWebservice {
    
    //外部调用webservice接口就会进来执行这个方法了
    @Override
    public String receiveMsg(String action, String message) {
        System.out.println("webservice进来工作了");
        //todo 做你想做的事情
        return "action="+action+","+"message="+message;//返回数据,后面调用接口会返回这个
    }
}

第三步:定义CXF的配置类(新老版本有差异)
使用@Configuration来进行定义一个配置类

package com.xrj.webtest.config;

import com.xrj.webtest.service.impl.DataWebserviceImpl;
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 CxfConfig {


//    @Bean
//    public ServletRegistrationBean dispatcherServlet() {
//        return new ServletRegistrationBean(new CXFServlet(), "/test
    @Bean
    public Endpoint testData() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), new DataWebserviceImpl());
        endpoint.publish("/data");
        return endpoint;
    }

}

第四步:启动服务
这个时候你可能会出现一些问题(如果你用得新版本):
第一个:

Action:

Consider defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.

这个时候看一下第三步,如果你是2.0.6版本以后得SpringBoot就把CXF配置类里的dispatcherServlet()方法删掉,对应得路径就在spring得配置文件中配置就行了

第二个:
在解决上面得问题之后,你可能还会遇到一个hibernate-validator得API不存在的问题(可能,或许和idea缓存有关),即使你没有用这个验证的包,导入依赖把


    org.hibernate
    hibernate-validator
    6.0.18.Final

直接跑起来服务,按照上面配置得路径去查看wsdl文件,将文档保存下来备用,见图:

能看到这个文档,就证明WebService服务已经发布完成了.

第五步:使用Postman测试Webservice接口

这个主要分为两步:

第一步:构造soap+xml请求体
这里可以使用一款软件SoapUI来根据wsdl文件来一键生成soap_xml请求体(百度搜索下载一下)

软件左上角读取你之前保存的wsdl文件,自动生成所有接口的请求体

导入wsdl文件

生成对应的接口的url和soap+xml的请求体

第二部:

根据SoapUI生成的请求体和定义的url,用postman访问

注意的点:必须post请求,请求头Content-Type为text/xml;charset=utf-8

请求体参数:body参数配置raw,类型为xml,贴上模板补上参数

最后一键send发射,获取响应

到这里就Ojbk了,如果你看到这里,希望对你有帮助!

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

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

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