user3117840
user3117840

Reputation:

How to access Webservices in Client Application?

I am working on Web-services.I Generated WSDL file for my web application.Here based on user name I am getting User details. And I want to run this Web-Service in another application.

That's why I generated .java files using d:>wsimport -keep -s src Wsdl url and I wrote Client Class to get details. But here I got an exception. how can I overcome this.

Below is my UserName.java class. By using this class I generated wsdl file.

public class UserName implements UserId {
String result = null;

@Override
public String getName(String name) {
    try {

        String name1 = name;

        JAXBContext jaxbContext = JAXBContext.newInstance(User.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        File file = new File("D:/MyWorkspace/JaxWsWithxml/User.xml");
        User userUnmarshal = (User) jaxbUnmarshaller.unmarshal(file);

        List<User> list = userUnmarshal.getUser();
        for (User user : list)
            if (user.getName().equals(name1)) {
                result = user.getName();
                System.out.println("Id " + " " + user.getId() + "  "+ user.getName());

            }

    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return result;
}
}

Below is UserId.java Interface

 public interface UserId
{
 public String getName(String name);
}

Below is Client.java In another application I got .java files using WSDL file

public class Client {

public static void main(String[] args) {
    // TODO Auto-generated method stub
String name1="raj";
int id;
String a=name(name1);
System.out.println("Name is"+a);

}
public static String name(String name1)
{
    UserNameService us=new UserNameService();
    UserName userNmae=us.getUserName();
    return userNmae.getName(name1);
}

 }

Here UserNameService is service name in WSDL file and UserName port name in WSDL file.

My Exception is:

Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException:
HTTP transport error: java.net.ConnectException: Connection refused: connect
at com.sun.xml.internal.ws.transport.http.client.HttpClientTransport.getOutput(Unknown Source)
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(Unknown Source)
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(Unknown Source)

This is my exception how can I solve Thank you...

Upvotes: 1

Views: 108

Answers (2)

Kristina
Kristina

Reputation: 23

At first you need to check whether the deployed service is running fine.Since, you are facing connection refused exception, I guess the server is not running properly. Or may be your client application can't access the web-service URL. Since, you already have a WSDL, you can also invoke a web-service by directly referencing the WSDL from java code/ JAX-WS Client.

Upvotes: 1

Prabha
Prabha

Reputation: 705

HTTP transport error means your application not running currently. First you should run your Web-service application first and Then execute your client code. It will be work fine. No changes required in your code.

Upvotes: 0

Related Questions