WebService wsdl配置 测试 【springboot】

时间:2025-03-18 12:53:01

目录

省流:

一、springboot中使用webService

1.通过@Configuration启动Endpoint

2.配置路径

二、测试 

三、main方法测试WebService服务


省流:

核心代码

Endpoint endpoint = (.. , ..);

一、springboot中使用webService

1.通过@Configuration启动Endpoint

@Configuration
public class WebServiceConfig {
	//创建websocket的Endpoint
	@Bean
	public Endpoint getEndpoint() {
        //业务功能的类				
		MyService myservice = new MyServiceImpl();
        String address = "http://localhost:8090/save";
		Endpoint endpoint = (address,myservice);
		return endpoint;
	}
}

这里的wsdl路径是:http://localhost:8090/save?wsdl

 参考:

SpringBoot中使用WebService(简单的使用)

2.配置路径

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

@Configuration
public class WebServiceConfig {
    @Autowired
    private MyService myservice;


    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean
    public ServletRegistrationBean newServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/mywebsvc/*");
    }

    @Bean(name = "myEndpoint")
    public Endpoint myEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), myservice);
        ("/save");
        return endpoint;
    }

}

这里的wsdl路径是:http://项目的域名/项目的根路径/mywebsvc/save?wsdl

例如项目域名是:,项目根路径是:/365buy,那么这个wsdl路径就是: /365buy/mywebsvc/save?wsdl

注意:在spring boot2.0.6之后的版本与cxf集成,不需要再定义方法 

return new ServletRegistrationBean(new CXFServlet(), "/mywebsvc/*"),直接在配置文件中添加:=/service(默认是services) 

参考: 

 springboot开发webservice服务端,发布多个webservice服务案例(带你玩转webservice)

二、测试 

为了验证上面的wsdl地址是否正确,可以直接用postman来检测。

将wsdl地址复制到postman,直接点击send即可。无需其他配置。如果地址正确,会拿到xml返回值,里面有你定义的参数,即实体类里的字段名。

如果地址错误,会返回 <body>No service was found.</body>

补充:默认使用get请求,请求参数也无需配置。 

 Postman测试WebService接口,详细步骤,带用户验证

三、main方法测试WebService服务

如果想要自测webService服务是否正常跑起来,可以写个main方法测一下

public class MyWebServiceServer{

    protected MyWebServiceServer() throws Exception {
        ("Starting Server");
        MyService myservice = new MyServiceImpl();
        String address = "http://localhost:8090/mywebsvc/save";
        (address, myservice);
    }

    public static void main(String args[]) throws Exception {
        new MyWebServiceServer();
        ("Server ready...");

        (5 * 60 * 1000);
        ("Server exiting");
        (0);
    }
}

四、解读

上面的是WebService路径wsdl的配置,下面是业务实现类的解读。

@WebService
(
        name="MyService", 
        serviceName="MyServiceService",
        portName="MyServicePort", 
        targetNamespace="" 
)
public class MyService{

    @WebMethod(operationName="process")
    public @WebResult(name="result") String process(@WebParam(name="name") String name,@WebParam(name="age") int age){
        ("name:" + name + ",age:" + age);
        return "code:200;数据保存成功";
    }
}

@WebService

自定义ws服务,jdk1.6仅支持 soap1.1格式,jdk1.7及以上支持 soap1.2格式。
Endpoint发布ws服务需要你提供一个业务实现类,在这个类上加上@WebService注解。

name="MyService"

服务实现类的名称


serviceName="MyServiceService"

默认在发布的服务实现类的名称后面添加Service


portName="MyServicePort"

服务类型的名称:默认在发布的服务实现类(MyService) 后面添加 port


targetNamespace=""

发布ws服务的命名空间。此空间默认为当前服务包路径的 "倒写"。此名称是 wsimport 命令生成 java类时默认的包路径 -p
 

@WebMethod(operationName="process")

方法名

@WebResult(name="result")

返回结果

@WebParam(name="age")

入参

wsimport 使用方法

【WebService】wsdl配置详解以及使用注解修改wsdl配置_武哥聊编程的博客-****博客

=========================分割线========================== 

文章到此已经结束,以下是紫薯布丁

Endpoint endpoint = (.. , ..); 

@Configuration
public class WebServiceConfig {
    //创建websocket的Endpoint
    @Bean
    public Endpoint getEndpoint() {
        //业务功能的类                
        MyService myservice = new MyServiceImpl();
        String address = "http://localhost:8090/save";
        Endpoint endpoint = (address,myservice);
        return endpoint;
    }
}

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

@Configuration
public class WebServiceConfig {
    @Autowired
    private MyService myservice;


    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean
    public ServletRegistrationBean newServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/mywebsvc/*");
    }

    @Bean(name = "myEndpoint")
    public Endpoint myEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), myservice);
        ("/save");
        return endpoint;
    }

}

public class MyWebServiceServer{

    protected MyWebServiceServer() throws Exception {
        ("Starting Server");
        MyService myservice = new MyServiceImpl();
        String address = "http://localhost:8090/mywebsvc/save";
        (address, myservice);
    }

    public static void main(String args[]) throws Exception {
        new MyWebServiceServer();
        ("Server ready...");

        (5 * 60 * 1000);
        ("Server exiting");
        (0);
    }
}

@WebService
(
        name="MyService", 
        serviceName="MyServiceService",
        portName="MyServicePort", 
        targetNamespace="" 
)
public class MyService{

    @WebMethod(operationName="process")
    public @WebResult(name="result") String process(@WebParam(name="name") String name,@WebParam(name="age") int age){
        ("name:" + name + ",age:" + age);
        return "code:200;数据保存成功";
    }
}