Need Resources to Learn Java Web Services from SCRATCH - java

I've bounced around Java and C# the last several years but now want to get up to speed with Web Services (RESTful). I used to be a master of J2EE circa 2003, but have since got side tracked into all kinds of stuff. I've worked through a couple of NetBeans tutorials (my IDE of choice) but they don't seem to expose the nitty gritty. I enjoy seeing how things exactly work, building an app from the byte array up.
Although I've touched a couple of frameworks, I've most enjoyed working with Java EE proper to understand the base case before the sugar of Spring/Hibernate is added.
Does that make sense? I am looking for a book or tutorial that really builds up your knowledge piece by piece so that you understand everything, not some Rails-like toolkit for rapid development.
Thanks-in-advance,
Guido

I'd start by doing a review of HTTP, since in practice this is a fundamental building block of REST web services, and it gets you into the right mindset for what comes next.
Next, think about how you'd structure the URIs for various resources. Martin Fowler wrote a good article on this topic. Various java frameworks for creating web services try to make it easier for you to map requests to specific methods, and even to map parts of requests (including path elements, query parameters, and the entire request body) straight into your object model. But all of this just help with the identification and manipulation of resources through URIs.
Next, if your resources are related, then think about hypermedia controls. You're probably used to putting <a href=""> links in web pages, and you certainly wouldn't want people to hardcode or bookmark every single URL in your website. How does the same idea extend to web services?
Next, think about actually moving data between client and server. JSON or XML? URI element or query parameter? Again, lots of java frameworks exist just to make this easier -- for example, Jackson and Gson for REST, JAXB for XML, etc. -- but the problem of dealing with HTTP-based requests and responses is very similar to the one that your average servlet container solved years ago.
Finally, consider security. For example, username/password or OAuth? HTTP or HTTPS?
There are lots of other details, but these topics should keep you busy for a while.

Have you checked out Jersey yet? This is the Java reference implementation of JAX-RS. There is a great community around this project, and the main author is available via mailing lists and forums and has personally answered questions of mine for some gnarly situations I ran into. The jersey site has a lot of great links to get you started from a variety of angles, depending on what you're interested in learning.

RestifyDayTrader has very good details of how to build a RESTFul service.
Java implementation can be found at Restified Trader

Java Web Services is a good read on learning Web Services.

Related

Can you write a web application with just the Java programming language?

My end-of-term assignment is to build a web-based student management application which connects to a MySQL database. However, the teacher does not allow us to use JS/ node because he says it is not OOP. Besides from JS, the members in our group can only use Java.
Therefore, I want to ask if it's it possible to do this assignment with just Java. If it is, what are the things I need to know and learn? Else, is it possible to learn PHP in 1 month for this kind of project?
Thank you very much for reading!
Yes it is possible to write a webserver in Java without any external (i.e. not Java SE) libraries. All you need to do is:
Learn about Socket and ServerSocket and the SSL stack, and ...
Spend a few days reading the HTTP specification in sufficient detail to understand what the protocol requires.
Spend a few more days implementing a server-side HTTP protocol stack and so on.
Which is ... a lot of effort, and probably a waste of you / your team's time.
If you are going to implement this in Java, you would be much better off either learning Spring / SpringMVC / SpringBoot, or learning Servlets and/or JSPs. They deal with the protocol side for you, and (more or less) leave you to focus on the aspects that are specific to your webapp.
These technologies (or equivalents) are what you are likely to use when you graduate.
If the other members of your team already know Java, that would be an obvious good reason to use it.
In short, there are 3 reasons to use Java + the above technologies:
Less effort
Less learning for your other team members1
You / they will be learning more immediately useful things.
PHP would be another alternative, though if your teachers are "down" on Javascript for being "not OO enough", they are probably unlikely to think well of PHP either.
On the topic of whether Javascript is OO or not, read this:
Is JavaScript object-oriented?
Read it and make up your own mind.
1 - How about volunteering to write all of the project's documentation so that you don't have to learn Java. No? You want to learn Java, don't you!
Sure you can write a web app in Java. Depending on what you're allowed to use there are tons of options. Just a short overview, further down means you need more 3rd party code but productivity goes up:
Build in on the JDK only. You'll need to build the WebServer on your own and compose HTML... (Only do this if requirements are strictly forbidding 3rd party libraries)
Use a servlet container like tomcat. You'll still have to build HTML on your own but request handling is mostly done for you. (Use this if a servlet container is all you're allowed to use)
Use a templating engine like JSP, Thymeleafe, Velocity. You will be able to build web pages quite easily. (This would already work conveniently, yet there are still better options)
Use an web application framework. This will take most boilerplate from you and allow you to use advanced concepts with little effort. There are multiple frameworks to choose from. I prefer Spring Boot and would recommend to start with s.th. like this (This will automatically provide much of what is asked from you, so check if you can use such a framework).

