SpringBoot集成WebService
导读
找了一天尝试了很多种方法,最终才搞定,分享一下 谢谢老板 springboot版本号为2.1.3.RELEASE。
dependency
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.6</version>
</dependency>
service
interface:
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace = "http://webservice.com")
public interface CommonService {
@WebMethod
public int doInitalize(@WebParam(name="name")String name, @WebParam(name="address")String address);
}
impl:
import com.alibaba.fastjson.JSONArray;
import com.webservice.CommonService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@WebService(name = "commonService", targetNamespace = "http://webservice.com",
endpointInterface = "webservice.CommonService")
@Component
public class CommonServiceImpl implements CommonService {
@Override
public int doInitalize(String aa, String bb){
int i = 0;
return i;
}
}
Config:
import com.webservice.CommonService;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 {
@Bean
public ServletRegistrationBean wsServlet(){
return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
}
@Autowired
private CommonService commonService;
@Autowired
@Qualifier(Bus.DEFAULT_BUS_ID)
private SpringBus bus;
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
endpoint.publish("/commonService");
return endpoint;
}
}
访问
这样就可以访问到wsdl了