项目结构
CxfConfig类
import com.namchow.webservice.service.CommonService;
import com.namchow.webservice.service.impl.CommonServiceImpl;
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 CxfConfig {
@Autowired
CommonService commonService;
@Bean
public ServletRegistrationBean dispatchServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/services/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public CommonService commonService() {
return new CommonServiceImpl();
}
/**
* JAX-WS
**/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), commonService());
endpoint.publish("/CommonService");
return endpoint;
}
}
CommonService类
import javax.jws.WebService;
/**
* 接口
*/
@WebService(name = "CommonService", // 暴露服务名称
targetNamespace = "http://webService.CommonService.com/"// 命名空间,一般是接口的包名倒序
)
public interface CommonService {
String submit(String strSend) throws Exception;
}
CommonServiceImpl类
package com.namchow.webservice.service.impl;
import com.namchow.webservice.service.CommonService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.io.*;
import java.net.Socket;
import java.net.URLDecoder;
/**
* 接口实现
*/
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
targetNamespace = "http://CommonService.service.com/", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.service.CommonService"// 接口地址
)
@Component
public class CommonServiceImpl implements CommonService {
@Override
@WebMethod(operationName="helloWord")
public String submit(@WebParam(name = "s") String s) throws Exception {
return "Hello Word";
}
}
访问url:http://localhost:8081/services/CommonService?wsdl
参考:https://www.cnblogs.com/myitnews/p/12370308.html