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.
Related
I'm using Tomcat to serve my Java Servlets and it's kinda more for me. I just need to serve, Servlet Requests alone, no static content, neither JSP, etc. So I was looking for a Servlet container that can be embedded in my Application. I felt it if stripped Jetty and use it as a Servlet Container alone, it can be more scalable and occupying small memory footprint, [I don't need Jetty's 'Web Server' and other Parts]. So I've a few questions though,
How do I embed Jetty in my Application Code to serve Servlet Requests alone?
If I embed Jetty code in my Application Code, will I be able to easily upgrade Jetty Versions?
I got the Jetty code here, if I have to embed Jetty's Servlet Container in my App, which one should I use from the source,
http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/snapshot/jetty-9.0.3.v20130506.tar.bz2 ,
jetty-9.0.3.v20130506/jetty-servlet or jetty-9.0.3.v20130506/jetty-servlets
I intend to serve API Requests with my Applications and I'm looking for Performance and Scalability as main constraints. And of course Servlet 3.0 support.
What you are looking for is running Jetty in an embedded scenario.
There's plenty of examples available showing how to tie together the various pieces you need to accomplish your goals.
Check out the embedded examples in the jetty source tree.
For the record, jetty standalone is really just jetty embedded with a few startup and classpath related bootstraps. It is the same code, and assembled in basically the same way.
Since you stated you want Servlet 3.0, have no interest in JSP, this is rather easy to setup. (JSP is trickier to setup, but possible).
For servlet 3.0 specific embedding, there's a complete example project hosted at github.
https://github.com/jetty-project/embedded-servlet-3.0
In short, you'll have the following initialization code.
package com.company.foo;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.FragmentConfiguration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.TagLibConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;
public class EmbedMe {
public static void main(String[] args) throws Exception {
int port = 8080;
Server server = new Server(port);
String wardir = "target/sample-webapp-1-SNAPSHOT";
WebAppContext context = new WebAppContext();
// This can be your own project's jar file, but the contents should
// conform to the WAR layout.
context.setResourceBase(wardir);
// A WEB-INF/web.xml is required for Servlet 3.0
context.setDescriptor(wardir + "WEB-INF/web.xml");
// Initialize the various configurations required to auto-wire up
// the Servlet 3.0 annotations, descriptors, and fragments
context.setConfigurations(new Configuration[] {
new AnnotationConfiguration(),
new WebXmlConfiguration(),
new WebInfConfiguration(),
new TagLibConfiguration(),
new PlusConfiguration(),
new MetaInfConfiguration(),
new FragmentConfiguration(),
new EnvConfiguration() });
// Specify the context path that you want this webapp to show up as
context.setContextPath("/");
// Tell the classloader to use the "server" classpath over the
// webapp classpath. (this is so that jars and libs in your
// server classpath are used, requiring no WEB-INF/lib
// directory to exist)
context.setParentLoaderPriority(true);
// Add this webapp to the server
server.setHandler(context);
// Start the server thread
server.start();
// Wait for the server thread to stop (optional)
server.join();
}
}
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
I've written a simple REST service class, and I need to deploy and run it under glassfish server, installed on my eclipse.
What steps have I to do in order to put this restlet service online and reach it from my browser?
This is the code:
import javax.ws.rs.*;
import javax.ws.rs.core.*;
#Path("/myApplication")
public class MailRestlet {
#SuppressWarnings("unused")
#Context
private UriInfo context;
/**
* Default constructor.
*/
public MyRestlet() {
// TODO Auto-generated constructor stub
}
#GET
#Produces("text/html")
public String getHtml() {
return "<html><body><h1>Hello World!!</h1>The service is online!!</body></html>";
}
}
You also need to configure the web.XML file, for example, as in http://download.oracle.com/docs/cd/E19776-01/820-4867/ggrby/index.html
There is also a way to avoid web.XML change, by extending the Application class from jaxrs...
As an alternative to configuring the end point using the web.xml file, you can extend the Application class as described here.
From in Eclipse, you can deploy on the server by:
Right clicking the project and then Run As > Run on Server.
Select your server from the list of servers you have connected to Eclipse on the screen that follows and click Next.
Ensure the project is listed under Configured and click Finish.
You can also set Eclipse to automatically publish to GlassFish when you change the code. Double click the GlassFish Server on the Server view. Expand the Publishing expandable list and select Automatically publish when resources change.
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.
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.