幾種調用web service的方式【含spring】

首先寫一個服務端:

package com.pp.ws.server;

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

@WebService(targetNamespace="http://wsapi.pp1618.com", serviceName="PPLoginService")
public class LoginService {
	
	@WebMethod
	public String login(@WebParam(name="username") String username, @WebParam(name="password") String password){
		if(username != null && username.equals(password)){
			return "OK";
		}
		return "Faliure";
	}
}
package com.pp.ws.server;

import javax.xml.ws.Endpoint;

public class AppServer  {
    public static void main( String[] args ) {
    	Endpoint.publish("http://127.0.0.1:6644/user/login", new LoginService());
    }
}

啓動。

然後,用wsimport(JDK自帶命令,本文章中使用的是JDK1.8)生成源代碼,命令如下:

wsimport -s ./src -p com.pp.client.ws -encoding utf-8 http://127.0.0.1:6644/user/login?wsdl


另外新建一個maven項目,把上面生成的代碼(src目錄)拷貝到項目的src目錄中去

代碼結構如下:


並且加入spring依賴

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>4.3.2.RELEASE</version>
</dependency>
調用的示例代碼如下:

package com.pp.client;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean;

import com.pp.client.ws.LoginService;
import com.pp.client.ws.PPLoginService;

public class AppClient {

	//方式一
	public static void fun1() throws Exception {
		URL url = new URL("http://127.0.0.1:6644/user/login?wsdl");
		//QName的兩個參數,參照LoginService上面的註解
		QName qname = new QName("http://wsapi.pp1618.com", "PPLoginService");
		Service service = Service.create(url, qname);  
		LoginService ls = service.getPort(LoginService.class);
		System.out.println(ls.login("admin", "123"));
		System.out.println(ls.login("admin", "admin"));
	}
	
	//方式二(PPLoginService內部其實使用的是方式一)
	public static void fun2() throws Exception {
		URL url = new URL("http://127.0.0.1:6644/user/login?wsdl");
		PPLoginService ppls = new PPLoginService(url);
		LoginService ls = ppls.getLoginServicePort();
		System.out.println(ls.login("admin", "123"));
		System.out.println(ls.login("admin", "admin"));
	}
	
	//方式三,使用spring封裝的方式
	public static void fun3() throws Exception {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(WSConfig.class);
		LoginService ls = context.getBean(LoginService.class);
		System.out.println(ls.login("admin", "123"));
		System.out.println(ls.login("admin", "admin"));
		context.close();
	}
	
	public static void main(String[] args) throws Exception {
		fun1();
		fun2();
		fun3();
	}
}

@Configuration
class WSConfig {
	
	@Bean
	public JaxWsPortProxyFactoryBean createMainCouponService() throws MalformedURLException {
		//JaxWsPortProxyFactoryBean 類在spring-web.jar裏面
		JaxWsPortProxyFactoryBean bean = new JaxWsPortProxyFactoryBean();
		bean.setServiceInterface(LoginService.class);
		bean.setServiceName("PPLoginService");
		bean.setWsdlDocumentUrl(new URL("http://127.0.0.1:6644/user/login?wsdl"));
		//設置超時
		bean.addCustomProperty("com.sun.xml.internal.ws.request.timeout", 20000);
		bean.addCustomProperty("com.sun.xml.internal.ws.connect.timeout", 20000);
		return bean;
	}
}

需要注意的是:前兩種方式是使用JDK自帶的方式,第三種是spring封裝的調用方式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章