代码地址
https://gitee.com/chrisfzh/dailytest
代码结构
代码及说明
pom文件中添加的依赖项
<!--WebServcie-->
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.4</version>
</dependency>
配置类
package com.chrisf.webservice.config;
import com.chrisf.webservice.service.DemoService;
import com.chrisf.webservice.service.impl.DemoServiceImpl;
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 javax.xml.ws.Endpoint;
@Configuration
public class CxfConfig {
//此处的方法名注意不能叫dispatcherServlet(),此处加个1是为了提醒一下,这个方法名与框架的方法名重复了
@Bean
public ServletRegistrationBean dispatcherServlet1(){
return new ServletRegistrationBean(new CXFServlet(), "/demo/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus(){
return new SpringBus();
}
@Bean
public DemoService demoService(){
return new DemoServiceImpl();
}
//第一个接口
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api");
return endpoint;
}
//第二个接口
@Bean
public Endpoint endpoint2(){
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api2");
return endpoint;
}
}
此处需要注意的就是上面注释中写的,不能用 dispatcherServlet()这个方法名,会跟框架的方法重名,启动会报如下错误:
Parameter 0 of method errorPageCustomizer 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.
服务接口
package com.chrisf.webservice.service;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(name = "DemoService",targetNamespace = "http://service.webservice.chrisf.com")
public interface DemoService {
public String sayHello(@WebParam(name = "user", //访问接口时的参数标签名
targetNamespace = "http://service.webservice.chrisf.com")
String user);
}
实现类
package com.chrisf.webservice.service.impl;
import com.chrisf.webservice.service.DemoService;
import javax.jws.WebService;
import java.util.Date;
@WebService(serviceName = "DemoService",targetNamespace = "http://service.webservice.chrisf.com",endpointInterface = "com.chrisf.webservice.service.DemoService")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String user) {
return user + ", 现在时间:" + "(" + new Date() + ")";
}
}
postman访问测试
地址:http://localhost:8080/demo/api?wsdl
请求方式:post
请求头:Context-Type:text/html
请求文本:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<sayHello xmlns="http://service.webservice.chrisf.com">
<user>
张三
</user>
</sayHello>
</soap:Body>
</soap:Envelope>
注意:user对应@WebParam中的name属性,xmlns对应的是targetNamespace属性,sayHello对应的是方法名
可以看到,已经返回了我们想要的结果,同时也可以测试一下第二个接口,地址为http://localhost:8080/demo/api2?wsdl