Would anyone be able to tell me how I can call a webservice in my struts application?
Well, I can think of several ways to do this depending on the web service. Most IDEs have ways to auto-generate webservice clients. I would probably create a java library package that which wraps the service client and provides an interface for your Struts application to invoke the client methods from within your Struts action class.
For example, if the service has a method getPerson(), I would create a remote DAO class that invokes the web service getPerson() method:
public class PersonServiceInterface{
public Person getPersonFromService(){
// web service calls to retrieve person object
return person;
}
}
Related
I need to create a client with WSDL. I have a Java web application with JSF, Spring and JPA. In this application I need to create a form and send the info to the SOAP web service. This service should return another object with status.
Please, any idea I will be grateful
regards
sorry by my english
I assume you have generated classes from the WSDL needed for you client. In Spring it is very simple by using Apache CXF. For e.g.:
<jaxws:client id="yourService"
serviceClass="com.something.YourService"
address="the URL of web service"
username="username"
password="password"/>
And in your class where you need to call this web service just autowire it:
#Autowired
#Qualifier("yourService")
private YourService service;
Take a look at the example: http://cxf.apache.org/docs/writing-a-service-with-spring.html
My application should show a list of stuff over RESTful WebServices.
This list of stuff is generated via an EJB which is deployed in JBoss AS 6 in a .jar.
My question is, how do I get this list of stuff in order to show it via Web Services?
If I try to inject #EJB in the stateless bean annotated with #WebService, and try to invoke the method that generates the list, I get an error saying that #EJB cannot be resolved (or imported). (fixed it, needed to modify maven dependencies).
Now I get a NullPointerException when I call the supposedly injected EJB...
I am using the webapp archetype from Maven, which contains an EAR, a JAR and a WAR.
I am at a total loss here, been trying to figure this for almost a week.
Thanks in advance!
--EDIT-- code and typo fixed
This is my stateless bean which SHOULD be exposed as a WebService (according to:
http://www.adam-bien.com/roller/abien/entry/restful_calculator_with_javascript_and )
#Stateless
#Path("/MyRESTApplication")
public class HelloWorldResource {
#EJB
private TestBean testBean;
#GET()
#Produces("text/plain")
public String sayHello(String name) {
return testBean.doMoreStuff();
}
}
the doMoreStuff() function simply returns "HELLO!".
I want to export a Web-Service which was implemented as a stateless EJB. I know that these WebServices were hanled by the EJB Container, when they are annotated as #Stateless + #Webservice. Is it possible to route all incoming requests to this Webservice through a Servlet-Filter.
The Servlet-Filter works when my Java-Class is annotated #Stateful and #Webservice, or just #Webservice. But not in conjunction with #Stateless. Anyway to register a Servlet Filter for an EJB Webservice?
Many thanks!
Adem
UPDATE:
Solved this problem, by annotating WebService Class with
#WebService
#RequestScoped
Filter works only in this constellation and acting as Stateless class for WebService consumer.
Lifecycle Callbacks : You can have a method with #PostConstruct annotation which gets called after the container has initialized the bean.
Interceptor : You can have a interceptor class which gets invoked when applied at bean class/method level by annotation #Interceptors(ProcessMonitor.class).
Note : I haven't tried it in conjunction with #Webservice.
i have a local client j2se application and backend is derby(javadb) database and dao is jpa eclipselink .
how do i send these database pojo to a remote database which linked with spring ( jsp) application on tomcat server
simply this is a rich client with swing which connects to tomcat deployed web application. The client should receive data and send data through HTTP requests to the server-side of the service,
what would be the best solution ??
01) direct database connection/transaction through socket using Eclipselink
02) web service ??
03) just send post request to spring web application and convert it to POJO and persist to database
how do i achieve this??
DISCLAIMER I am not suggesting you port your app from Spring to EJB. Despite how people like to compare them as exclusively one or the other, you can use them both. Its your app, you can be as pragmatic as you want to be :)
You don't necessarily have to use Web Services if you wanted. You could drop the OpenEJB war file into Tomcat as well and create an Remote EJB to send data back and forth.
Once you drop in OpenEJB you can put a remote #Stateless bean in your app like so:
#Stateless
#Remote
public class MyBean implements MyBeanRemote {
//...
}
public interface MyBeanRemote {
// any methods you want remotely invoked
}
Then look it up and execute it over HTTP from your Swing app like so:
Properties p = new Properties();
p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
p.put("java.naming.provider.url", "http://tomcatserver:8080/openejb/ejb");
// user and pass optional
p.put("java.naming.security.principal", "myuser");
p.put("java.naming.security.credentials", "mypass");
InitialContext ctx = new InitialContext(p);
MyBean myBean = (MyBean) ctx.lookup("MyBeanRemote");
Client-side all you need are the openejb-client.jar and javaee-api.jar from the OpenEJB war file and your own classes.
Since it's already a Spring app don't bother trying to use #PersistenceContext to get a reference to the EntityManager so the EJB can use it. Just figure out how to expose the EntityManagerFactory that Spring creates (or you create) to the EJB via any means possible. The direct and ugly, but effective, approach would be a static on the MyBean class and a bit of startup logic that sets it. You'd just be using the EJB for remoting so no need for fancier integration.
If you did really need web services for non-java communication or something, you can add #WebService to the top of your bean and then it will have WSDL and all that generated for it:
#Stateless
#Remote
#WebService(portName = "MyBeanPort",
serviceName = "MyBeanService",
targetNamespace = "http://superbiz.org/wsdl"
endpointInterface = "org.superbiz.MyBeanRemote")
public class MyBean implements MyBeanRemote {
//...
}
public interface MyBeanRemote {
// any methods you want remotely invoked
}
Then you can also use the same bean as a web service like:
Service service = Service.create(
new URL("http://tomcatserver:8080/MyBeanImpl?wsdl"),
new QName("http://superbiz.org/wsdl", "MyBeanService"));
assertNotNull(service);
MyBeanRemote myBean = service.getPort(MyBeanRemote.class);
Both approaches are over http, but the web service approach will be a bit slower as it isn't a binary protocol.
I have 2 different webservices running on 2 different tomcat application servers (w/ axis2 web service engine) (Webservice A runs on Server A and Webservice B runs on Server B).
How can web service A on Server A pass Data A (file) to Web Service B on Server B? I am new to web services and would appreciate any help in this regard. The webservices are in Java.
Thanks!
Service A needs to be a client of service B. Service B should expose some method service A will consume (and pass required data using it). The process is as follows:
If suitable service method doesn't exist yet in service B then add new method to service B's WSDL file.
Regenerate intefaces from extended WSDL file.
Create functional test for new service method .
Make service A a consumer of method of new (extended) service.
Create acceptance tests for service A methods using service B's method :-)
Implement new service method in service B.
Implement conusmer logic in service A.
Expose a "send" web-service API on B and call it from A.
There are thousends of ways, but with HTTP Protocol you can use: POST or PUT methods.
However, you'll need to write application on each side...