| Explanation Point: Create a Web Service:The point to drive home here is that a web service is nothing but a POJO with the @WebService annotation. |
2. Create a new Java Class named Hello in the endpoint package. Note, you could use the web service wizard to do this, but I think it drives the POJO point home if you just create a class and annotate it with @WebService. Your complete class will look as follows:
| Explanation Point: If you use code completion for "@WebService", the import will be added for you. |
package endpoint;
import javax.jws.WebService;
@WebService
public class Hello {
public String sayHello (String name) {
return "Hello " + name;
}
}
3. Deploy and test the web service from NetBeans (expand the Web Services folder and right-click the HelloWorld node).
4. Show the service can also be viewed and tested from the Java EE 5 SDKs admin console.
5. Create a new General Java Application named SpeakToMe 6. Add a Web Service Client, using the WSDL URL to the Hello Service (you can get this from the properties of the HelloWorld Web Services node, or the Test Web Service page). Put it in a package named wsclient. Running the Web Service Client wizard is equivalent to running wsimport from the command line, which creates the client artifacts.
7. Switch to the File tab and show the generated client classes.
8. Add the following to the main method:
HelloService service = new HelloService();
Hello port = service.getHelloPort();
System.out.println(port.sayHello(args[0]));
9. Set an argument in the project properties and run the client.
10. Create a new EJB Module named HelloWorldEJB.
11. Copy the Hello POJO from the web module and paste it to the ejb module.
12. Add the Stateless annotation to the Hello POJO.
| Explanation Point: Since the web service will have the same endpoint URL "HelloService/Hello" as the HelloWorldWeb web service, we need to undeploy the HelloWorldWeb web application. |
13. Go to the Services window and select Servers->Glassfish v2->Applications->Web Applications->HelloWorldWeb, right-click and select Undeploy.
14. Deploy the HelloWorldEJB project.
15. Right-click on the HelloWorldEJB->Web Services->HelloService node and select Test Web Service.
16. Test the web service by typing a name in.
| Explanation Point: Here we'll collect and persist statistics on who we've said hello to. |
17. On the HelloWorldEJB project, right-click and choose "new->Persistence Unit".
18. Select "jdbc/sample" for the "Data Source:" and make sure "Table Generation Strategy" is set to "Create".
Create the Persistence Unit
Create the Entity Class for Persisting the Statistics
19. Create a new Java Class named Person in the endpoint package. Like with the Web Service, we're using a POJO to show how easy all this really is.
20. Add two fields, name and visits. Annotate the class with @Entity and the name field with @Id. Note the warning given by trying to use "name" as a field name. Change it to visitor instead. The final class should look as follows:
@Entity
public class Person implements Serializable {
@Id
private String visitor;
private int visits;
/** Creates a new instance of Person */
public Person() {
}
public Person(String name) {
this.visitor = name;
}
public int incrementVisit() {
return visits++;
}
}
| Explanation Point: Now we will update the Web Service to Persist to the Person Entity |
21. Add a persistence context, @PersistenceContext private EntityManager em; and the following code to sayHello, so the class looks like the following:
@WebService()
@Stateless()
public class Hello {
@PersistenceContext private EntityManager em;
public String sayHello (String name) {
Person p = em.find(Person.class, name);
if (p == null) {
p = new Person(name);
em.persist(p);
}
p.incrementVisit();
return "Hello " + name;
}
}
22. Deploy the project and show that the Person table is created (as specified by our persistence unit) during deployment time.
23. Run the web service tester with the same name twice and show the new table and the data in the table.
24. Create a new web->web application project named "CustomerDB".
25. Right-click on the project and select "new->Entity Classes From Database...".
26. Choose the "jdbc/sample" database for the Data Source and the Customer table for the Selected Tables:.
27. Press "next" and enter entity for the package name.
28. Press the "Create Persistence Unit..." button.
29. Take the defaults and press the "Create" button.
30. Press "Finish".
31. right-click on the "entity" package and choose "new->RESTful Web Services from Entity Classes" (note that this might be under "Other:->web services" if never used).
32. Click "Next", select "Add Add >>" and select "next", take the defaults and press "Finish".
33. Right-click on the project node and choose "_Test RESTful Web Services".
34. Show the xml GET of the customer table.
| javaee5_1.jpg | ![]() |
22304 bytes |
| javaee5_2.jpg | ![]() |
62076 bytes |
| javaee5_3.jpg | ![]() |
57485 bytes |
| visualweb1.jpg | ![]() |
37537 bytes |