一: webservice依赖包
org.springframework.boot spring-boot-starter-web-services
二: cxf依赖包
org.apache.cxf cxf-core3.1.12 org.apache.cxf cxf-rt-frontend-jaxws3.1.12 org.apache.cxf cxf-rt-transports-http3.1.12
三: 创建接口(接口加上@WebService注解)
package com.javaweb.piclesoft.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService;
@WebService(
name = "PsonWebService", //服务名(直接写接口名)
targetNamespace = "http://service.piclesoft.javaweb.com" //命名空间,接口的倒序
)
public interface PsonWebService {
@WebMethod //方法注解
String psonGet(@WebParam(name = "say") String say); //参数注解
}
四: 创建接口的实现类(实现类加上 @Service 和 @WebService注解)
package com.javaweb.piclesoft.service.impl; import com.javaweb.piclesoft.service.PsonWebService; import org.springframework.stereotype.Service; import javax.jws.WebService; import java.text.SimpleDateFormat; import java.util.Date;
@Service
@WebService(
name = "PsonWebService", //和接口服务名一样
targetNamespace = "http://service.piclesoft.javaweb.com", //和接口的命名空间一样
endpointInterface = "com.javaweb.piclesoft.service.PsonWebService" //接口的路径
)
public class PsonWebServiceImpl implements PsonWebService {
@Override
public String psonGet(String say) {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
return say+"时间为: "+simpleDateFormat.format(new Date());
}
}
五: 创建一个配置类
package com.javaweb.piclesoft.config;
import com.javaweb.piclesoft.service.WebServiceDemoService;
import com.javaweb.piclesoft.service.impl.PsonWebServiceImpl;
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.beans.factory.annotation.Autowired;
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 WebServiceConfig {
@Autowired
private PsonWebService psonWebService ;
@Bean(name = "cxfServlet")
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/webservice
@Bean
public Endpoint psonEndpoint(){
EndpointImpl endpoint =new EndpointImpl(springBus(),psonWebService );
endpoint.publish("/webservice");
return endpoint;
}
}
六: 启动项目,谷歌浏览器访问地址http://localhsot:自己的访问端口号/webservice // 端口号后面的webservice是配置类第一个设置的: (new CXFServlet(),"/webservice/*")
七: postman测试:测试地址为http://localhost:(自己的端口号)/webservice/webservice 1. 端口号后面的webservice是配置类里面: (new CXFServlet(),"/webservice/*"); 2. 第二个webservice是配置类里面: endpoint.publish("/webservice"); 3. postamn请求方法改为: post; 4. Headers添加一个参数: key = Content-Type value = text/xml;charset=UTF-8; 5. 在Body 先选择 raw 在选择 xml 格式 复制下面的格式到xml里面;
注意:
1. xmlns:test= "填写接口上设置的命名空间". 2.
八: 点击Send就有结果啦 结果如下:



