Add a web service to a already available Java project - java

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.

Related

How to create simple RESTful service in IntellijIDEA 2016? (JAX-RS)

My google-fu has failed me.
I have lost 2.5 hours trying to figure this out. I just want to make this simple RESTful service with get and post requests that generate hello world on get (i.e. localhost:9000/hello ) and on post prints to service console what was sent in bla variable.
I have found some simple examples
import javax.ws.rs.GET;
import javax.ws.rs.Path;
#Path("greeting")
public class Greeter {
#GET
public String sayHi() {
return "Hi!!";
}
}
But it doesn't work intellij doesnt recognize Path and GET annotations. It asks me if I want to implement them. I've tried on both NewProj->JavaEE->Restful Web Service and NewProj->Java->WebApp->WebServices.
Some sample generated code from one of them made this #webmethod annotation for which I wasn't able to find any info on the net. And the JetBrains video from 2013 looks like an overkill/a-bit-outdated?. This is really simple app, I don't need/know how to use/ Maven.
You need to download the jar-file containing the classes you have imported (from e.g. http://download.oracle.com/otndocs/jcp/jaxrs-2_0_rev_A-mrel-spec/index.html).
Open Project Structure, Go to Libraries, press the plus button, and add the jar-file to your project.

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.

Deploying web service on cloud

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.

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

In-process SOAP service server for Java

OK, I am developing a program which will be deployed to lots of machines (Windows, Linux, AIX, z/Linux, openVMS, etc.). I want that application to contain a SOAP web service, but I don't want to bundle tomcat or run a separate service for the services (I want them in the same process as the rest of the application).
Basically what I'm looking for is something where I can define a class (say WebServices). I'm OK with writing WSDL or any other kind of service description as well. The I want something like this:
SOAPServer server = makeMeASoapServer();
//do config on the server
server.add(new WebService(...));
server.listen(port);
Obviously the names and parameters will be different.
I've been looking at Axis, and it seems like it provides this, but I don't know what classes I need to use. Am I crazy in wanting this kind of behavior? I can't believe more people aren't looking for this, I do this all the time with embedded web services within .NET clients.
Seems jdk 6.0 already comes with a jax-ws implementation, and a little server you can embed.
I havn't figured out all the pieces but here's a start:
mkdir -p helloservice/endpoint/
helloservice/endpoint/Hello.java :
package helloservice.endpoint;
import javax.jws.WebService;
#WebService()
public class Hello {
private String message = new String("Hello, ");
public void Hello() {}
public String sayHello(String name) {
return message + name + ".";
}
}
helloservice/endpoint/Server.java:
package helloservice.endpoint;
import javax.xml.ws.Endpoint;
public class Server {
protected Server() throws Exception {
System.out.println("Starting Server");
Object implementor = new Hello();
String address = "http://localhost:9000/SoapContext/SoapPort";
Endpoint.publish(address, implementor);
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
Build the thing:
mkdir build
javac -d build helloservice/endpoint/*java
$JAVA_HOME/wsgen -d build -s build -classpath . helloservice.endpoint.Hello
Run the thing:
java -cp build helloservice.endpoint.Server
Somethings running on http://localhost:9000/SoapContext/SoapPort now.
You can get the wsdl on http://localhost:9000/SoapContext/SoapPort?WSDL
Havn't gotten around to making a client yet..
In addition to nos's great answer, I found a class in Apache axis called SimpleHTTPServer which I'm pretty sure does the same thing but only requires Java 1.5 for those of you stuck with 1.5
I'm not going to explore it since I'm going to use the other solution, so I haven't actually verified it does what I think it does, but I'm pretty sure it does.
Most(/all?) Java SOAP server implementations provide a Servlet (the javax.xml.ws.Endpoint approach in another answer does look a bit simpler though...). Some SOAP implementations you could consider are: Apache CXF: cxf.apache.org, Apache Axis2: ws.apache.org/axis2/ or Spring Web Servies: static.springsource.org/spring-ws/site/ .
The most popular embedded Java web server seems to be Jetty, you can configure it either programatically (using plain Java or Spring beans) or using a custom XML format.
To address the main question directly, another approach would be to go with Jetty's embedded server. See this link for details. The links from the aforelinked page help you understand both the simple web server (i.e., one that serves up static pages; though I am fully aware "simple" is a horribly vague term wrt web servers) and the web server that helps you deploy web services.

Categories

Resources