SOAP是 simple object access protocol(简单对象访问协议)的缩写。这个协议是用http的post请求实现的,跟一般的post请求不同的是,在请求的header里添加了一些标志来说明自己是SOAP请求,然后body里传XML数据。
web service description language的缩写,想当于一个SOAP接口的说明书。
1、添加依赖,springboot版本过高会报错。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
2、创建服务接口WeatherService
@WebService(targetNamespace = "http://service.webservice2020.ysp.org")
public interface WeatherService {
@WebMethod
String queryWeather(@WebParam(name = "city") String city);
}
3、实现类
@Service
@WebService(serviceName = "weatherService"
,name="WeatherService" //porType名称 客户端生成代码时为接口名称
,targetNamespace = "http://service.webservice2020.ysp.org"//wsdl命名空间
,endpointInterface = "org.ysp.webservice2020.service.WeatherService")
public class WeatherServiceImpl implements WeatherService {
public final static String[] WEATHER_DESC = new String[]{"晴天", "多云", "暴雨", "大雨", "中雨", "小雨"};
@Override
public String queryWeather(String city) {
StringBuilder builder = new StringBuilder(city);
builder.append("天气:");
builder.append(WEATHER_DESC[getRandomNumber(5)]);
return builder.toString();
}
private static int getRandomNumber(int num) {
return (int) (Math.random() * num);
}
}
4、配置类
@Configuration
public class AppConfig {
@Autowired
private Bus bus;
@Autowired
private WeatherService weatherService;
/**
* 添加普通的controller处理,兼容rest接口用,非必须
* @return
*/
@Bean
public ServletRegistrationBean dispatcherRestServlet() {
AnnotationConfigWebApplicationContext context
= new AnnotationConfigWebApplicationContext();
//替换成自己的controller包路径
context.scan("org.ysp.webservice2020.controller");
DispatcherServlet disp = new DispatcherServlet(context);
ServletRegistrationBean registrationBean = new ServletRegistrationBean(disp);
registrationBean.setLoadOnStartup(1);
//映射路径自定义,必须设置一个不重复的名称
registrationBean.addUrlMappings("/rest/*");
registrationBean.setName("rest");
return registrationBean;
}
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(bus,weatherService);
endpoint.publish("/weatherService");
return endpoint;
}
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
}
}
1.Pom文件配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<build>
<plugins>
<!-- cxf-codegen-plugin -->
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>src/main/resources/wsdl/weather.wsdl</wsdl>
<wsdlLocation>classpath:wsdl/weather.wsdl</wsdlLocation>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、将WSDL文件放到resources下的wsdl文件夹,然后执行mvn generate-sources
3、配置类
@Configuration
public class AppConfig {
/**
* 以接口代理方式进行调用 WeatherService接口
*/
@Bean("cxfProxy")
public WeatherService createAuthorPortTypeProxy() {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setServiceClass(WeatherService.class);
//服务地址
jaxWsProxyFactoryBean.setAddress("http://localhost:8080/ws/weatherService");
return (WeatherService) jaxWsProxyFactoryBean.create();
}
}
4、controller调用
@RestController
@RequestMapping("/cxf")
public class ClientController {
@Autowired
@Qualifier("cxfProxy")
WeatherService weatherService;
@RequestMapping("/queryWeather")
@ResponseBody
public Map<String,Object> queryWeather(String city){
Map<String,Object> result = new HashMap<>();
result.put("code",0);
result.put("msg","success");
result.put("obj",weatherService.queryWeather(city));
return result;
}
}
参考:https://blog.csdn.net/shipaiYang/article/details/106100469