java springcloud中发布webservice 接口

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>
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要准备好以下环境和工具: - JDK 1.8 - Maven - Spring Boot - Spring Web Services 接下来,可以按照以下步骤来编写一个简单的 SOAP Web Service 接口: 1. 创建一个 Spring Boot 项目,并添加 Spring Web Services 的依赖。 ```xml <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>3.0.10.RELEASE</version> </dependency> ``` 2. 定义一个 XML Schema 文件,用于描述接口的数据结构和消息格式。比如,可以创建一个名为 `person.xsd` 的文件,内容如下: ```xml <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/person" xmlns:tns="http://www.example.com/person" elementFormDefault="qualified"> <xs:element name="GetPersonRequest"> <xs:complexType> <xs:sequence> <xs:element name="id" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="GetPersonResponse"> <xs:complexType> <xs:sequence> <xs:element name="person" type="tns:Person"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="Person"> <xs:sequence> <xs:element name="id" type="xs:int"/> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:schema> ``` 3. 创建一个 Java 类,用于处理 SOAP 请求和响应。比如,可以创建一个名为 `PersonEndpoint` 的类,内容如下: ```java @Endpoint public class PersonEndpoint { private static final String NAMESPACE_URI = "http://www.example.com/person"; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetPersonRequest") @ResponsePayload public GetPersonResponse getPerson(@RequestPayload GetPersonRequest request) { Person person = new Person(); person.setId(request.getId()); person.setName("张三"); person.setAge(18); GetPersonResponse response = new GetPersonResponse(); response.setPerson(person); return response; } } ``` 4. 在 Spring Boot 的配置文件,配置 XML Schema 文件和 Java 类的位置。比如,可以在 `application.properties` 文件添加以下配置: ``` spring.ws.servlet.initTransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl spring.ws.servlet.initXsdMappings=/person.xsd spring.ws.servlet.initEndpointMappings=/person=cn.edu.nju.saas.learn.webservice.PersonEndpoint ``` 其,`spring.ws.servlet.initXsdMappings` 指定了 XML Schema 文件的位置,`spring.ws.servlet.initEndpointMappings` 指定了 Java 类的位置。 5. 启动项目,并使用 SOAP 工具测试接口。比如,可以使用 SOAPUI 工具创建一个名为 `GetPerson` 的请求,请求内容如下: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:per="http://www.example.com/person"> <soapenv:Header/> <soapenv:Body> <per:GetPersonRequest> <per:id>123</per:id> </per:GetPersonRequest> </soapenv:Body> </soapenv:Envelope> ``` 发送请求后,会得到以下响应: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:per="http://www.example.com/person"> <soapenv:Header/> <soapenv:Body> <per:GetPersonResponse> <per:person> <per:id>123</per:id> <per:name>张三</per:name> <per:age>18</per:age> </per:person> </per:GetPersonResponse> </soapenv:Body> </soapenv:Envelope> ``` 至此,一个简单的 SOAP Web Service 接口就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值