I am really really having hard time to create simple "Hello World" Web Service in Java. If you exclude several mobile Android mobile apps I have done, I am pretty new to Java environment.
First I tried Axis2 and that simply doesn't work out of the box. Fresh installation of Eclipse, Tomcat 6.0 and Axis2. Tried sample test app and it failed miserably. You can read about it here. My conclusion is that Axis2 simply doesn't work (maybe it used to work). I will try to go back to install older versions, maybe one will magically start working. I have to modify some old project at work that was using Axis2, so that's why I have to stick with it. I would hate to have to migrate to some other tool.
Then I ran on Oracle article Getting Started with JAX-RPC and I was thinking, cool, let's try that. Well, I just managed to get pissed off. After getting half way through the article and trying to write some simple test web service I realized that their code examples are poorly written. Missing brackets, wrong references, missing explanations, etc...
First of all, an interface should extend not implement. Am I right?
public interface SunRegPort implements java.rmi.Remote {
Also, this doesn't exist:
import java.xml.rpc.server.ServiceLifecycle;
But this does:
import javax.xml.rpc.server.ServiceLifecycle;
Well, I don't feel competent to criticize too much (cause of my level of knowledge about this subject of course), but after all I am suspicious that this article is complete JUNK and therefore I am not able to follow it.
Please somebody tell me that I am wrong and if anybody has some advice or link to some How-To page that talks about web services, I would appreciate.
Thanks.
The terminology Web Services is pretty vague. In Java, the modern and reasonably easy way to do this is by annotating classes.
I would recommend that first, you decide whether you want to implement:
SOAP Web Services -> look at JAX-WS
REST Web Services -> look at JAX-RS
Once you have chosen the "type" of Web Services, choose the library that implements the specification.
The Wikipedia entries list a few of these implementations. Apache CXF (complete but a beast), Jersey (popular) and Restlet are very common choices. I personally like the JBoss implementations as well for JAX-WS.
For JAX-WS with Apache CXF, here is a quick start tutorial that looks pretty good (untested)
For JAX-RS with Jersey, try this
Unless you have strong reasons to do otherwise (for instance you need to call an existing system), use REST which is simpler and leaner.
I would recommend you to check out the CXF project by apache. It's quite easy to use and should help you to set up a webservice.
There is a nice guide to Hello world set up.
http://cxf.apache.org/docs/a-simple-jax-ws-service.html
If you're still interested in giving Tomcat a shot, here is a tutorial on youtube of making a very basic "Hello world" example using Eclipse. It leaves a lot to learn; but it looks like it will at least get you started.
FYI, the version of Eclipse used is slightly outdated. So some of the menu options may not be exactly the same:
http://www.youtube.com/watch?v=EOkN5IPoJVs
Also, the meaning of "Web Service" can be subjective. So, to be specific; this just shows you how to get a basic HTTP endpoint started on your local machine.
If you are not tied to Eclipse, give NetBeans a shot. I think it's much better for Web Services in Java.
Web Services are most widely implemented examples of Service Oriented Architecture (SOA). A service contract is defined with the help of SOAP and Web Service Definition Language (WSDL) and that is published for other applications to use.
In this article we will see how we can create web service and web service client in java.
http://www.opencodez.com/java/how-to-build-and-deploy-web-service-and-client-in-java.htm
Related
I have limited exposure in creating a Java web app from scratch, and was hoping that some of you folks could help provide me with some insight.
Basically the flow of the application is as follows:
1.) User logs in (Authentication + session handling)
2.) Requests containing XML are passed to the server
3.) Responses are in XML format also and parsed by the client side
Are there any type of frameworks I can use to help me out with adhering to the "best practices" for what I want to accomplish? I'm comfortable with being able to handle the XML/DB interactions, however I'm not too comfortable with the auth and session handling.
So far I've gotten to the point where I'm using Jetty as a servlet container, and coded basic functions to handle hitting the DB. I'm sort of lost on properly implementing the rest.
I was thinking about using SOAP but it seems like overkill?
Any advice would be greatly appreciated. My apologies if this is a silly question to ask, but after searching for hours.
thanks!
-bp
This is a very broad question, so I'll just give a partial answer. As far as using SOAP, its main practical advantage is for interoperability.
However, it would also be easier as a developer than creating something from scratch: both Axis2 and the Java Web Services Development Pack come with very easy to use code generators (the latter made easier w/ Netbeans or Eclipse). You would basically mark up the code you want to expose as a web service by using annotations (examples abound on the web) and then run it through a code generator and run it. This will produce both the client and the server code. Do some research on Axis2 and Java WSDP.
Implementing security with this solution is trickier. But using headers you can exchange encoded login information.
Using a web framework might actually be overkill if all you need is a webservice as opposed to a human-accessible app through a browser.
EDIT: I should add that the web services solution above, as an added bonus, handles all of the xml for you, although implementing security headers will require some knowledge of SOAP and maybe security standards. It's probably best to make these high-level decisions and ask more specific questions here as you go along.
my company runs a site that primarily has its backend logic coded in Perl. It works fine but ultimately we are headed to something Java driven. I do plan on studying this and would like to start setting up a Java environment on another server in our company so when the time comes I at least have a headstart.
Does anyone have a recommendation on where to begin? Just interested in generalities now.
JW
What are you planning to study? All Java or just the web service extensions?
If you come from Perl, I must suggest you starting from "hello world" with a Java cookbook, then you can start moving towards web services after a while.
Another good book about web service is what you might need.
Finally, when beginning to build the web service, I suggest you two things:
Design the WSDL independently from Java code that will implement it. It's very important for cross-platform interoperability. Try to use Altova XMLSpy or a similar tool to define the pure WSDL as a design contract
Deploy with Axis
I don't know how to propose to move this question to Stackoverflow, because you can find LOTS of answers there
You need J2EE
http://download.oracle.com/javaee/1.4/tutorial/doc/
Java Tutorial
http://download.oracle.com/javase/tutorial/
General Java Stuff
http://www.oracle.com/technetwork/topics/newtojava/overview/gettingstarted-jsp-138588.html
http://www.oracle.com/technetwork/articles/javase/webapps-1-138794.html
About the Class Path
http://kevinboone.net/classpath.html
At the moment, I have lot's of Java which does all kind of nifty stuff and I'm happy with it. The code is command line driven which have been great so far, but I recently decided I want to make the functionality available through web-services. Since my is complex and I'm happy with the way it's written , I don't want go through the pain of porting it to other languages. So I set out on a google journey to find out what web servers exist (on a Linux machine, though it's interesting to hear the answer without that limitation).
From what I could find, it seems that there are two viable options: Apache Tomcat and Sun Java Server.
What are the reason to choose one on top of the other? what are the strength of each and what are the weaknesses? Or, perhaps, there is a third one which is much easier, flexible and less cumbersome.
Anyone?
Easy, flexible and not cumbersome, that would be Jetty, but also Simpleweb might be useful. You dont say much about your software so i'm not really sure, but for a command line program, I don't think you need all the JavaEE stuff.
The mainstream servers are these.
I think the Apache Tomcat vs Glassfish (Sun Java Server) discussion is irrelevant for your needs, any would do.
There are many containers for Java web applications, they all have their own strengths and weaknesses. If you're looking for a container to support a business application, you should probably take a look at how they differ and work out which suits your business and IT drivers.
The key thing is that they all support the servlet specification - your webapps can run in any of them - which means you can change your mind easily. Some of them will also support more of the Java Enterprise Edition specs, so may be too heavy for your needs.
If you're just getting started, I'd suggest Tomcat. It's basic, but it's reliable, quick to run and start up, and it's got a really easy web-based webapp deployment interface.
Your question is actually a bit too ambiguous and wide. You can in fact run Java code at any machine you like, regardless of the language you programmed the webbased interface in. You can for example create a PHP based website which interacts with a "backend" Java application (the "command line application" as you call it). The only requirement is to have a JRE at the server machine. Then basically everything as web interface suffices: CGI, PHP, ASP, Python, etcetera, you name it. As long as it has access to the underlying commandline runtime, which is in the PHP example to be done by exec().
But Java, actually Java EE, also provides a web application programming interface: the JSP/Servlet API, the web component of the large Java EE API. This make integration with the "commandline Java application" much more seamless. You can basically just put your application in the classpath and import/access/use it in a Servlet class the real Java way:
YourApplication app = new YourApplication();
Result result = app.doStuff();
// ...
To be able to run JSP/Servlet (JSP is at end actually also a Servlet), you need a concrete implementation of the Servlet API (the whole Java EE is just an abstract specification). Apache Tomcat is good to start with, the other popular alternative being Eclipse Jetty. Those are 'simple' servletcontainers which implements the Servlet API, with Jetty being a more embedded approach of it (you can configure and run it as a "plain vanilla" Java Application). But if you need to support/cover the other aspects of the Java EE API as well, then you need an application server, such as Sun Glassfish or JBoss AS (both which by the way uses Tomcat as the servletcontainer part).
To learn more about JSP/Servlet, I can recommend the Coreservlets.com tutorials.
Apache Tomcat should do good.
The standard concept for running code inside a web server is the "Servlet API" from Sun.
Essentially it provides a few interfaces which the web server uses to invoke your code, and defines how the web server should behave. This allows you to write very generic code that can run in a variety of web containers which implement the Servlet API. See the Wikipedia article for details - http://en.wikipedia.org/wiki/Java_Servlet.
They come in all sizes too, depending on your needs. Some small enough for embedding in your own application, some very big. The servlet API allows you not to care.
For a beginner, the quickest way to get up and running, is to download the full version of Netbeans which includes full support for doing this kind of work, and has a built in servlet container.
There's a lot of information out there, but I can't quite figure out:
What's Java's answer to PHP's SoapClient()?
No frills, just want to create a client to access the methods described by a .wsdl document.
See this question. There are many Web Services frameworks in Java that can generate a client from a WSDL file. Eclipse has a plug-in to create a client stub from a WSDL file using the Axis2 framework. Perhaps this is the easiest option for a beginner.
If all you want is a piece of software to generate client code from an existing WSDL, you have more options than you can shake a stick at. I've tried several, and they all do pretty much the same thing in the same way, the big difference between them being the documentation. For me, the best documented of the lot is Apache CXF, which will generate JAX-WS-compliant code that you can then use with any JAX-WS library in your application (such as CXF itself, or Sun's JAX-WS-RI, and so on).
It's going to be more complex than SoapClient, I would imagine.
JAX-WS, most appserver vendors supply implementations and tooling.
See glassfish for an exmple implementation. The glassfish guide docs give details of how to go about developing, there are many variations depending upon how you would normally go about doing Java development. For me, I work with IBM tools and therefore simply load up RAD point as the WSDL and click "generate".
You can try using RCPServiceClient class in axis2.
Here is the java doc for it:
ws.apache.org/axis2/1_1_1/api/org/apache/axis2/rpc/client/RPCServiceClient.html
It doesn't require you to generate code to call web services.
Here is a decent example of how you can interact with a service using RPCServiceClient.
ws.apache.org/axis2/1_2/pojoguide.html#testingpojows
Do remember that java is strongly typed language so you can't do things like serviceClient->someRandomFunction(params), since someRandomFunction will not be defined ahead of time if you are trying to do something like PHP's SoapClient.
However, it will be equivalent to $soapClient->_soapCall function, as you will be able to do something like
serviceClient.invokeBlocking(
opProcess, -- function you are trying to call..
opResponseArgs, -- these are arguments you are passing
returnTypes); -- types that you are expecting back
Hope this helps.
Just wanted to ask if anyone knows of a reliable & easy way to expose a webservice (wsdl) as a webform to end-users.
Our team develops a lot of software for external contacts, which often involve creating a web service and exposing it as both a wsdl and a website. We'd like to automate this last step as much as possible (especially for the mindless data-pumping projects) so we can concentrate on developing web services instead of websites.
I recently came across Enunciate, but have not found the time yet to play with it in detail. I was wondering if anyone has any experience with it (or with any similar projects) what your findings are, and what direction you would advice us to take with this.
Best regards,
Tim
Update2:
Turns out what I need is perfectly described in this Enunciate module: http://jira.codehaus.org/browse/ENUNCIATE-23. The only problem is that it's not implemented... (yet?)
Anyone have any experience in creating your own modules, or knows of an existing solution that provides the same functionality?
I'm not sure how much weight my response carries (since I'm the lead developer of Enunciate) but I'd like to encourage you to try it out. I know a lot of happy Enunciate users.
Even if you decide not to use it, I'd like to hear about your experience and why you decided against it.
I've found that using the Metro libraries to provide the web service engine, a web page is produced when accessing the end point with a GET which allows you to easily get the WSDL if you need it.
I do not know if that is what you mean with a "web site" but it is a nice default way to work.