WebServiceの練習


最近棟兄の影響でwebserviceに接触し、彼の例を参照して練習した.後で使用するために記録する.添付ファイルはaxisです.JArパッケージ
WebServiceクラスにテストするいくつかの方法を書きます.

package com;

public class WebService {
	public boolean login(String name, String password) {
		if (name.equals("a") && password.equals("b")) {
			return true;
		}
		return false;
	}

	private String privateMathod(String content) {
		return content;
	}

	protected String protectedMethod(String content) {
		return content;
	}
}


<?xml version="1.0" encoding="UTF-8"?>
<deployment
	xmlns="http://xml.apache.org/axis/wsdd/"
	xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
	<globalConfiguration>
		<parameter
			name="adminPassword"
			value="admin" />
		<parameter
			name="sendXsiTypes"
			value="true" />
		<parameter
			name="sendMultiRefs"
			value="true" />
		<parameter
			name="sendXMLDeclaration"
			value="true" />
		<parameter
			name="axis.sendMinimizedElements"
			value="true" />
		<requestFlow>
			<handler
				type="java:org.apache.axis.handlers.JWSHandler">
				<parameter
					name="scope"
					value="session" />
			</handler>
			<handler
				type="java:org.apache.axis.handlers.JWSHandler">
				<parameter
					name="scope"
					value="request" />
				<parameter
					name="extension"
					value=".jwr" />
			</handler>
		</requestFlow>
	</globalConfiguration>
	<handler
		name="Authenticate"
		type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
	<handler
		name="LocalResponder"
		type="java:org.apache.axis.transport.local.LocalResponder" />
	<handler
		name="URLMapper"
		type="java:org.apache.axis.handlers.http.URLMapper" />
	<service
		name="AdminService"
		provider="java:MSG">
		<parameter
			name="allowedMethods"
			value="AdminService" />
		<parameter
			name="enableRemoteAdmin"
			value="false" />
		<parameter
			name="className"
			value="org.apache.axis.utils.Admin" />
		<namespace>http://xml.apache.org/axis/wsdd/
		</namespace>
	</service>
	<service
		name="Version"
		provider="java:RPC">
		<parameter
			name="allowedMethods"
			value="getVersion" />
		<parameter
			name="className"
			value="org.apache.axis.Version" />
	</service>
	<transport
		name="http">
		<requestFlow>
			<handler
				type="URLMapper" />
			<handler
				type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
		</requestFlow>
	</transport>
	<transport
		name="local">
		<responseFlow>
			<handler
				type="LocalResponder" />
		</responseFlow>
	</transport>

	
	<service
		name="WebService"
		provider="java:RPC">
		<parameter
			name="className"
			value="com.WebService" />
	</service>
</deployment>

これはserver-configです.wsddファイルの内容..いくつかの書類の上の大きな部分は何をしているのかまだよく分からない.

<service
		name="WebService"
		provider="java:RPC">
		<parameter
			name="className"
			value="com.WebService" />
	</service>

この段落がポイントです.WebServiceを暴露すると、WebServiceのすべてのpublicが外部にアクセスできます.

<servlet>
		<servlet-name>Apache Axis Servlet</servlet-name>
		<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Apache Axis Servlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

この段落はwebです.xmlの内容

package com;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.junit.Test;

public class WebServiceTest {
	private String url = "http://localhost:8080/aaa/services/WebService?wsdl";

	@Test
	public void testLogin() throws Exception {
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(url);
		call.setOperationName("login");
		Boolean result = (Boolean) call.invoke(new Object[] { "a", "b" });
		assertTrue(result);
	}

	@Test
	public void testPrivateMathod() throws Exception {
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(url);
		call.setOperationName("testPrivateMathod");
		String returnContent = (String) call.invoke(new Object[] { "aaaa" });
		assertEquals(returnContent, "aaaa");

	}

}