Aprender por ejemplo funciona para mí
Aquí hay un ejemplo rápido de Java 6 idiomático
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);
}
}
No se moleste con Java5, está obsoleto con respecto a Java6.
Siguiente paso, anotaciones. Estos solo definen aspectos de su código que permiten que los lectores de anotaciones completen la configuración de boilerplate por usted. Considere un servicio web simple que use la especificación JAX-RS (entiende URIs RESTful). No querrás molestarte en hacer todo el desagradable WSDL y jugar con Axis2, quieres un resultado rápido. Bien, haz esto:
// 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. Con un poco de magia de configuración en tu web.xml estás fuera. Si está construyendo con Maven y tiene configurado el plugin Jetty, su proyecto tendrá su propio servidor web de forma inmediata (sin necesidad de jugar con JBoss o Tomcat), y el código anterior responderá a los URI del forma:
GET http://localhost:8080/contextName/Service/the/raw/path/params
Trabajo hecho.