A simple Spring webservice example for Google Suggest

05 Dec 2005 17:37 - (0) comments

We'll be using Axis in this example. So you'll need to download this first.

Let's start by defining the interface for the service based on the WSDL.

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface GoogleService extends Remote {

String doSpellingSuggestion(String key, String input) throws RemoteException;

String doGoogleSearch(String key, String input) throws RemoteException;

String doGetCachedPage(String key, String input) throws RemoteException;
}

The Google service returns a simple GoogleMessageBean:

public class GoogleMessageBean implements Serializable {

private String key;
private String phrase;

public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getPhrase() {
return phrase;
}
public void setPhrase(String phrase) {
this.phrase = phrase;
}
}

Next we'll map the MessageBean to the service.

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean;

public class GoogleServiceJaxRpcProxyFactoryBean
extends JaxRpcPortProxyFactoryBean {

protected void postProcessJaxRpcService(Service service) {

TypeMappingRegistry tmr = service.getTypeMappingRegistry();
TypeMapping tm = tmr.createTypeMapping();

QName qname = new QName("http://www.google.com", "GoogleMessageBean");

tm.register(GoogleMessageBean.class, qname,
new BeanSerializerFactory(GoogleMessageBean.class, qname),
new BeanDeserializerFactory(GoogleMessageBean.class, qname));

tmr.register("http://schemas.xmlsoap.org/soap/encoding/", tm);
}
}

We also need to configure the Spring beans. Save this file as googleExample.xml:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="googleService" class="GoogleServiceJaxRpcProxyFactoryBean">
<property name="serviceFactoryClass">
<value>org.apache.axis.client.ServiceFactory</value>
</property>
<property name="wsdlDocumentUrl">
<value>http://api.google.com/GoogleSearch.wsdl</value>
</property>
<property name="namespaceUri">
<value>urn:GoogleSearch</value>
</property>
<property name="serviceName">
<value>GoogleSearchService</value>
</property>
<property name="portName">
<value>GoogleSearchPort</value>
</property>
<property name="serviceInterface">
<value>GoogleService</value>
</property>
</bean>
</beans>

Let's run a unittest to see if everything works. Don't forget to change YOUR_KEY to your Google key.

import java.rmi.RemoteException;

import junit.framework.TestCase;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class GoogleWebServiceExampleTest extends TestCase {

private ApplicationContext context = new ClassPathXmlApplicationContext("googleExample.xml");
private GoogleService service = (GoogleService) context.getBean("googleService");

public void testSpellingSuggestExample() throws RemoteException {
String test = "webservie";
assertEquals("web service", service.doSpellingSuggestion("YOUR_KEY",test));
}

}

And now we should have a working client for the webservice.

Comments

No comments allowed.

Admin