Publishing Java based WebServices using JAX-WS in JDeveloper 11g

Recently I have been doing quite some work with WebServices – calling them and implementing them, from and in both PL/SQL and Java. And in conjunction with BPEL and ESB. Today, I did some quick tests using JDeveloper 11g (TP4) and more specifically JAX-WS. It turns out to be very simple to publish (and test) a Java based WebService – a matter of minutes.

This article will very briefly demonstrate how to get a WebService up and running, based on a simple Java Class.....

 

Steps:

1. create new JDeveloper Application

2. create empty project – in my case JAXWSQuizMaster

3. Add a number of libraries to the project

Publishing Java based WebServices using JAX-WS in JDeveloper 11g 

4. Implement Java Class that we want to publish as WebService

For example:

package nl.amis.quiz;

public class QuizMasterService {

public QuizQuestion getOneQuizQuestion(String topic) {
QuizQuestion q = new QuizQuestion();
q.setQuestion("What is the capital of France?");
q.setAnswer("Paris");
return q;
}
}

and:

package nl.amis.quiz;

public class QuizQuestion {
private String question;
private String answer;
private String hint;
private int difficulty;


public void setQuestion(String question) {
this.question = question;
}

public String getQuestion() {
return question;
}

public void setAnswer(String answer) {
this.answer = answer;
}

public String getAnswer() {
return answer;
}

public void setHint(String hint) {
this.hint = hint;
}

public String getHint() {
return hint;
}

public void setDifficulty(int difficulty) {
this.difficulty = difficulty;
}

public int getDifficulty() {
return difficulty;
}
}

 

5. Add JAX-WS annotations to the Java Class

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(name = "QuizMaster", serviceName = "QuizMasterService", portName = "QuizMasterPort")
public class QuizMasterService {

@WebMethod
public QuizQuestion getOneQuizQuestion(String topic) {
 

 

Note: after adding these annotations, we can look at the WSDL that will be dynamically generated for this class by selecting Show WSDL from the right mouse button menu:

Publishing Java based WebServices using JAX-WS in JDeveloper 11g

I just learned a better way of getting up the web.xml – please ignore step 6A that I first included in this article.

6. Create WebService for the QuizMasterService:

From the New Gallery, Business Tier – Web Services select the option Java Web Service. In the wizard that is started, select the QuizMasterService class as the Component to publish. Accept all defaults in the remainder of the wizard, then press Finish. When done, the web.xml will have been generated:

    <servlet>
<servlet-name>QuizMasterPort</servlet-name>
<servlet-class>nl.amis.quiz.QuizMasterService</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>QuizMasterPort</servlet-name>
<url-pattern>/quizmasterport</url-pattern>
</servlet-mapping>

 

6A. Note: this was orginally in this article. While not necessarily wrong, it describes an inferior way of creating the web.xml file!

Configure a servlet in the web.xml file for the Port

Go to the New Gallery, From the Web Tier node, Servlets category choose HttpServlet. Choose Servlet 2.5.

Publishing Java based WebServices using JAX-WS in JDeveloper 11g

Press Next

Publishing Java based WebServices using JAX-WS in JDeveloper 11g

Press Finish.

Now we need to slightly edit the web.xml file, as we do not really want to map to a class called QuizMasterServiceServlet. Just strip the Servlet from the class name. The web.xml should now contain:

    <servlet>
<servlet-name>QuizMasterService</servlet-name>
<servlet-class>nl.amis.quiz.QuizMasterService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>QuizMasterService</servlet-name>
<url-pattern>/quizmasterservice</url-pattern>
</servlet-mapping>

 Feel free to delete the class QuizMasterServiceServlet that this wizard has generated.

7. Test (or Run) WebService

On the RMB menu on QuizMasterService class, select the option Test Web Service:

Publishing Java based WebServices using JAX-WS in JDeveloper 11g

The WebService is deployed – its WSDL can for example be accessed from a browser at in this case: http://localhost:8988/JAXWS-JAXWSQuizMaster-context-root/quizmasterservice?WSDL .

The JDeveloper 11g WebService Tester is displayed:

Publishing Java based WebServices using JAX-WS in JDeveloper 11g

Enter the value for the input parameter (not that it makes any difference, but with a more advanced service implementation – that is: better fleshed out getOneQuizQuestion() method in the QuizMasterService class – it would make a difference) and press the Send Request button.

The WebService is now invoked and the Java Class can do its work. The result:

Publishing Java based WebServices using JAX-WS in JDeveloper 11g 

Note: the WebService can also be tested by accessing it from a browser at the ‘servlet url’ (in this case http://localhost:8988/JAXWS-JAXWSQuizMaster-context-root/quizmasterservice). OC4J will turn this request over to the built in WebService invocation facility that shows up like this:Publishing Java based WebServices using JAX-WS in JDeveloper 11g

 

And show the reply like this:

Publishing Java based WebServices using JAX-WS in JDeveloper 11g

Summary

We have seen how using a few JAX-WS annotations allows us to very rapidly turn a Java Class into a WebService. JDeveloper 11g has support for JAX-WS and for locally deploying and tesing the WebService (as well as dynamically generating the WSDL). JAX-WS compliant JEE containers such as WebLogic 10.3 and OC4J 11g can be used to actually deploy and run this WebService.

3 Comments

  1. Rakesh November 18, 2009
  2. Hegyvari Krisztian November 6, 2008
  3. Edwin Biemond July 6, 2008