spring boot 发布 web service接口

25 篇文章 0 订阅

最近项目中又用到了webservice服务,依赖spring boot项目简单写了一个demo跑一下,东抄抄西抄抄,总算跑起来了,趁热总结整理一下。

一、创建spring boot项目

我的spring boot用的是2.1.7.RELEASE版本。

二、引入依赖

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.2.6</version>
</dependency>
<dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-rt-transports-http</artifactId>
     <version>3.2.6</version>
</dependency>
<!--下面的依赖是直接在springboot启动类中使用Endpoint发布服务时需要的,
	否则会报错:
	Caused by: java.io.IOException: Cannot find any registered HttpDestinationFactory from the Bus.
--> 
<dependency>
	<groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>3.2.6</version>
 </dependency>

启动项目可能会报错:

Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.SessionManager

需要根据自己的实际情况修改依赖的版本。

三、新建接口和实现类

接口

public interface HelloWorld {
    
    String sayHi(String name);
    
    String sum(Integer a,Integer b);
    
}

实现类

@WebService
public class HelloWorldImpl implements HelloWorld {

    @Override
    public String sayHi(String name) {
        return name+"您好!现在时间是:"+new Date();
    }

    @Override
    public String sum(Integer a, Integer b) {
        Integer sum = a + b;
        return sum.toString();
    }

}

四、发布服务

  • 第一种方式:直接在启动类中使用Endpoint,不需编写配置类,这种方式可以自定义webservice的端口,但不要和服务器的端口冲突了。(注意上面的pom依赖)
import javax.xml.ws.Endpoint;

@SpringBootApplication
@ComponentScan(basePackages = {"com.lee.demo"})
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
		
		Endpoint.publish("http://localhost:8082/webService/hello/", new HelloWorldImpl());
	}
	
}

访问路径查看wsdl描述信息 http://localhost:8082/webService/hello/?wsdl

  • 第二种方式:使用配置类,不用在启动类中加Endpoint.publish,这种接口的端口号和服务器端口号是一致的。
@Configuration
public class CxfConfig {
    //默认servlet路径/*,如果覆写则按照自己定义的来
    @Bean
    public ServletRegistrationBean myDispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }

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

    @Bean
    public Endpoint endpoint() {
        // 绑定要发布的服务
        EndpointImpl endpoint = new EndpointImpl(springBus(), new HelloWorldImpl());
        endpoint.publish("/hello");
        return endpoint;
    }
}

注意:网上很多教程的配置类中,第一个@Bean的方法名都是

public ServletRegistrationBean dispatcherServlet(){....}

这样会报错:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.

The following candidates were found but could not be injected:
	- Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.

给这个方法改个名字就解决问题了,比如说改成myDispatcherServlet……
最后访问路径查看wsdl描述信息
http://localhost:8011/demo/services/hello?wsdl

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值