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

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.

Related

Accessing JAX-WS Published Endpoint is Not Working

I am trying to create a Web service using JAX-WS. I do have a very basic Java project with the following:
EmployeeService .java
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService
public class EmployeeService {
#WebMethod
public String getEmployee(String id) {
return "Vlad Danila";
}
}
Exporter.java
import javax.xml.ws.Endpoint;
import services.EmployeeService;
public class Exporter {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/hello",
new EmployeeService());
System.out.println("Successfull!");
}
}
Running the above will throw no error and print "Successfull!".
However, accessing http://localhost:8080/hello on browser gives This page isn’t working.
What am I missing?
I did an example with your code, and it works.. you have to add this to the browser to see
http://localhost:9999/ws/hello?wsdl
This is the url on my case. Then consume it with soap ui or another ws client.
The error you see its cause you are doing a get request on that url and not a soap request.
You don't give much context about what you are doing. JAX-WS is supposed to run in container. Do you run in container which is JEE compatible. See this tutorial, especially the last part:
https://docs.oracle.com/javaee/6/tutorial/doc/bnayn.html#gjyge
If you want something simple, I would recommend to make a spring-boot app, which will work out of the box for you. Forget about heavy JEE containers and try to run a simple spring-boot app which have integrated server inside the spring-boot app.
Here is a link to follow: https://spring.io/guides/gs/rest-service/

Best Practice XML Request Builder in Java

I am developing a restful web service in Java. One of my service needs to call a SOAP service and for this I need to build an XML request. My question is that "I want to build that xml file in a seperate helper class, is this a convenient way to do? " I am using Spring for MVC, is there any advantages of it here that I can implement?
Example pseucode;
#RestController
#RequestMapping("/rest/menu")
public class MenuController {
#RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> createUser(#RequestBody JSONObject userInfo){
//Here I need to make a Soap call to retrieve some information
MyXMLBuilder myXMLBuilder = new MyXMLBuilder();
String soapRequest = myXMLBuilder.build();
}}
and here the helper class;
public class MyXMLBuilder(){
public String build(){
//xml build implementation
}}
Usually, to consume a SOAP WebService, you will create the required stubs with the wsimport tool, which is within your Java SDK in the /bin folder.
Within your favourite terminal, switch to your desired folder for the stubs to be created in and type:
wsimport -keep -verbose http://example.com/myservice?wsdl
You can then use the Service Stubs like this
MyServiceImplService myService = new MyServiceImplService();
MyService port = myService.getMyServiceImplPort();
port.yourFunction(param1, param2);

JAX-WS webservice client

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?

What is URL of WSDL of EJB WebService (JAX-WS)?

As far as I understood, you can introduce:
#Stateless
#WebService
public class MyWebServiceEndpoint {
#Inject SomeBean aBean;
#WebMethod
public String getSomething() {
return "something";
}
}
and when application deployed, the WebService is exposed in the Application Server (such as WebSphere). Then what is the URL of WSDL, where other applications can find my service?
The URL of your WSDL on the server should be in the following format:
http://hostname:port/contextRoot/MyWebServiceEndpoint?WSDL
But this is running under the assumption that the url-pattern attribute of the endpoint entity in your sun-jaxws.xml file uses the same name as your web service class.
Just a hint to the first answer/URL,
In some cases the "context Root" get excluded from the webservice url after deployment.
I can't say for other application servers but for WebLogic and EJB web service URL follow the following default pattern:
http://host:port/'className'/'className'+Service
where 'className' is the simple name of the Java class implementing the web service.
You can easily override the end of the URL by setting the serviceName attribute in the #WebService annotation. If you need to change the root context you must package your web service as an EJB jar embedded in an EAR and use the WebLogic specific deployement descriptor (which I would avoid at all costs).
Hope it helps :)
you need to publish it, You can have a Publisher class like this:
import javax.xml.ws.Endpoint;
//Endpoint publisher
public class Publisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/webserv/Test", new FilenetWebService());
}
}

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.

Categories

Resources