How to create a java client that consumes a webservice? - java

Well, I've created a webservice that i can find accessing locally at:
http://127.0.0.1:8080/myapp/WSPA?wsdl
Now i need to test my webservice by calling it from another java application to verify it its working fine. I've seen that its working using WebService Client from JBoss plugin on eclipse. But the problem is that i have a method wich recieves a list of SoapFile containing a String and array of bytes. And i need to verify if its working.
#XmlType
public class SoapFile implements Serializable {
private String fileName;
private byte[] fileData;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public byte[] getFileData() {
return fileData;
}
public void setFileData(byte[] fileData) {
this.fileData = fileData;
}
}
I've not found how to create a simple webservice client that consumes that service to test.
I would like some direction for this... Tutorial or some website that explains how to make it step by step.
How can i create a java client for this webservice?

Igor, just use wsimport with your web service url - you will get generated classes for WebService and then just invoke service in that way:
ServiceGenerateFromWSImportWhichIsTheSameAsYour iService =
new ServiceGenerateFromWSImportWhichIsTheSameAsYour().
getServiceGenerateFromWSImportWhichIsTheSameAsYourPort();
// now on iServie instance you can invoke method from your webservice
// but you have to use stub classes generated by wsimport
iService.myMethodWhichGetFileList(List<SoapFileStubGeneratedClass> sopaFiles);
And wsimport is standard java tool in jdk instal folder
More on wsimport tool you can find here:
wsimport doc
Using wsimport in your case will be:
wsimport -p generated_classes -s generated_sources http://127.0.0.1:8080/myapp/WSPA?wsdl
and you will find .class files in folder generated_classes and .java files in folder generated-sources

Do you have a WSDL file. If yes then you can use IDE like eclipse to generate client stub.
Below link will also be a good place to start
http://docs.oracle.com/cd/E17802_01/webservices/webservices/reference/tutorials/wsit/doc/Examples_glassfish6.html

A "Hello World" Tutorial with wsimport for Jax-WS can you find here
Tim

Related

Read Data from File in a Java Class Library Without Main Method

I have a set of Java console applications which act as my system's backend Services and which I package each into their own Jars. I also have written a class library (e.g. CoreLibrary) which consists of a set of classes and no main() execution method of it's own. This CoreLibrary is a dependency for each of these Services so I bundle The same CoreLibrary Jar into each of these Services Jars.
A number of classes in my CoreLibrary use some constant value(s) (configurations) for which I had defined a class like:
public class Config {
public static final String serverIP = "localhost:9092";
}
Now I want to shift this configuration into a config.json File like:
{
"serverIP" : "localhost:9092"
}
and access it through something like:
public class Config {
public static final String serverIP = getConfigValue();
private String getConfigValue()
{
try {
Reader reader = Files.newBufferedReader(pathToConfig);
CoreConfig item = gson.fromJson(reader, CoreConfig.class);
reader.close();
return item.getServerIP();
} catch (IOException e) {
e.printStackTrace();
return "localhost:9091";
}
}
}
Assuming that having a config file outside my application's Jar would allow me to change the file contents (values) each time before I run my Services Jars instead of going through the process of generating a Jar for each service every time I change this value. I also wouldn't want to load the value from the file from each Service but instead have the value loaded upon startup.
Is there a proper way to achieved this in Java (loading a value from a file from a class library on its own) ? and would it even be considered a valid code practice since item.getServerIP() may possibly produce NullPointerException if the file or json object couldn't be read ?
Note: I'm not using Spring or any such framework. It's plain Java 11 code and uses gson library.

.dll file integration in java eclipse

