This post shows an example to create a web service that generates a response based on information received from the client. The environment is as follows:
1. EJB Container i.e. GlassFish Server
2. A stateless session bean i.e. HelloServiceBean
3. NetBeans IDE
1. EJB Container i.e. GlassFish Server
2. A stateless session bean i.e. HelloServiceBean
3. NetBeans IDE
HelloServiceBean is the endpoint implementation class. It's a stateless session bean that implements a single method: sayHello. The web service implementation class must have following requirements.
- The class must have annotated with javax.jws.WebService or javax.jws.WebServiceProvider annotation.
- The implementation class may explicitly reference ans SEI through the endpointInterfacce element of the @webService annotation but it is not mandatory. If nothoing specified, the SEI is implicitly defined for the implementation class.
- The business methods must be public and must not be declared static or final.
- The business methods that are exposed to web service clients must be annotated with javax.jws.WebMethod.
- The business methods that are exposed to web service clients must have JAXB-compatible parameters and return types. See this.
- The implementing class must not be declared final and abstract.
- The implementing class must have a default public constructor.
- The implementing class must be annotated with @Stateless.
- The implementing class must not define the finalize method.
- The implementing class may use javax.annotation.PostConstruct or javax.annotation.PreDestroy annotations on its methods for life cycle event callbacks.
The @PostConstruct method is called by the container before the implementing class begins responding to web service clients.The @PreDestroy method is called by the container before the the endpoint class is removed from the operation.
1. Create a simple Web Application Project named HelloService and select GlassFish server and click Finish.
2. Create a class HelloServiceBean annotated with @WebService and implements the sayHello method annotated with @WebMethod. The source code of HelloServiceBean is as follows:
package com.web.helloservice;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
*
* @author Khan.Ashik
*/
@Stateless
@WebService
public class HelloServiceBean {
private String message = "Hello, ";
public HelloServiceBean() {
}
@WebMethod
public String sayHello(String name)
{
return message + name + ".";
}
}
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
*
* @author Khan.Ashik
*/
@Stateless
@WebService
public class HelloServiceBean {
private String message = "Hello, ";
public HelloServiceBean() {
}
@WebMethod
public String sayHello(String name)
{
return message + name + ".";
}
}
▼ Test the web service without a a client
1. Open GalssFish Server administration console by opening following URL in a web browser.
http://localhost:4848/
2. In the left pane of the Administration Console, select the Applications node.
3. In the Applications table, click HelloService.
4. Click View Endpoint and click Test link page. If it is not present then go to following URL directly.
http://localhost:8080/HelloServiceBeanService/HelloServiceBean?Tester
5. Under Methods, type name as parameter and click sayHello button.
6. The sayHello Method invocation page opens. Under Method Returned, The output is printed. And SOAP Request and SOAP Response XML is printed.
If you want to learn how to create web service client and more deeply about web service, please visit my another post of Create Web Service (JAX-WS), Web Service Client and call the Web Service Using Client.
And that's all about this post.
No comments:
Post a Comment