下面记录并分享一版用springboot写的webservice实例:
1、新建一个maven项目,并加入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、新建一个webiservice接口
package com.web.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* author:wxy_jdhk
*/
@WebService
public interface ISendMessage {
@WebMethod
public String send(@WebParam(name = "data") String data);
}
3、新建一个webservice接口的实现类
package com.web.webservice.impl;
import javax.jws.WebService;
import com.example.demo.web.webservice.ISendMessage;
/**
* author:wxy_jdhk
*/
@WebService(targetNamespace="http://webservice.web.com/",endpointInterface = "com.web.webservice.ISendMessage")
public class SendMessage implements ISendMessage{
@Override
public String send(String data) {
return data;
}
}
4、新建一个配置类
package com.example.demo.web.webservice.config;
import javax.xml.ws.Endpoint;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import com.example.demo.web.webservice.ISendMessage;
import com.example.demo.web.webservice.impl.SendMessage;
/**
* author:wxy_jdhk
*/
@Configuration
public class ReleaseWebservice {
@Bean
public ServletRegistrationBean cxfServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
bean.setLoadOnStartup(0);
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public ISendMessage sendService() {
return new SendMessage();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), sendService());
endpoint.publish("/send");
return endpoint;
}
}
5、最后新建一个springboot启动类
package com.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* author:wxy_jdhk
*/
@SpringBootApplication
public class SpringbootWebserviceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootWebserviceApplication.class, args);
}
}