Tuesday, August 19, 2014

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 web application. 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:

2 comments:

  1. I get - java.lang.ClassCastException: biz.dss.HelloBean cannot be cast to javax.servlet.Servlet
    on GlassFish server

    ReplyDelete
    Replies
    1. This example was tried on JBoss server. However looking to the exception you are getting, it looks like configuration issue.

      Delete