Implementing a JAX WS web service

Hi, I would be implementating webservices, I would like to know about the some of the
content of the webservices like

• Implementing a SEI
• @weservice and @webmethod annotations
• Understanding server side generated artifacts and JAX-B under the hood
• Understanding xml data types - marshelling/unmarshelling
• Understand wsimport and wsgen utilities

can i get the information..

Thanks
roy_sanudeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
can i get the information..
Yes you can. All this is easy available from Google searches. If you can be a little bit more specific on the issue that you are facing, or the specific items that you aren't understanding, tell us and we could provide more targeted help.
0
roy_sanudeveloperAuthor Commented:
can you tell me about  wsimport and wsgen utilities.
what i understand that with bottom approach on the java file one can generate wsdl files through jax-ws .

Is these are command line to generate  wsdl files..
I am not clear on these...
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Is these are command line to generate  wsdl files..
There might be utilities to do this but the usual way, with JAX-WS, is the actual runtime generates the WSDL for you when you start up the server part of the web-services.

Take this very simple example...


Start with an interface that describes your service.

HelloWorld.java (which contains JAX-WS annotations to declare it as a service and the name of a parameter)
package demo.jaxws.model;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name = "text") String text);
}

Open in new window

Then you implement the above interface, in order to provide whatever logic the web service performs.

HelloWorldImpl.java
package demo.jaxws.model;

import javax.jws.WebService;

@WebService(endpointInterface = "demo.jaxws.model.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }
}

Open in new window

Now you can have some simple code that starts a server and publishes your web service via that server. "Endpoint.publish(...)" does all that magic in one call.

Server.java
package demo.jaxws.server;

import javax.xml.ws.Endpoint;

import demo.jaxws.model.HelloWorldImpl;

public class Server {

    public static void main(String args[]) throws Exception {
        System.out.println("Starting Server");
        Endpoint.publish("http://localhost:9000/helloWorld", new HelloWorldImpl());
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);

        System.out.println("Server exiting");
        System.exit(0);
    }
}

Open in new window

Once the server is up and running, you can navigate to this address in a browser (http://localhost:9000/helloWorld?wsdl) and JAX-WS will generate the WSDL on the fly and return it to your browser screen.

However, retrieving this manually might not be necessary because JAX-WS can create you a proxy to your web service by automatically parsing the WSDL that is published by the server. See this client code for an example of consuming your web service...
Client.java
package demo.jaxws.client;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import demo.jaxws.model.HelloWorld;

public class Client {
    public static void main(String[] args) throws MalformedURLException {
        Service service = Service.create(new URL("http://localhost:9000/helloWorld?wsdl"), new QName("http://model.jaxws.demo/", "HelloWorld"));
        HelloWorld client = service.getPort(HelloWorld.class);
        String reply = client.sayHi("HI");
        System.out.println("Server said: " + reply);
    }
}

Open in new window

0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

roy_sanudeveloperAuthor Commented:
Hi mccarl,

Thank you so much providing the info, but my question if i want to  generate  JAX-WS portable artifacts  using the command wsgen. How i should do that.
can you provide me the  syntax of it.  well tried using the command prompt it give me
"class not found"

" Let us say i am using a calculator.java class say

package com.theopentutorials.ws.calc;

import javax.jws.WebService;
@WebService
public class Calculator {
      
      
          public int add(int a, int b) {
              return (a + b);
          }
      
          public int sub(int a, int b) {
              return (a - b);
          }
      
      }

error as

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.


C:\Users\roy\workspace\WebServ>wsgen -cp bin -d bin com.theopentutorials.ws.
calc.Calculator
Class not found: "com.theopentutorials.ws.calc.Calculator"
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
C:\Users\roy\workspace\WebServ>wsgen -cp bin -d bin com.theopentutorials.ws.
calc.Calculator
For that to work the following file/folders must exist...
C:\Users\roy\workspace\WebServ\bin\com\theopentutorials\ws\calc\Calculator.class

Open in new window

If that is not the case, ie. if your Calculator.class file is somewhere else, then you need to set the correct classpath (-cp option) to point to the directory that contains you "com" folder (which is the one at the start of the following path, com\theopentutorials\ws\calc\Calculator.class).

If the above doesn't make sense, just post the FULL path to your Calculator.class file and I can then say what your classpath should be.
0
roy_sanudeveloperAuthor Commented:
it has generated 4 files such as  Add.java, AddResponse.java, Sub.java, SubResponse.java

In Add.java  looks like these

==================================================================

package com.theopentutorials.ws.calc.jaxws;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "add", namespace = "http://calc.ws.theopentutorials.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "add", namespace = "http://calc.ws.theopentutorials.com/", propOrder = {
    "arg0",
    "arg1"
})
public class Add {