REST web services vs JSON services

REST webservices imply the server-side is stateless (for the most part) and urls correspond to resources and HTTP GET/POST/DELETE/PUT methods correspond to operations on those resources.
I am planning to work on a JSON services layer that is dependent on server-side state, having urls that correspond to resources and having urls like /add /update /delete corresponding to different operations and all of those urls using HTTP POST.
What is the right terminology for this? Should this be called JSON
web services? Is there any specific term for this?
It looks like Jersey+JAX-RS would be very useful for this purpose. Is it ok?
Would it make sense to use Struts for this kind of application?
JSON is a file format which your rest service may (or may not) use to send its answers. If you use this format to communicate with your webservice, you cann say that your service is a JSON web service.
Indeed, Jersey is a good JAX-RS implementation. However it is not the only one.
If you need a webapp to interact with the webservice you can use Struts. However, you can use any other tool (included some more recent technology, struts is really old) for the front-end of your system.
Well, it's not REST. However trying to defeat that tide of general misunderstanding just isn't going to happen.
What you are describing is actually what most of the world views as REST and therefor should be mostly OK with just calling it as such.
In the presence of a true Restifarian, you will get some push-back because they will be correct - however, there isn't really much need to add a tremendous amount of confusion to your daily life as the distinctions (and the MASSIVE benefits they afford...) are largely lost on anyone you work with.
(1) is accurate enough, or simply rest. (2) is redundant but it is a good framework for the technology. (3) I would say no, but noticing when this question was asked, I'm sure you already decided.

GWT: working with JSON services

It's not a very concrete question. I created a simple project with a help of this tutorial, it's really fine. All the GWT code samples related to JSON I saw so far seem to work with a JSON (or immitate this work with some mock-up JSON), that is retrieved and processed in GWT. I'm a newbie in GWT, and I wonder, what are the cases of interacting with services that return JSON (services are mentioned in the same tutorial) and what are the pros and cons of such interaction.
I thought about two options (well, service is an overloaded term):
everything that is mentioned in these JSON GWT tutorials is about the third-party services, like GData and Yahoo! Web Services mentioned here, which would make sense, cause it's about retrieving some data and processing in the app,
and the second option is about services, that are created within the confines of a project (and, if there are some cases, and definitely there are some, my question is about them).
It's probably can't be fully explained in the answer, so a link (or a few) would be appreciated. Thanks in advance.
Your question is really quite generic. But here are some pointers:
JSON is just a data interchange format similar to XML or Protocol Buffers or some other proprietary format.
They are necessary in modern web applications because the UI is entirely controlled by the javascript code running in the browser.
However the data that a web application presents to the user usually resides on the backend. In order to get the data from the backend to the frontend you have to use some data interchange format like JSON or XML.
The advantage of JSON is, that is fairly efficient compared to XML and well accepted.
As you mentioned there are third party services that rely on JSON. These are very useful when you want to include the services in your applications.
The biggest advantage of applying this service oriented approach to your own project is that you decouple your components (frontend and backend). By doing this you achieve following things:
Make your services available to other (web-)applications and users because your service exposes a specific API/data exchange format that they can use.
Easily replace or add another frontend (for example create a desktop application in addition to your GWT application) that can work with your data (display or modify).

How to consume ad hoc web services (non-SOAP, schemaless XML)?

I need to write integrations to multiple external web services. Some of them are SOAP (have WSDL), some of them pretty much ad hoc - HTTP(s), authentication either by basic auth or parameters in URL (!), natural-language like XML which does not really map nicely to domain classes..
For now, I've done the spike integrations using Spring Web 3.0 RestTemplate and binding using JAXB2 (Jaxb2Marshaller). Some kind of binding is needed because domain classes need to be cleaner than the XML.
It works, but it kind of feels bad. Obviously this partially just because how the services are built. And one minor issue I have is naming of RestTemplate as services have nothing to do with REST. This I can live with. JAXB2 feels a bit heavy though.
So, I'm looking for some other alternatives. Ideas? I'd like to have a simple solution (so RestTemplate is fine), not too enterprisey..
While some of your services may be schemaless XML, they will still probably have a well-documented API. One of the techniques that the Spring folks seem to be pushing, at least from the web-service server side, is to use XPath/XQuery for retrieving only the information you really need from a request. I know that this may only end up being part of your solution, but I'm not sure that this is a situation where one particular binding framework is going to meet all your needs.
If I understand correctly you have 1 application that has to make calls to various external (web) services by use of different technologies. The first thing that comes to mind is to have some intermediate level. While this could be something as elaborate as en ESB-solution, my guess is that is not what you're looking for.
You could for example achieve this intermediate level by having a class hierarchy with at its top an interface 'Consumer'. Method to be implemented: doConsume() and so on.
If you look into it you'll probably have the opportunity to make use of several design patterns like Strategy or Template. Remember to be pro-active and try to ask a few times 'What if ..' (As in: what if they need me to consume yet another service? etc.)
If JAXB feels too heavy there are other API's to be found:
Axis
JAX-WS
CXF
other
It'll depend on the situation which one would be better. If you run into troubles with any of them I'm sure you'll be able to find help here on SO (and from people who have more hands-on experience with them than me ;-)

