Back Forum Reply New

Help with echo example

Trying my first Spring web service, downloaded the sample code. Have a few questions.

\WEB-INF\spring-ws-servlet.xml

Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;schema/beansquot;gt;

lt;bean id=quot;echoEndpointquot; class=quot;echo.endpoint.EchoEndpointquot;gt;
lt;property name=quot;echoServicequot;gt;lt;ref bean=quot;echoServicequot;/gt;lt;/propertygt;
lt;/beangt;

lt;bean id=quot;echoServicequot; class=quot;echo.service.impl.EchoServiceImplquot;/gt;

lt;bean class=quot;org..ws.server.endpoint.mapping.PayloadRootQNameEndpointMappingquot;gt;
lt;property name=quot;mappingsquot;gt;
lt;propsgt;
lt;prop key=quot;{propgt;
lt;/propsgt;
lt;/propertygt;
lt;property name=quot;interceptorsquot;gt;
lt;bean
class=quot;org..ws.server.endpoint.interceptor.PayloadLoggingInterceptorquot;
/gt;
lt;/propertygt;
lt;/beangt;

lt;bean id=quot;echoquot; class=quot;org..ws.wsdl.wsdl11.DynamicWsdl11Definitionquot;gt;
lt;property name=quot;builderquot;gt;
lt;bean
class=quot;org..ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilderquot;gt;
lt;property name=quot;schemaquot; value=quot;/WEB-INF/echo.xsdquot;/gt;
lt;property name=quot;portTypeNamequot; value=quot;Echoquot;/gt;
lt;property name=quot;locationUriquot; value=quot;echoservice/quot;/gt;
lt;/beangt;
lt;/propertygt;
lt;/beangt;
lt;/beansgt;
web.xmlCode:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;web-app xmlns=quot;xml/ns/j2eequot; xmlns:xsi=quot;2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;xml/ns/j2eequot;gt;

lt;display-namegt;Echo Web Service Applicationlt;/display-namegt;

lt;servletgt;
lt;servlet-namegt;spring-wslt;/servlet-namegt;
lt;servlet-classgt;org..ws.transport.from.MessageDispatcherServletlt;/servlet-classgt;
lt;/servletgt;

lt;servlet-mappinggt;
lt;servlet-namegt;spring-wslt;/servlet-namegt;
lt;ucl-patterngt;/*lt;/ucl-patterngt;
lt;/servlet-mappinggt;

lt;/web-appgt;
WEB-INF\echo.xsdCode:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;schema xmlns=quot;2001/XMLSchemaquot; elementFormDefault=quot;qualifiedquot; attributeFormDefault=quot;qualifiedquot;       targetNamespace=quot;spring-ws/samples/echoquot;       xmlns:tns=quot;spring-ws/samples/echoquot;gt;
   lt;element name=quot;echoRequestquot;gt;       lt;simpleTypegt;lt;restriction base=quot;stringquot;gt;    lt;pattern value=quot;([A-Z]|[a-z])+quot;/gt;lt;/restrictiongt;       lt;/simpleTypegt;   lt;/elementgt;
   lt;element name=quot;echoResponsequot; type=quot;stringquot;/gt;
lt;/schemagt;

Code:
package echo.endpoint;

import echo.service.EchoService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org..ws.server.endpoint.AbstractDomPayloadEndpoint;

public class EchoEndpoint extends AbstractDomPayloadEndpoint {

private EchoService echoService;

public void setEchoService(EchoService echoService) {
this.echoService = echoService;
}

@Override
protected Element invokeInternal(Element arg0, Document arg1) throws Exception {
//Dummy code
echoService.echo(quot;abcquot;);
return null;
}

}

Code:
package echo.service;

public interface EchoService {
public String echo(java.lang.String name);

}

Code:
package echo.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import echo.service.EchoService;

public class EchoServiceImpl implements EchoService {

public String echo(String name) {
if (name == null || name.trim().length() == 0) {
return quot;echoing back: -provide a name-quot;;
}
SimpleDateFormat dtfmt = new SimpleDateFormat(quot;MM-dd-yyyy hh:mm:ss aquot;);
return quot;echoing back: name quot; + name + quot; received on date quot; + dtfmt.format(Calendar.getInstance().getTime());
}
}
That being the code, following are my questions:

1. Is my prop-key configuration in spring-ws-servlet.xml incorrect?

2. Do I need to modify the echo.sxd namespace?

3. Should I be able to see the wsdl under: spring-ws/echo.wsdl  

If I type the above in browser, it gives error as: Error 404: No target servlet configured for uri: /spring-ws/echo.wsdl

I read the documentation, tutorial etc.

Please advise on what I am missing.

As you guessed, the prop-key entry is incorrect. It should be:
lt;prop key=quot;{spring-ws/samples/echo}echoRequestquot;gt;echoEndpointlt;/propgt;

You need to put the XML namespace and the request name (case-sensitive) as defined in the XSD in the prop-key above.

Also you seem to have got the WSDL ucl wrong. It should be:
lt;host-name(with port number if required)gt;/lt;application-namegt;/lt;wsdl-bean-namegt;.wsdl
I think your application name is quot;echoservicequot; and not quot;spring-wsquot;. Just have a look at it, and change accordingly. It should be like this:
echoservice/echo.wsdl

I made some modifications to the sample code to make it work for me. It might help a new starter. Following are the steps:

Create a project (e.g. Dynamic Web Project) e.g. MyEcho. So the context root is MyEcho. Application runs under: MyEcho

Put all the jar files found from spring web services module in the classpath.

Under src folder, create the of the Java project, create the following classes:Code:
package org..ws.samples.echo.service;

public interface EchoService {
   String echo(String s);
}

Code:
package org..ws.samples.echo.service.impl;

import org..ws.samples.echo.service.EchoService;

public class EchoServiceImpl implements EchoService {
   public String echo(String s) {       return s;   }
}
This will require more work. Never the less, invokeInternal method does get executed by the Cleint program.Code:
package org..ws.samples.echo.ws;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org..util.Assert;
import org..ws.samples.echo.service.EchoService;
import org..ws.server.endpoint.AbstractDomPayloadEndpoint;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class EchoEndpoint extends AbstractDomPayloadEndpoint{
    public EchoEndpoint() {    }        @Override
protected Element invokeInternal(Element arg0, Document arg1)
throws Exception {
System.out.println(quot;xyzquot;);
return null;
}
}
Under WebContent\WEB-INF directory, create echo.xsd as:Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;schema xmlns=quot;2001/XMLSchemaquot; elementFormDefault=quot;qualifiedquot; attributeFormDefault=quot;qualifiedquot;       targetNamespace=quot;spring-ws/samples/echoquot;       xmlns:tns=quot;spring-ws/samples/echoquot;gt;
   lt;element name=quot;echoRequestquot;gt;       lt;simpleTypegt;lt;restriction base=quot;stringquot;gt;    lt;pattern value=quot;([A-Z]|[a-z])+quot;/gt;lt;/restrictiongt;       lt;/simpleTypegt;   lt;/elementgt;
   lt;element name=quot;echoResponsequot; type=quot;stringquot;/gt;
lt;/schemagt;
Under WebContent\WEB-INF\web.xml, code as: Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;web-app xmlns=quot;xml/ns/j2eequot; xmlns:xsi=quot;2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;xml/ns/j2eequot;gt;

lt;display-namegt;Echo Web Service Applicationlt;/display-namegt;

lt;servletgt;
lt;servlet-namegt;spring-wslt;/servlet-namegt;
lt;servlet-classgt;org..ws.transport.from.MessageDispatcherServletlt;/servlet-classgt;
lt;/servletgt;

lt;servlet-mappinggt;
lt;servlet-namegt;spring-wslt;/servlet-namegt;
lt;ucl-patterngt;/*lt;/ucl-patterngt;
lt;/servlet-mappinggt;
lt;/web-appgt;
The name of the servlet chosen arbitrarily as quot;spring-wsquot;, as found in web.xml. Create an XML file with the name of the servlet (named arbitrarily). The name of the new file to create will be spring-ws-servlet.xml, As such, create a file  called spring-ws-servlet.xml under WebContent\WEB-INF directory.

Under WebContent\WEB-INF directory, create spring-ws-servlet.xml as:Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;schema/beans schema/beans/spring-beans-2.0.xsdquot;gt;

lt;bean id=quot;echoEndpointquot; class=quot;org..ws.samples.echo.ws.EchoEndpointquot;gt;
lt;/beangt;

lt;bean class=quot;org..ws.server.endpoint.mapping.PayloadRootQNameEndpointMappingquot;gt;
lt;property name=quot;mappingsquot;gt;
lt;propsgt;
lt;!-- echoRequest is the exact name found in WEB-INF\echo.xsd --gt;
lt;prop key=quot;{spring-ws/samples/echo}echoRequestquot;gt;echoEndpointlt;/propgt;
lt;/propsgt;
lt;/propertygt;
lt;property name=quot;interceptorsquot;gt;
lt;bean
class=quot;org..ws.server.endpoint.interceptor.PayloadLoggingInterceptorquot;
/gt;
lt;/propertygt;
lt;/beangt;

lt;bean id=quot;echoquot; class=quot;org..ws.wsdl.wsdl11.DynamicWsdl11Definitionquot;gt;
lt;property name=quot;builderquot;gt;
lt;bean
class=quot;org..ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilderquot;gt;
lt;property name=quot;schemaquot; value=quot;/WEB-INF/echo.xsdquot;/gt;
lt;property name=quot;portTypeNamequot; value=quot;Echoquot;/gt; lt;!-- Port name can be arbitrary name chosen by you --gt;
lt;property name=quot;locationUriquot; value=quot;MyEcho/quot;/gt; lt;!--  MyEcho is context root of application, need to specify context root --gt;
lt;/beangt;
lt;/propertygt;
lt;/beangt;
lt;/beansgt;
Deploy your EAR file in the Application server and start your server. Depending on the application server of your choice, you may have a need to change the classloader mode of the EAR file deployed to PARENT_LAST and WAR classloader policy is Application. In the application.xml of the EAR file in the Deployment tab, you can choose the classloader policies.

If your server and application deployed in the server starts successfully, you can see the WSDL by opening a browser and typing ucl as:

MyEcho/echo.wsdl

The content of the above echo.wsdl is:

Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot; ?gt;
- lt;wsdl:definitions xmlns:wsdl=quot;wsdl/quot; xmlns:schema=quot;spring-ws/samples/echoquot; xmlns:soap=quot;wsdl/soap/quot; targetNamespace=quot;spring-ws/samples/echoquot;gt;
- lt;wsdl:typesgt;
- lt;schema xmlns=quot;2001/XMLSchemaquot; xmlns:tns=quot;spring-ws/samples/echoquot; attributeFormDefault=quot;qualifiedquot; elementFormDefault=quot;qualifiedquot; targetNamespace=quot;spring-ws/samples/echoquot;gt;
- lt;element name=quot;echoRequestquot;gt;
- lt;simpleTypegt;
- lt;restriction base=quot;stringquot;gt; lt;pattern value=quot;([A-Z]|[a-z])+quot; /gt;  lt;/restrictiongt; lt;/simpleTypegt; lt;/elementgt; lt;element name=quot;echoResponsequot; type=quot;stringquot; /gt;  lt;/schemagt; lt;/wsdl:typesgt;
- lt;wsdl:message name=quot;echoResponsequot;gt; lt;wsdl:part element=quot;schema:echoResponsequot; name=quot;echoResponsequot; /gt;  lt;/wsdl:messagegt;
- lt;wsdl:message name=quot;echoRequestquot;gt; lt;wsdl:part element=quot;schema:echoRequestquot; name=quot;echoRequestquot; /gt;  lt;/wsdl:messagegt;
- lt;wsdl:portType name=quot;Echoquot;gt;
- lt;wsdlperation name=quot;echoquot;gt; lt;wsdl:input message=quot;schema:echoRequestquot; name=quot;echoRequestquot; /gt;  lt;wsdlutput message=quot;schema:echoResponsequot; name=quot;echoResponsequot; /gt;  lt;/wsdlperationgt; lt;/wsdl:portTypegt;
- lt;wsdl:binding name=quot;EchoBindingquot; type=quot;schema:Echoquot;gt; lt;soap:binding style=quot;documentquot; transport=quot;soap/fromquot; /gt;
- lt;wsdlperation name=quot;echoquot;gt; lt;soapperation soapAction=quot;quot; /gt;
- lt;wsdl:input name=quot;echoRequestquot;gt; lt;soap:body use=quot;literalquot; /gt;  lt;/wsdl:inputgt;
- lt;wsdlutput name=quot;echoResponsequot;gt; lt;soap:body use=quot;literalquot; /gt;  lt;/wsdlutputgt; lt;/wsdlperationgt; lt;/wsdl:bindinggt;
- lt;wsdl:service name=quot;EchoServicequot;gt;
- lt;wsdl:port binding=quot;schema:EchoBindingquot; name=quot;EchoPortquot;gt; lt;soap:address location=quot;MyEcho/quot; /gt;  lt;/wsdl:portgt; lt;/wsdl:servicegt; lt;/wsdl:definitionsgt;
Next, you can write a client as follows:Code:
package org..ws.samples.echo.ws;

import java.io.StringReader;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org..ws.client.core.WebServiceTemplate;

public class WebServiceClient {
//echoRequest tag is defined in WEB-INF\echo.xsd
// spring-ws/samples/echo is the namespace defined in spring-ws-servlet.xml and echo.xsd   private static final String MESSAGE =    quot;lt;echoRequest xmlns=\quot;spring-ws/samples/echo\quot;gt;Hello Web Service Worldlt;/echoRequestgt;quot;;    private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
   public void setDefaultUri(String defaultUri) {       webServiceTemplate.setDefaultUri(defaultUri);   }
   // send to the configured default URI   public void simpleSendAndReceive() {       StreamSource source = new StreamSource(new StringReader(MESSAGE));       StreamResult result = new StreamResult(System.out);       boolean bool = webServiceTemplate.sendSourceAndReceiveToResult(source, result);       System.out.println(bool);   }     public static void main(String[] args){   WebServiceClient client = new WebServiceClient();   client.setDefaultUri(quot;MyEchoquot;);   client.simpleSendAndReceive();       }
}
This client code invoked the invokeInternal(..) method of the Endpoint class.

However System.out.println(bool); shows bool as false. Any idea?
¥
Back Forum Reply New