    @XmlElement(name = "arg0", namespace = "")
    private int arg0;
    @XmlElement(name = "arg1", namespace = "")
    private int arg1;

    /**
     *
     * @return
     *     returns int
     */
    public int getArg0() {
        return this.arg0;
    }

    /**
     *
     * @param arg0
     *     the value for the arg0 property
     */
    public void setArg0(int arg0) {
        this.arg0 = arg0;
    }

    /**
     *
     * @return
     *     returns int
     */
    public int getArg1() {
        return this.arg1;
    }

    /**
     *
     * @param arg1
     *     the value for the arg1 property
     */
    public void setArg1(int arg1) {
        this.arg1 = arg1;
    }

}

========================================

AddResponse.java looks like these below

==========================================

package com.theopentutorials.ws.calc.jaxws;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "addResponse", namespace = "http://calc.ws.theopentutorials.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "addResponse", namespace = "http://calc.ws.theopentutorials.com/")
public class AddResponse {

    @XmlElement(name = "return", namespace = "")
    private int _return;

    /**
     *
     * @return
     *     returns int
     */
    public int getReturn() {
        return this._return;
    }

    /**
     *
     * @param _return
     *     the value for the _return property
     */
    public void setReturn(int _return) {
        this._return = _return;
    }

}
===============================================================

My question to you , How i am going to use these files,
how i need to publish these class as a web service endpoint ?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Sorry about the delay in getting back to you, I didn't notice your post in my emails...

Bottom line is that you don't NEED to use those classes at all, in order to publish your web service. Maybe I wasn't clear in my earlier post but the reason that I posted that code was to show you just how you do publish classes as web service endpoints.

how i need to publish these class as a web service endpoint ?
I know this may look too simple, but the following code will do just that.
package com.theopentutorials.ws.calc;

import javax.xml.ws.Endpoint;

public class WebService {
    
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:7888/webservice", new Calculator());
    }
}

Open in new window

When you run the above, it will create a HTTP server listening on port 7888 and if you then navigate (in a browser) to http://localhost:7888/webservice/Calculator?wsdl, JAX-WS will generate the WSDL and show it to you.

You can then write a client that can consume those services. In this case you can't do it as simply as I had shown in the earlier post, because your service implementation, ie. the Calculator class, isn't based of an interface. But THIS is where those generated classes can be used... to create a client app that consumes the web service. Have a look at the following client code.
package com.theopentutorials.ws.calc;

import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.namespace.QName;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;

import com.theopentutorials.ws.calc.jaxws.Add;
import com.theopentutorials.ws.calc.jaxws.AddResponse;

public class Client {

    public static void main(String[] args) throws Exception {
        // Create a 'Service' object that refers to the service published by the server, and which is described by the WSDL
        Service service = Service.create(new URL("http://localhost:7888/webservice/Calculator?wsdl"), new QName("http://calc.ws.theopentutorials.com/", "CalculatorService"));
        
        // Create a dispatch object that can be used to call the 'add' method of the service (To be able to call the 'sub' method you would need to list those classes too in the below JAXBContext)
        Dispatch<Object> dispatch = service.createDispatch(new QName("http://calc.ws.theopentutorials.com/", "CalculatorPort"), JAXBContext.newInstance(Add.class, AddResponse.class), Mode.PAYLOAD);
        
        // Create a new request that will call the 'add' method
        Add request = new Add();
        
        // Set the parameters of the request
        request.setArg0(5);
        request.setArg1(2);
        
        // Call the 'add' method of the service and return the result
        AddResponse result = (AddResponse) dispatch.invoke(request);
        
        // Print the 'Return' value of the response message
        System.out.println("Result = " + result.getReturn());
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Services

From novice to tech pro — start learning today.