Java for the web

I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help me out.
I read a few of the tutorials at sun.
The basic syntax I understand. The thing I don't understand and really need to (because I think my company is going to tell me start working with this) is to learn how to work with java for the web. I am a php programmer and I did some asp.net c# a few months ago. I am really not understanding how to work with java on the web.
I installed netbeans.
When I start a web project I also get asked what frameworks I want.
I don't know any java frameworks (I do know about spring but never really worked with it).
Is there no simple tutorial on how to do some java for the web??? I never had these much problems when I was first introduced to php :(
Someone help me out, or at least point me in a good direction.
You may find this question helpful.
All you really need to know is HTML, Java, and JSPs.
Creating dynamic content is very easy using JSPs and Java. You also need a webserver like Tomcat or Glass Fish.
As far as the back end Java code goes, there is no difference between that and a desktop application. The only thing that you are changing is the user interface. One of the biggest challenges with switching to a web interface is the fact that it is stateless by design.
The frameworks make web programming easier, they're not required. You can write web applications using plain old servlets and JSPs (with a web application container like Tomcat or JBoss) or even do all the HTTP I/O yourself (obviously that's pointless with Tomcat, etc, around).
A framework like Spring with Hibernate, etc, is similar to frameworks in PHP like CakePHP, they make development easier, they enforce sensible patterns (MVC) upon the programmer, the abstract code that is always going to be done the same way for this type of application. Yes, they limit your choices and abilities, and restrict the actions you can take, to varying degrees. Some are far more heavyweight than others. But there's a lot of choice in the Java world which is a strength, but you just have to spend time evaluating them, and picking one based upon your capabilities.
Take struts and tiles. You can simply use Struts as an MVC system and avoid all of the utility JSP tags it provides. It would take you a short time to rewrite the functionality you end up using yourself, but that's time you don't have, and then there's debugging, and specific domain knowledge and experience that you can't account for.
Are you familiar with MVC? There are multiple frameworks in Java that implement it. Struts is one of the more popular. It may help to start using Struts and work through a tutorial.
This tutorial is a good one that also will introduce you to spring.
This tutorial deals with struts in netbeans
By technical side, you can start downloading Tomcat and code the exercises and documentation.
By "architectural" sie, you code in Java the business logic, that is all the components that take the parameter by communication client-server (request), manipulate this and use this for create the result.
this result is give to communication server-client (response) and it appear, with presentation layer (JSP, etc) in the web page.
The explication is very preciseless and not very "orthodoxe", I know.
It's matter that you understand.
The programming model that is (in most cases) used when writing a Java web application is actually a subset of J2EE. Not a rocket science, but there's a need to know at least bits of it.
Get yourself familiar with concept of J2EE Web Container, J2EE Web application, it's descriptor (web.xml), Servlets (which are, in fact the basic "building stones" of any J2EE webapp) and, of course, JSPs.
Sun has many nice tutorials on their's web pages. Googling any of the above terms with "site:sun.com" usually brings good results (e.g. this one about the concept of Servlets: http://java.sun.com/products/servlet/articles/tutorial/ ).
I'd also forget about web frameworks first, and start with the J2EE stuff, then MVC as a design pattern. After that, go into the web frameworks. Use of a framework is usually of a great help, but even when using one, you still need to know the basics.
Since you're using NetBeans, start with the tuturials they have on their site.
http://www.netbeans.org/kb/docs/web/quickstart-webapps.html
Go through the simple tutorials on the site before you start going into the details on any frameworks.
Java webapps are pretty simple. You have your Java classes (.java) that handle your business logic and your .jsp files that handle the presentation. Try and keep them seperate. You also want to familiarize yourself with the JSTL tags and el (expression language) syntax

Categories

Resources