JAX-WS webservice client - java

I am new to web service programming. I have created one JAX-WS SOAP web service and deployed it locally. I am writing client in java to invoke the service. Basically, this service takes two integers and returns the sum of them. I have written the following JAX-WS client in java.
public class WebserviceClient {
public static void main(String[] args) throws Exception {
URL url = new URL
("http://localhost:9999/ws/additionService?wsdl");
QName qname = new QName("http://test/",
"AdditionServiceImplService");
Service service = Service.create(url, qname);
AdditionService additionService = service
.getPort(AdditionService.class);
System.out.println(additionService.add(1, 2));
The above code is working fine. But, in this code I need to create QName by passing the targetNamespace and name of the service. I want to know whether constructing QName is mandatory? Since, I am already passing the WSDL URL, no point creating QName with the WSDL targetnamespace string. Is there any alternative approach available?

Related

How provide authenticatioon to check username and password for Soap based webservices

I am working on SOAP based web services. I want to check for credentials.
#WebService(name="Helloworld",portName="HelloworldPort", serviceName = "Helloworld", targetNamespace="http://Helloworld.com/webservices")
#SOAPBinding(style = SOAPBinding.Style.RPC, use= SOAPBinding.Use.LITERAL, parameterStyle= SOAPBinding.ParameterStyle.WRAPPED)
public class Helloworld {
public String sayHello(#WebParam(name="name")String name) {
return "hello "+name;
}
}
I have read some where that we can use USERNAME_PROPERTY, and PASSWORD_PROPERTY to services.
It seems it is Basic http authentication.
I don't know how to accommodate it in service as well as in client.
Can some one tell me how to achieve it.
Thanks.

Different web service object after each call

I am new with Java EE and SOAP. I have tried to create a simple web service application and its client (environment: NetBeans 7.2.1 IDE, GlassFish Server 3.1, Java 1.6).
Web service code:
package simplews;
import javax.jws.*;
#WebService(serviceName = "SimpleWebService")
public class SimpleWebService {
String something = null;
#WebMethod(operationName = "setSomething")
#Oneway
public void setSomething(#WebParam(name = "smth") String smth) {
something = smth;
}
#WebMethod(operationName = "getSomething")
public String getSomething() {
return something;
}
}
Client application code:
package simpleclientapp;
import simplews.*;
public class SimpleClientApp {
public static void main(String[] args) {
SimpleWebService_Service service = new SimpleWebService_Service();
SimpleWebService port = service.getSimpleWebServicePort();
port.setSomething("trololo");
String smth = port.getSomething();
System.out.println(smth);
}
}
Unfortunately, the client application printed out null. After short investigation I have realised, that on the server side a new SimpleWebService object is created for each client call (sounds like stateless approach).
What is wrong here? Why the client port does not refer to the same WS object for each call?
Web services are stateless by nature. In order to keep state between requests, you have to persist the data (in a file,database etc.).
You're right, JAX-WS web services are stateless by default and you can't rely on something thatviolates this premise. Follow a different approach in storing such values. You can read this doc Java TM API for XML Web Services (JAX-WS) Stateful Web Service with JAX-WS RI, if you really want to follow the direction in your post.

IntelliJ web service and Java client IllegalArgumentException TestWebService is not an interface

In IntelliJ 10.0.3
I use the menu option "new web service" and this generates a class file and adds to sun-jaxws.xml - this is fine - it's working.
Now if I try to write a Java client for this web service I get IllegalArgumentException TestWebService is not an interface
Here's my client code:
public class WebServiceTest {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost/services/TestWebService?wsdl");
//1st argument service URI, refer to wsdl document above
//2nd argument is service name, refer to wsdl document above
QName qname = new QName("http://ws.mydomain.com/", "TestWebServiceService");
Service service = Service.create(url, qname);
TestWebService test = service.getPort(TestWebService.class); // fails here
System.out.println(test.sayHelloWorldFrom("TESTING...."));
}
}
How should I implement this? Should I have an interface and a class? Is there a good example? Best practice?
this is my endpoint definition in sun-jaxws.xml
<endpoint
name='TestWebService'
implementation='com.allscripts.ws.TestWebService'
url-pattern='/services/TestWebService'/>
I was getting messed up because I was trying to use the web service withing my application using the same classpath. Running a test in a different java project works fine.

How to call web service from java code?

I want to call a web service "gatewaedi" from java code.
I am not getting how to call it, could someone please provide an example?
This is how you call a webservice with JAX-RPC
String wsdlURL = "http://localhost:6080/HelloWebService/services/Hello?wsdl"[1];
String namespace = "http://Hello.com"[2];
String serviceName = "GatewaediWebService";
QName serviceQN = new QName(namespace, serviceName);
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service service = serviceFactory.createService(serviceQN);
Should be replaced by the gatewaedi webservice call, which I can not find now.
Should be replaced by the gatewaedi webservice corresponding namespace, that too I can't find.
If you want send me more information about this webservice and I will write you the complete code.

Axis2 SOAP Envelope Header Information

I'm consuming a web service that places an authentication token in the SOAP envelope header. It appears (through looking at the samples that came with the WS WSDL) that if the stub is generated in .NET, this header information is exposed through a member variable in the stub class. However, when I generate my Axis2 java stub using WSDL2Java it doesn't appear to be exposed anywhere.
What is the correct way to extract this information from the SOAP envelope header?
WSDL:
http://www.vbar.com/zangelo/SecurityService.wsdl
C# Sample:
using System;
using SignInSample.Security; // web service
using SignInSample.Document; // web service
namespace SignInSample
{
class SignInSampleClass
{
[STAThread]
static void Main(string[] args)
{
// login to the Vault and set up the document service
SecurityService secSvc = new SecurityService();
secSvc.Url = "http://localhost/AutodeskDM/Services/SecurityService.asmx";
secSvc.SecurityHeaderValue = new SignInSample.Security.SecurityHeader();
secSvc.SignIn("Administrator", "", "Vault");
DocumentServiceWse docSvc = new DocumentServiceWse();
docSvc.Url = "http://localhost/AutodeskDM/Services/DocumentService.asmx";
docSvc.SecurityHeaderValue = new SignInSample.Document.SecurityHeader();
docSvc.SecurityHeaderValue.Ticket = secSvc.SecurityHeaderValue.Ticket;
docSvc.SecurityHeaderValue.UserId = secSvc.SecurityHeaderValue.UserId;
}
}
}
The sample illustrates what I'd like to do. Notice how the secSvc instance has a SecurityHeaderValue member variable that is populated after a successful secSvc.SignIn() invocation.
Here's some relevant API documentation regarding the SignIn method:
Although there is no return value, a successful sign in will populate the SecurityHeaderValue of the security service. The SecurityHeaderValue information is then used for other web service calls.
I believe the call you're looking for is:
MessageContext.getCurrentMessageContext().getEnvelope().getHeader()

Categories

Resources