springboot中简单创建webservice服务

添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

添加配置类

不添加配置类也可以在springboot的启动类了里面发布服务,但是那样的话就不是ioc托管的了,在服务里面就不能注入其他IOC容器里面的对象

ip 用本机IP,端口不能跟当前项目springboot配置文件里面的端口一样

package com.gt.SPYZT.configuration;

import com.gt.SPYZT.webservice.PlaceService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.xml.ws.Endpoint;

/**
 * @author vhukze
 * @date 2022/4/12 14:57
 */
@Configuration
public class WebServiceConfig {

    @Resource
    private PlaceService placeService;

    @Value("${webs.ip-port}")
    private String webs;

    @Bean
    public Endpoint endpoint() {
        String url = "http://" + webs + "/PlaceService";
        System.out.println("服务发布//");
        return Endpoint.publish(url, placeService);
    }
}

创建服务接口

package com.gt.SPYZT.webservice;

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

@WebService
public interface PlaceService {

    @WebMethod
    String queryPlace(@WebParam(targetNamespace = "http://impl.webservice.SPYZT.gt.com/", name = "token") String token,
                      @WebParam(targetNamespace = "http://impl.webservice.SPYZT.gt.com/", name = "name") String name);
}

接口实现类

package com.gt.SPYZT.webservice.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gt.SPYZT.entity.po.slave1.TbSpyztServiceinfo;
import com.gt.SPYZT.http.PlaceHttp;
import com.gt.SPYZT.mapper.slave1.TbSpyztServiceinfoMapper;
import com.gt.SPYZT.service.TokenService;
import com.gt.SPYZT.webservice.PlaceService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author vhukze
 * @date 2022/4/12 13:53
 */
@WebService
@Component
public class PlaceServiceImpl implements PlaceService {

    @Resource
    private TbSpyztServiceinfoMapper tbSpyztServiceinfoMapper;

    @Value("${place.name}")
    private String placeName;

    @Value("${place.query-url}")
    private String queryUrl;

    @Override
    @WebMethod
    public String queryPlace(@WebParam(targetNamespace = "http://impl.webservice.SPYZT.gt.com/", name = "token") String token,
                             @WebParam(targetNamespace = "http://impl.webservice.SPYZT.gt.com/", name = "name") String name) {


        return null;
    }
}

命名空间一般就是当前类所在包路径倒过来,一开始没有写命名空间,用postman测试一直拿不到参数值,加上命名空间就可以了

postman测试

请求体

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <queryPlace xmlns="http://impl.webservice.SPYZT.gt.com/">
            <token>meZ0fzvx2uB6BdeDQ7yizmufRZNONer9</token>
            <name></name>
        </queryPlace>
    </soap:Body>
</soap:Envelope>

请求头

示例

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是使用Spring Boot集成Apache Axis2发布Web Service服务端的示例: 1. 首先,需要在pom.xml添加Axis2和Spring Boot的依赖: ```xml <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-webapp</artifactId> <version>1.7.9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 创建一个简单Web Service服务端: ```java package com.example.demo; import org.apache.axis2.context.MessageContext; import org.apache.axis2.transport.http.HTTPConstants; import org.springframework.stereotype.Component; @Component public class MyService { public String sayHello() { MessageContext messageContext = MessageContext.getCurrentMessageContext(); String userAgent = (String) messageContext.getProperty(HTTPConstants.HEADER_USER_AGENT); return "Hello, " + userAgent + "!"; } } ``` 3. 创建一个发布Web Service服务端的配置类: ```java package com.example.demo; import org.apache.axis2.AxisFault; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContextFactory; import org.apache.axis2.description.AxisService; import org.apache.axis2.engine.AxisConfiguration; import org.apache.axis2.transport.http.server.AxisHttpService; import org.apache.axis2.transport.http.server.HttpServiceHandler; import org.apache.axis2.transport.http.server.SimpleHttpServer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.net.InetSocketAddress; @Configuration public class WebServiceConfiguration { private SimpleHttpServer server; @Autowired private MyService myService; @PostConstruct public void start() throws Exception { ConfigurationContext configurationContext = ConfigurationContextFactory.createDefaultConfigurationContext(); AxisConfiguration axisConfiguration = configurationContext.getAxisConfiguration(); AxisService axisService = new AxisService("MyService"); axisService.addParameter("ServiceClass", MyService.class.getName()); axisService.addMethod("sayHello"); axisConfiguration.addService(axisService); HttpServiceHandler handler = new HttpServiceHandler(configurationContext); AxisHttpService axisHttpService = new AxisHttpService(axisConfiguration, handler); server = new SimpleHttpServer(); server.setBindAddress(new InetSocketAddress(8080)); server.deploy(axisHttpService); server.start(); } @PreDestroy public void stop() throws AxisFault { server.stop(); } @Bean public MyService myService() { return new MyService(); } } ``` 4. 运行Spring Boot应用程序并访问http://localhost:8080/axis2/services/MyService?wsdl,应该能够看到服务的WSDL描述。 现在,您已经成功地使用Spring Boot集成Apache Axis2发布Web Service服务端。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿演

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值