You are here

JAX-WS Servlet Endpoint

The basic steps to write a Servlet end point approach is:

  • Write a POJO class
  • Annotate it
  • Configure in web.xml
  • Build and deploy.
  • WSDL file is generated automatically

Make a webapplication/ In eclipse you can make a project which is a dynamic web project.Let's write a POJO bean class which can be invoked by sending a SOAP Request which contains a string and it will return the response "Hello World" + input String.

POJO class:

@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class HelloBean{

@WebMethod
public String hello(String name){
   return "Hello " + name;
  }
}

Note the annotation on the bean class. The class does not implements any interface.
Register this bean as a servlet in the web,xml

<servlet>
      <servlet-name>helloBean</servlet-name>
      <servlet-class>com.oyeJava.HelloBean</servlet-class>

  </servlet>
  <servlet-mapping>
      <servlet-name>helloBean</servlet-name>
      <url-pattern>/hello</url-pattern>
  </servlet-mapping>

Deploy the war in your server and hit the service at:

http://<hostname>/JAX_WS_HelloWorld/hello?wsdl

Please change the host name with your host name and JAX_WS_HelloWorld with your application context.
You can also follow the video:

This will show the wsdl generated:

<definitions name="HelloBeanService" targetNamespace="http://crayom.com/">
//As there are no types so nothing is here
<types/>

//Message denote the payload that travels on wire for request and response.
//At this level we do not distinguish whether it's a request or response
<message name="HelloBean_hello">
   <part name="arg0" type="xsd:string"/>
</message>
<message name="HelloBean_helloResponse">
   <part name="return" type="xsd:string"/>
</message>

//Represents the Bean and the operation.
//Also messages are designated as request and response here
<portType name="HelloBean">
   <operation name="hello" parameterOrder="arg0">
      <input message="tns:HelloBean_hello"/>
      <output message="tns:HelloBean_helloResponse"/>
   </operation>
</portType>

//binding to SOAP over HTTP
<binding name="HelloBeanBinding" type="tns:HelloBean">
   <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="hello">
        <soap:operation soapAction=""/>
           <input>
             <soap:body namespace="http://crayom.com/" use="literal"/>
           </input>
           <output>
             <soap:body namespace="http://crayom.com/" use="literal"/>
           </output>
      </operation>
</binding>

//Service location
<service name="HelloBeanService">
  <port binding="tns:HelloBeanBinding" name="HelloBeanPort">
  <soap:address location="http://127.0.0.1:8080/JAX_WS_HelloWorld/hello"/>
  </port>
</service>

</definitions>

Let's hit the service with soapUI. The SOAP request will look like:

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cray="http://crayom.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <cray:hello>
         <arg0>?</arg0>
      </cray:hello>
   </soapenv:Body>
</soapenv:Envelope>

In place ? if you put oye Java the SOAP response you will get is

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <env:Body>
      <cray:helloResponse xmlns:cray="http://crayom.com/">
         <return>Hello Oye Java</return>
      </cray:helloResponse>
   </env:Body>
</env:Envelope>

Comments

Hi,
I like your example but I wonder can I do the following:

- generate the classes with jaxws (Jdk6) from wsdl to Java i.e. using wsimport
- Create a bean that implements the generated interface, I have done this with stateless bean

- then do the same you do i.e. adding the implemented class in the web.xml.

regards
Alvaro

Hi lalit,
Any resolution to the original question posted by rits.
I am using glassfish and the code has compiled fine. But i get the below e error

exception

javax.servlet.ServletException: PWC1403: Class satish.webservices.codefirst.HelloBean is not a Servlet

root cause

java.lang.ClassCastException: satish.webservices.codefirst.HelloBean cannot be cast to javax.servlet.Servlet

Check if your class is extending Servlet.

SEVERE: Allocate exception for servlet helloBean
java.lang.ClassCastException: hello.CircleFunctions cannot be cast to javax.servlet.Servlet
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1104)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)

Yes, you need some library which supports JAX-WS. Metro or Apache Axis. Or you can use one of the application server like Glassfish or JBoss

 

  Thanx for this gr8 tutorial
But ,when I am trying the HelloBean example, it's throwing the following Exception (But, it's working fine in your video ???) :-

javax.servlet.ServletException: Class mypack.HelloBean is not a Servlet
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Thread.java:619)

Reply

 

Check if your servlet mapped correctly in web.xml Also your class mypack.HelloBean has all the annotations correctly places

Add new comment