I am having a .dll file which has a wrapper in C,C++, .Net and python, but not in Java. I am successful in loading the .dll file using following code in my Java
public class
public static void main(String[] {
// print when the program starts
System.out.println("Program starting...");
System.out.println("Loading Library...");
Runtime.getRuntime().loadLibrary("HelloJava");
System.out.println("Library Loaded.");
}
}
which gives the following output:
Now my question is that if this file is loaded, How do I access it's functions to use in my Java workspace?
Since it is the C-DLL, so How should I fetch the module values from this .dll.
Note:
I have made a folder named dll under my Java project path from where I loaded the library in the above code.
I browsed for the concept for JNA and JNI but lacked the understanding concept, that's why posted the question.
Thanks in advance.
I think you need to know the methods in your DLL first. I really don't know how to list the methods using JNA or JNI, but you have to know the method's signature before starting, maybe from a documentation, because you normally can find the DLL's documentation on the web, or you even can use a Reflector (like red-gate) to find out your methods.
Then:
Download the JNA .jar file and add it to your build in your Java project.
Put the .dll in the root directory of your Java project.
Create an Interface that contains the functions from the dll that you want to use.
For example, lets say your HelloWorld DLL has a String hello(String hey) method in C++, then in your Java project you will have to do something like:
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Main {
public interface Ihello extends Library {
public String hello(String hey);
}
public static void main(String args[]) {
//"hello-world is the name my DLL, for example.
Hello h = Native.load("hello-world", Hello.class);
System.out.println(h.hello(" John! ");
}
}
Here is a good example, regards.

Lotus Notes: Java Runs Fine when on an Agent, but fails when on a java lib

I created a Web Service Consumer.
I want to call a method that is named setCredentials so I can pass my authentication information to the service.
I have two entities that import the web service consumer, an agent and a java library, meant to be called from LotusScript.
The strange thing is that on my agent everything works fine. Library compiles OK, but when it is executed from LotusScript and reaches that line
stub.setCredentials("xxxx","ttttt");
Java throws a java.lang.nosuchmethod error. What can I be doing wrong?
Thank you so much in advance for your help.
Update:
Maybe I didn't explain fully. The action occurs fully inside java. This is sort of a test. On LotusScript I am just calling the constructor with the sequence GetClass/CreateObject. The code is inside the constructor (For test sake). And it looks precisely the same, both on my test agent and on java library. Answering your question, Jason, no , setCredentials is part of a certain lotus.domino.types.PortTypeBase Interface. When I consume the .wsdl to create a web service consumer, I can see from the generated .java files that my interface extends portTypeBase and Remote
It is not possible to call a Java Web Service consumer from LotusScript (LS2J). This is detailed in SPR SODY7UDKE8 / APAR LO42772. This also applies to calling a Java agent which in turn calls a Java consumer.
You will need to create a LotusScript consumer to access the web service in LotusScript. However there are known limitations in LotusScript which can prevent some web services from being consumed.
40 character variable/method limit
Extremely large SOAP messages can cause performance/crash issues.
Reserved keywords mismatch in LS/WSDL/SOAP.
That said I created the following sample Provider.
Class wsClass
Function hello ( helloText As String) As String
hello = "Hello " + helloText
End Function
End Class
In the NSF it was stored I set it to only allow authenticated users.
I then created a LS consumer and Java Consumer libraries from the generated WSDL.
After that I created the following sample code.
LotusScript
Use "LsWebServiceConsumer"
Sub Initialize
Dim stub As New Wsclass
Dim answer As String
Call stub.Setcredentials("testuser", "password")
answer = stub.Hello("world")
MsgBox answer
End Sub
JAVA (added consumer library to agent)
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
WsClass stub = new WsClassServiceLocator().getDomino();
stub.setCredentials("testuser", "password");
System.out.println(stub.HELLO("world"));
} catch(Exception e) {
e.printStackTrace();
}
}
}
Both of these worked as expected with their own respective consumer.

runtime modeler error while creating very simple web service in java

I am beginner in java web-services.
I created a simple web service and when trying to publish it as below
Endpoint.publish("http://localhost:8080/HelloWeb", new HelloWeb());
Getting error as below
Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class com.ravi.jaxws.SayGreeting is not found. Have you run APT to generate them?
at com.sun.xml.internal.ws.model.RuntimeModeler.getClass(RuntimeModeler.java:256)
at com.sun.xml.internal.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:567)
at com.sun.xml.internal.ws.model.RuntimeModeler.processMethod(RuntimeModeler.java:514)
at com.sun.xml.internal.ws.model.RuntimeModeler.processClass(RuntimeModeler.java:341)
at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:227)
at com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:308)
at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:174)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:420)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:439)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:208)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:138)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:90)
at javax.xml.ws.Endpoint.publish(Endpoint.java:170)
at com.ravi.Server.main(Server.java:9)
Any Idea whats going wrong here.
My webservice class is very simple and here is code:
#WebService
#SOAPBinding(style = Style.DOCUMENT, use=Use.LITERAL)
public class HelloWeb {
#WebMethod
public String sayHello(String name){
return "Hello "+name;
}
}
first invoke the wsgen utility. This utility generates the various artifacts , i.e. java types needed by the method Endpoint.publish to generate the service's WSDL.Here is the example
In the working directory run
wsgen -keep -cp package.HelloWeb

Custom class in axis2 web service

I try to create web service to Axis2.
I am use eclipse and the "Axis2 Service Archiver" to create aar file from java class.
My problem is that I have function that return custom class like:
public TestClass TestFunc(){
return new TestClass();
}
My question is how my client know what is TestClass? , the TestClass don't show in the wsdl file.
Thanks For the help
If you can see the operation TestFunc() appears in the WSDL - you should be able to see type corresponding to the TestClass in your WSDL. If you cant see, how does the WSDL show the return type of the TestFunc() in the WSDL..?
Thanks..

Categories