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

3

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

 

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

For example:

package nl.amis.quiz;<br /><br />public class QuizMasterService {<br /><br />    public QuizQuestion getOneQuizQuestion(String topic) {<br />    QuizQuestion q = new QuizQuestion();<br />    q.setQuestion(&quot;What is the capital of France?&quot;);<br />    q.setAnswer(&quot;Paris&quot;);<br />       return q;<br />    }<br />}<br /> <br />

and:

package nl.amis.quiz;<br /><br />public class QuizQuestion {<br />    private String question;<br />    private String answer;<br />    private String hint;<br />    private int difficulty;<br />    <br /><br />    public void setQuestion(String question) {<br />        this.question = question;<br />    }<br /><br />    public String getQuestion() {<br />        return question;<br />    }<br /><br />    public void setAnswer(String answer) {<br />        this.answer = answer;<br />    }<br /><br />    public String getAnswer() {<br />        return answer;<br />    }<br /><br />    public void setHint(String hint) {<br />        this.hint = hint;<br />    }<br /><br />    public String getHint() {<br />        return hint;<br />    }<br /><br />    public void setDifficulty(int difficulty) {<br />        this.difficulty = difficulty;<br />    }<br /><br />    public int getDifficulty() {<br />        return difficulty;<br />    }<br />}<br /> <br />

 

5. Add JAX-WS annotations to the Java Class

import javax.jws.WebMethod;<br />import javax.jws.WebService;<br /><br />@WebService(name = &quot;QuizMaster&quot;, serviceName = &quot;QuizMasterService&quot;, portName = &quot;QuizMasterPort&quot;)<br />public class QuizMasterService {<br /><br />    @WebMethod<br />    public QuizQuestion getOneQuizQuestion(String topic) {<br />&nbsp;

 

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:

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:

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

 

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.

Press Next

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:

    &lt;servlet&gt;<br />        &lt;servlet-name&gt;QuizMasterService&lt;/servlet-name&gt;<br />        &lt;servlet-class&gt;nl.amis.quiz.QuizMasterService&lt;/servlet-class&gt;<br />    &lt;/servlet&gt;<br />    &lt;servlet-mapping&gt;<br />        &lt;servlet-name&gt;QuizMasterService&lt;/servlet-name&gt;<br />        &lt;url-pattern&gt;/quizmasterservice&lt;/url-pattern&gt;<br />    &lt;/servlet-mapping&gt;<br /><br />

 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:

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:

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:

 

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:

 

And show the reply like this:

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.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

3 Comments

  1. Hi,
    Do you know how to call this JAX-WS from ADF context by storing the same in connection.xml?
    Please provide the example, if you have?

  2. Hi,

    Did you not by any change make webservice from a pl/sql package? Using the built-in generators I can generate jax-rpc services which cannot be deployed due to some mystic serialization symbol not found on Sun Application Server. I was looking around to see if somebody knows a way to generate jax-ws services directly from pl/sql.

    Cheers,

    Krisztian

  3. Hi Lucas,

    nice article, I think this should work in 10.1.3 too , I already did this with a adobe flex / blazeds example with jdev 10.1.3.3 ( http://biemond.blogspot.com/2008/04/flex-and-web-services-using-blazeds.html )

    By the way. Rest support is also very easy
    add @Deployment(restSupport = true) to the code and you have a rest web service too.

    @WebService(name = “QuizMaster”, serviceName = “QuizMasterService”, portName = “QuizMasterPort”)
    @Deployment(restSupport = true)
    public class QuizMasterService {

    and you can test this rest ws with the following url /JAXWS-JAXWSQuizMaster-context-root/getOneQuizQuestion?topic=geography