最近项目中又用到了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