java springcloud中发布webservice 接口

发布时间:2024年01月18日

java springcloud中发布webservice 接口

一、在pom文件中添加依赖:

<!--webservice-->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <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.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>stax2-api</artifactId>
            <version>4.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <!-- 这个主要是client访问的,但是问题多多-->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>

二、添加配置类

package com.flow.mongodb.wsconfig;

import com.flow.mongodb.service.WbceshijkService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @author :wcb
 * @date :Created in 2024/1/18
 * 文件说明: </p>
 */
@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;
	//接口调用
    @Autowired
    private WbceshijkService busBuildService;

    @Bean
    public ServletRegistrationBean disServlet() {
    	//发布服务的地址路径
        return new ServletRegistrationBean(new CXFServlet(), "/ResWebservice/services/*");
    }

    @Bean
    public Endpoint buildOrderEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, busBuildService);
        endpoint.publish("/BusBuildService");
        return endpoint;
    }
}

三、新增接口

package com.flow.mongodb.service;


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

/**
 * webservice的接口文件
 * 以下是需要被调用的接口,参数
 */
@WebService(name = "WbceshijkService", targetNamespace = "http://server.webservice.jk.com")
public interface WbceshijkService {
	//jkService方法
    @WebMethod
    String jkService(@WebParam(name = "data") String data,@WebParam(name = "data2") String data2);
    //新增jkService方法
    @WebMethod
    String addNe(@WebParam(name="param") String param);
}

四、接口实现类(空间targetNamespace 和接口需保持一致)

package com.flow.mongodb.service.impl;

import com.flow.mongodb.service.WbceshijkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
@Slf4j
@Component
@WebService( targetNamespace = "http://server.webservice.jk.com",
        name = "WbceshijkService")
public class WbceshijkServiceImpl implements WbceshijkService {
    @Override
    public String jkService(String data, String data2) {
        log.info("======emrService===111===="+data);
        log.info("======emrService==222====="+data2);
        return "操作成功";
    }
    @Override
    public String addNe(String param) {
        log.info("======student===111====\n"+param);
        return "操作成功";
    }
}

五、启动服务,访问接口

由于是在本地启动的,所以启动地址为127.0.0.1,
接口访问地址组成:IP+端口+配置类中写明的路径+?wsdl=

在这里插入图片描述

六、通过postman进行对接口调用以及参数说明

1. 访问的接口地址同上(http://127.0.0.1:10069/ResWebservice/services/BusBuildService?wsdl=)
2. 设置请求头参数:Content-Type:text/xml
3. 请求报文
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://server.webservice.jk.com"> //代码中的目标空间地址
  <soapenv:Body>
    <test:addNe> //addNe 是代码中的方法名称
      <param>//param 为方法中的接收参数的字段名称
      <![CDATA[<?xml version="1.0" encoding="UTF-8"?><Data>
         <Params> //里面是具体的字段内容参数
           <name>张三</crm_order_id>
           <address>云南昆明西山区</address>
           <age>2000</age>
         </Params>
       </Data>]]>
       </param>
    </test:addNe>
  </soapenv:Body>
</soapenv:Envelope>
4.接口调用返回参数
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:addNeResponse xmlns:ns2="http://server.webservice.jk.com"><return>操作成功</return></ns2:addNeResponse></soap:Body></soap:Envelope>
文章来源:https://blog.csdn.net/weixin_47056195/article/details/135668208
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。