天天看點

springboot整合cxf 代碼結構配置類 webservice接口 webservice接口實作cxf用戶端動态調用 

 代碼結構

springboot整合cxf 代碼結構配置類 webservice接口 webservice接口實作cxf用戶端動态調用 

配置類 

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.asiainfo.group.webservice.HelloService;

@Configuration
public class CxfConfig {
	@Autowired
    private Bus bus;

    @Autowired
    HelloService helloService;

    //預設是service
    /*@Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
    }*/

    
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, helloService);
        endpoint.publish("/hello");
        return endpoint;
    }

}
           

webservice接口 

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface HelloService {
	//标注該方法為webservice暴露的方法,用于向外公布,它修飾的方法是webservice方法,去掉也沒影響的,類似一個注釋資訊
	@WebMethod
    public String getUsername(@WebParam(name = "userId") String userId);

    @WebMethod
    @WebResult(name="String",targetNamespace="")
    public String getUserAge(@WebParam(name = "userId") String userId);

}
           

webservice接口實作

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.asiainfo.group.webservice.HelloService;



@WebService(serviceName="helloService",//對外釋出的服務名
targetNamespace="http://webservice.group.asiainfo.com",//指定你想要的名稱空間,通常使用使用包名反轉
endpointInter)//服務接口全路徑, 指定做SEI(Service EndPoint Interface)服務端點接口
@Component
public class HelloServiceImpl implements HelloService{

	@Override
	public String getUsername(String userId) {
		
		return userId+":zhangsan";
	}

	@Override
	public String getUserAge(String userId) {
		
		return userId+":18";
	}

}
           

cxf用戶端動态調用 

import javax.xml.namespace.QName;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


public class CxfClient {

	

    public static void main(String[] args) throws Exception {
    	String wsdl="http://localhost:8080/services/hello?wsdl";
    	//最後的斜杠不能少
    	String targetNamespace = "http://webservice.group.asiainfo.com/";
        String methodName ="getUsername";
        String userId="100001";
        Object[] params=new Object[]{userId};
        
        // 建立動态用戶端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdl);
        
        // 需要密碼的情況需要加上使用者名和密碼
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects;
            
        objects = client.invoke(new QName(targetNamespace, methodName), params);
        System.out.println("傳回資料:" + objects[0]);
        
    }
}
           

繼續閱讀