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>

请求头

示例

经验分享 程序员 微信小程序 职场和发展