在Spring Boot中,可以使用Apache CXF来从WSDL生成SOAP服务。下面是详细的步骤:
- 添加Apache CXF依赖:在项目的pom.xml文件中,添加以下依赖:
-
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version> </dependency>
2.创建服务配置文件config
-
package com.webservice.config; import com.webservice.AppService; 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 WSConfig { @Autowired private AppService appService; //默认servlet路径/*,如果覆写则按照自己定义的来 @Bean public ServletRegistrationBean cxfServlet() { return new ServletRegistrationBean(new CXFServlet(), "/webservice/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } //终端路径 @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), appService); endpoint.publish("/test"); return endpoint; } }
创建service服务定义接口参数类型等
-
package com.webservice; import com.alibaba.fastjson.JSONArray; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import java.util.Map; @WebService(name = "AppService", // 暴露服务名称 targetNamespace = "http://webservice.com"// 命名空间,一般是接口的包名倒序 ) public interface AppService { @WebMethod String casePaces(@WebParam(name = "map") String maps); @WebMethod String getPlKt(@WebParam(name = "map") String map); }
-
实现接口
package com.webservice.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.entity.CourtUser; import com.enums.ApiResponseEnum; import com.fasterxml.jackson.databind.ObjectMapper; import com.service.CourtUserService; import com.util.*; import com.webservice.AppService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import tdh.court.ws.ajcx.IAjcxWebServiceProxy; import tdh.szft.SzftWebServiceProxy; import javax.annotation.PostConstruct; import javax.jws.WebService; import java.security.SecureRandom; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @WebService(serviceName = "AppService", // 与接口中指定的name一致 targetNamespace = "http://webservice.com", // 与接口中的命名空间一致,一般是接口的包名倒 endpointInterface = "com.webservice.AppService"// 接口地址 ) @Component @Slf4j public class AppServiceImpl implements AppService { @PostConstruct private void init() { } @Override public String casePaces(String maps) { // return ""; } @Override public String getPlKt(String maps) { return ""; } }
- 运行应用程序:启动Spring Boot应用程序,SOAP服务将在http://ip:prot/webservice/test?wsdl路径下发布。
-
至此,你已经成功在Spring Boot中从WSDL生成了SOAP服务。可以使用SOAP客户端工具来测试该服务。