目录

省流:

一、springboot中使用webService

1.通过@Configuration启动Endpoint

2.配置路径

二、测试 

三、main方法测试WebService服务


省流:

核心代码

Endpoint endpoint = EndpointImpl.publish(.. , ..);

一、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 = EndpointImpl.publish(address,myservice);
		return endpoint;
	}
}

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

 参考:

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

2.配置路径

package com.test.springboot.config;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@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);
        endpoint.publish("/save");
        return endpoint;
    }

}

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

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

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

return new ServletRegistrationBean(new CXFServlet(), "/mywebsvc/*"),直接在application.properties配置文件中添加:cxf.path=/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 {
        System.out.println("Starting Server");
        MyService myservice = new MyServiceImpl();
        String address = "http://localhost:8090/mywebsvc/save";
        Endpoint.publish(address, myservice);
    }

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

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}

四、解读

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

@WebService
(
        name="MyService", 
        serviceName="MyServiceService",
        portName="MyServicePort", 
        targetNamespace="t1.server.ws.ali.com" 
)
public class MyService{

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

 

wsimport 使用方法

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

06-28 10:45