Deploying web service on cloud - java

Developed an web service , below are the steps
1) Create a Web Service Endpoint Interface..
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service Endpoint Interface
#WebService
#SOAPBinding(style = Style.RPC)
public interface HelloWorld{
#WebMethod String getHelloWorldAsString(String name);
}
2. Create a Web Service Endpoint Implementation ..
import javax.jws.WebService;
//Service Implementation
#WebService(endpointInterface = "com.abc.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
Create a Endpoint Publisher...
import javax.xml.ws.Endpoint;
import com.abc.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloWorldPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}
Now I have also tested the deployed web service by accessing the generated WSDL (Web Service Definition Language) document via this URL “http://localhost:9999/ws/hello?wsdl” .
But My query is that as I new to the world of cloud , I want to deploy my webservice to cloud like amazon so that If I provide the wsdl to anyone in the world he can access my wsdl through his browser as my web service is deployed on cloud.
Please advise me how to achieve this..!!

This aproach will not work when you deploy your application to a real server on the cloud because you cannot execute your main method to publish the web service.
You need to configure something to publish your web service when your application starts on server.
For example, using Spring, to run an SOAP Web Service on Tomcat you need to inject your WS beans and use the SimpleJaxWsServiceExporter bean to publish it, these configurations are realized on your application-context.xml or equivalent.
In your case, take a look on this link, it is an example of how to publish an WSDL Web Service using JAX-WS RI distribution.
For tests, you can deploy your WAR application to Openshift.
Hope it helps,
Best Regards.

Related

How can I bootstrap Jersey REST service server?

I've been thrown into a project that is basically just a REST service an provides some functionality to web clients. However, I can't see any bootstrapping going on yet for the services - like not at all..
In particular I have to setup the file system for the server and its services. Therefore I am looking for a way to get control of the web application as the server is booting up and before it is loading the REST resources:
import javax.ws.rs.Path;
import com.sun.jersey.spi.resource.Singleton;
#Path("/")
#Singleton
public class EnrichmentResource {
// ...
}
How can I do that? I can only find such simple examples where a REST Controller gets defined but no bootstrapping examples.
There isn't a "pre-entry" part of JAX-RS per se. However, in any JEE application you can always define a WebListener:
#WebListener
public class MyListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println( "context initialized" );
}
}
The contextInitialized() method will be called before anything is called into your REST services. Remember that JAX-RS is still built on top of the servlet framework.

Add web services functionality to current working java application

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.

Programmatically create subdomains with JBOSS and java

Right now I am developing an application on JBOSS 7.1 using JSF, SEAM and Primefaces. The application is providing a user registration. What I need is when the user registers an account for the nickname for example "andrew", his profile will be publicly accessed as andrew.mysite.com.
How can I implement this programmatically.
Thanks in advance,
Ilya Sidorovich
This is simply a process of mapping your subdomain to URL's that can be accessed by the appserver and use something like REST to map the URL to request parameters.
In your example, you will probably need a webserver like Apache web server to handle the incoming requests that can do some URL rewriting. Something like this
user.mysite.com --> www.mysite.com/user
In Apache this can be achieved by creating a virtualhost and using RewriteCond and RewriteRule. Here is an example
RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.com$
RewriteRule ^/(.*)$ http://www.mysite.com/%1/$1 [L,R]
You can then forward your requests from the webserver to your application server. If using Apache this can be done using mod_jk, mod_proxy or mod_cluster.
Once you have that, you can create a RESTFul service (jboss supports REST) that can map the URL to your application code. Here is an example
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/")
public class UserService {
#GET
#Path("/{param}")
public Response printMessage(#PathParam("param") String user) {
String result = "User : " + user;
return Response.status(200).entity(result).build();
}
}

How to create a Java client for Web Service?

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

Add a web service to a already available Java project

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.

Categories

Resources