Learning by example works for me
Here is a quick example of idiomatic Java 6
public class Main {
public static void main(String[] args) {
// Shows a list forced to be Strings only
// The Arrays helper uses generics to identify the return type
// and takes varargs (...) to allow arbitary number of arguments
List<String> genericisedList = Arrays.asList("A","B","C");
// Demonstrates a for:each loop (read as for each item in genericisedList)
for (String item: genericisedList) {
System.out.printf("Using print formatting: %s%n",item);
}
// Note that the object is initialised directly with a primitive (autoboxing)
Integer autoboxedInteger = 1;
System.out.println(autoboxedInteger);
}
}
Don't bother with Java5, it's deprecated in respect to Java6.
Next step, annotations. These just define aspects to your code that allow annotation readers to fill in boilerplate configuration for you. Consider a simple web service that uses the JAX-RS specification (it understands RESTful URIs). You don't want to bother doing all the nasty WSDL and mucking about with Axis2 etc, you want a quick result. Right, do this:
// Response to URIs that start with /Service (after the application context name)
@Path("/Service")
public class WebService {
// Respond to GET requests within the /Service selection
@GET
// Specify a path matcher that takes anything and assigns it to rawPathParams
@Path("/{rawPathParams:.*}")
public Response service(@Context HttpServletRequest request, @PathParam("rawPathParams") String rawPathParams) {
// Do some stuff with the raw path parameters
// Return a 200_OK
return Response.status(200).build();
}
}
Bang. With a little sprinkle of configuration magic in your web.xml you're off. If you're building with Maven and have the Jetty plugin configured, your project will have it's own little web server right out of the box (no fiddling about with JBoss or Tomcat for you), and the above code will respond to URIs of the form:
GET http://localhost:8080/contextName/Service/the/raw/path/params
Job done.