I have successfully created Web Service. Tested it and getting the WSDL file also. The client that will use this Web Service is a simple Java class.
I am able to create a jsp client and call the methods of Web Service. But I need to call the Web Service from a Java class.
How do I bind this Java client with Web Service?
The following steps I followed in NetBeans for creating the Java Client...
I created a simple J2SE Application.
Made it a Web Service Client of the WebService made by me.
I'm getting the Web Service References of my WebService.
But I'm not able to call the method of the WebService. Here is the Client file...
package client_package;
public class client {
public static void main(String args[])
{
System.out.println("1");
System.out.println(hello("megha"));
System.out.println("2");
}
private static String hello(String name) {
WS_package.WebService1 service = new WS_package.WebService1(); //package WS_package does not exists
WS_package.WebService1 port = service.getWebService1Port(); //package WS_package does not exists
name = port.hello(name);
return name;
}
}
You could use wsimport tool to generate a client stub files, from command line:
wsimport -keep http://localhost:8080/webservices/helloService?wsdl
then import the generated files and use them like you did above
HelloServiceImplService helloService = new HelloServiceImplService();
HelloService hello = helloService.getHelloServiceImplPort();
There are also some frameworks arround to work with Webservices, like Apache CXF and Apache Axis
Update: Just noticed its an old question, if the OP knew the answer, he should update the topic.
You could try Jersey and its Client API
Related
I am very new to the building of web-services, so please forgive my ignorance.
I have been given some an .wsdl files with some .xsd files that it imports.
I am told that a web-service can be created from the .wsdl file by using wsdl2java from the apache axis2 project.
The web-service I am trying to build is expecting to have data pushed to it and I would like to test it that I have the process right for data to be pushed to a web-service that I created.
The basis for my actions have been from here, but not too sure how much of it is applicable.
I am on a MacOSX but also have access to an ubuntu system too.
the steps I have taken so far are:
cd /directory/of/wsdl/file
wsdl2java.sh -uri tmp.wsdl -d adb -s
This creates a build.xml file and src directory
I then try and run
ant
or
ant jar.client
After this I am not too sure what to do, in order to get the web-server running so that I could test it...any suggestions would be greatly appreciated.
Thanks in advance.
In SOAP web service:-
The basic concept in web service is that it has consumer and producer.
A consumer is one which consumes a web service and a producer is one which produces a web service. A producer publish its service so that consumer can consume it. It basically publishes a wsdl file so that you can create a client code or jar out of it and can directly call it from your code. You can use soap UI to call the web service directly as well. If you are looking for generating producer code from wsdl as well it will not be good enough since it will not provide business logic to you and you need to implement it by yourself. This is not a recommended approach. Generally first java implementation is written and based on it a wsdl is created from which client jars are created for the clients to use the web service in their code. For directly testing the producer soapui is used.
If you want to create producer it is a straight forward process. Need to create a dynamic project in eclipse-->create a class-->use #WebService(serviceName="xyz") on class and similarly on method level define #WebMethod. Deploy it as run on server and you are done with your Hello World Web service producer.
For creating the client:-
Lets take an example of a published wsdl on the net as :-
http://www.webservicex.net/geoipservice.asmx?WSDL
First you need to create the client jar or java classes as :-
wsimport -keep -s C:\wsdl http://www.webservicex.net/geoipservice.asmx?WSDL
Look at the documentation or look at the service name in the wsdl.
It will be GeoIPService.
Now in your class call the webservice method as:-
package com.soap.client;
import net.webservicex.GeoIP;
import net.webservicex.GeoIPService;
import net.webservicex.GeoIPServiceSoap;
public class SoapWebServiceClient {
public static void main(String[] args) {
GeoIPService ipService = new GeoIPService();
GeoIPServiceSoap gp = ipService.getGeoIPServiceSoap();
GeoIP ip = gp.getGeoIP("117.198.208.1"); //google.com
System.out.println(ip.getCountryName());
}
}
Now similarly for local wsdl you can create classes and jars by
using axis2 or simply wsimport
Put your wsdl and schemas in a folder as shown below:-
C:\wsdl>wsimport -keep -s C:\wsdl C:\wsdl
C:\wsdl>wsimport -clientjar client.jar C:\wsdl
It will create a client for you. Look at the service name and similarly can test the deployed service from java code as shown above.
For testing using soapui you use need to download it and create a new soap project. Give any name and browse to your local drive where all the schema and wsdl is present. It will create all the requests for your. You need to fill in the values in the request parameters ("?") and run the service. If everything went well it will display a result.
Note:-
wsimport is a command line tool for the JAX-WS reference implementation. The JAX-WS RI uses JAXB for data-binding.
Axis2 merely implements the JAX-WS API to some extent, so the Java artifacts generated can be quite different compared to those generated by the JAX-WS RI. Also Axis2 doesn't use JAXB but instead offers the choice of ADB (default), Apache XmlBeans, or JiBX for data-binding. The most popularly used are either xmlbeans or JAXB.
You lookup the wsdl file from publishing URL and reverse engineer the webservice to generate the types so use wsimport
wsimport -d . -p servicesource -keep tmp.wsdl
I'm trying to create web services functionality linked to my current working java application and struggling to do it. Any ides ?
At line:
TopUpApp application = new TopUpApp();
I'm creating a new instance from the other class , but it doesn't seem to work when I test web service. Giving false as a return where it should be true.
Below is the code for web service.
package org.me;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;
/**
*
* #author KS
*/
#WebService(serviceName = "DOAws")
#Stateless()
public class DOAws {
/**
* Web service operation
*/
#WebMethod(operationName = "login")
public boolean login(#WebParam(name = "username") int username, #WebParam(name = "password") String password) {
TopUpApp application = new TopUpApp();
return application.authenticate(username, password);
}
}
It works fine and I get true as a response when I run it in new main class this way (not a web service):
public class NewClass {
public static void main(String []args){
TopUpApp application = new TopUpApp();
System.out.println(application.authenticate(12345, "12345"));
}
}
To deploy your application as a web service, you need to do a little bit more than what is necessary for a normal java application.
I short, what you need to do is to deploy your application on a web server that is also able to deploy java web applications. Many such web servers exists, a couple of popular alternatives are Tomcat and Jetty.
The normal thing to do is to install the web server on the machine that is supposed to handle the requests. Next you need to deploy your application on that server. To do that, you will need to do a couple of things:
Package your application as a war (web archive). How to do this is quite straightforward, but involves a few steps. You should be able to figure this out by yourself using google.
Deploy your application to the web server. This is usually done by copying the war file to the web applications directory of your web server. E.g. the webapps directory under the tomcat installation directory.
When this is done, you just need to make sure that your web server is up and running, and you should be able to access your web services.
For customers to create apache cxf client of your web service, if they use JaxWsProxyFactoryBean, they need the endpoint address and the SEI code. About providing endpoint address there is no problem, but how you provide the SEI ? You just export the interface within a jar and give the jar to the customer ? Each time you change the SEI, you must contact with all customers and give the jar again ?
I mean the IDataService interface in below example:
public static void main(String[] args) throws SomeException {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(IDataService.class);
factory.setAddress("http://localhost:8080/WSTest/services/dataService");
IDataService client = (IDataService) factory.create();
}
Ideally you should just be giving them the WSDL of the service - WSDL should the contract here using which they can generate their own set of templates using whatever tool is available to them - wsdl2java etc.
If the client is an internal to your company, then yes, you can as well create a thin project with just the interfaces, types, messages, package it as a jar and provide the jar via some internal repository, assuming that your interface does not too often and even if it changes, that it is published afresh to the internal repository.
I'm new to Java. I have a Java project. It runs perfectly on my Windows 7 machine. I want to use some of the functionalities of this project as Web Services to be able to use them in my Silverlight app. Both the Silverlight app and this Java project would be on the single server machine. The problem I have is that when I right click on the project there's no Web Service in the New menu. What should I do to add a web service to my project? Thanks.
Based on the article I linked in the comments above :: http://www.ibm.com/developerworks/webservices/tutorials/ws-eclipse-javase1/index.html
With the JWS annotations you can setup a Web Service in your java application to expose some of its functionality. There is no extra libraries needed. The below examples were written with Java 6.
An example of Defining your web service :
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService
public class MyWebService {
#WebMethod
public String myMethod(){
return "Hello World";
}
}
Note the 2 annotations of #WebService and #WebMethod. Read their API's which are linked and configure them as required. This example will work without changing a thing
You then only need to setup the Listener. You will find that in the class javax.xml.ws.Endpoint
import javax.xml.ws.Endpoint;
public class Driver {
public static void main(String[] args) {
String address = "http://127.0.0.1:8023/_WebServiceDemo";
Endpoint.publish(address, new MyWebService());
System.out.println("Listening: " + address);
}
}
Run this program and you will be able to hit your web service using http://127.0.0.1:8023/_WebServiceDemo?WSDL. At this point it is easy to configure what you want to send back and forth between the applications.
As you can see there is no need to setup a special web service project for your use.
OK, I am developing a program which will be deployed to lots of machines (Windows, Linux, AIX, z/Linux, openVMS, etc.). I want that application to contain a SOAP web service, but I don't want to bundle tomcat or run a separate service for the services (I want them in the same process as the rest of the application).
Basically what I'm looking for is something where I can define a class (say WebServices). I'm OK with writing WSDL or any other kind of service description as well. The I want something like this:
SOAPServer server = makeMeASoapServer();
//do config on the server
server.add(new WebService(...));
server.listen(port);
Obviously the names and parameters will be different.
I've been looking at Axis, and it seems like it provides this, but I don't know what classes I need to use. Am I crazy in wanting this kind of behavior? I can't believe more people aren't looking for this, I do this all the time with embedded web services within .NET clients.
Seems jdk 6.0 already comes with a jax-ws implementation, and a little server you can embed.
I havn't figured out all the pieces but here's a start:
mkdir -p helloservice/endpoint/
helloservice/endpoint/Hello.java :
package helloservice.endpoint;
import javax.jws.WebService;
#WebService()
public class Hello {
private String message = new String("Hello, ");
public void Hello() {}
public String sayHello(String name) {
return message + name + ".";
}
}
helloservice/endpoint/Server.java:
package helloservice.endpoint;
import javax.xml.ws.Endpoint;
public class Server {
protected Server() throws Exception {
System.out.println("Starting Server");
Object implementor = new Hello();
String address = "http://localhost:9000/SoapContext/SoapPort";
Endpoint.publish(address, implementor);
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
Build the thing:
mkdir build
javac -d build helloservice/endpoint/*java
$JAVA_HOME/wsgen -d build -s build -classpath . helloservice.endpoint.Hello
Run the thing:
java -cp build helloservice.endpoint.Server
Somethings running on http://localhost:9000/SoapContext/SoapPort now.
You can get the wsdl on http://localhost:9000/SoapContext/SoapPort?WSDL
Havn't gotten around to making a client yet..
In addition to nos's great answer, I found a class in Apache axis called SimpleHTTPServer which I'm pretty sure does the same thing but only requires Java 1.5 for those of you stuck with 1.5
I'm not going to explore it since I'm going to use the other solution, so I haven't actually verified it does what I think it does, but I'm pretty sure it does.
Most(/all?) Java SOAP server implementations provide a Servlet (the javax.xml.ws.Endpoint approach in another answer does look a bit simpler though...). Some SOAP implementations you could consider are: Apache CXF: cxf.apache.org, Apache Axis2: ws.apache.org/axis2/ or Spring Web Servies: static.springsource.org/spring-ws/site/ .
The most popular embedded Java web server seems to be Jetty, you can configure it either programatically (using plain Java or Spring beans) or using a custom XML format.
To address the main question directly, another approach would be to go with Jetty's embedded server. See this link for details. The links from the aforelinked page help you understand both the simple web server (i.e., one that serves up static pages; though I am fully aware "simple" is a horribly vague term wrt web servers) and the web server that helps you deploy web services.