Adam Bien's Weblog

Saturday Oct 25, 2014

Enterprise Nashorn with Java 8 at JavaOne 2014

See also other screencasts at: http://tv.adam-bien.com or subscribe to http://www.youtube.com/user/bienadam.

See you at Java EE Workshops at Munich Airport or on demand and in a location very near you: airhacks.io!


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Friday Oct 24, 2014

JAX-RS: Returning A List Of Instances, Problem and Solution

Wrapping a list of instances with a Response:


    @GET
    public Response workshops() {
        List<Workshop> workshops = ...//a list of entities
        return Response.ok(workshops).build();
    }


Leads to a type loss carried by the Collection and the following (or similar) exception:


MessageBodyWriter not found for media type=application/json, type=class java.util.Arrays$ArrayList, 
genericType=class java.util.Arrays$ArrayList

JAX-RS comes with GenericEntity which carries the generic type. You only have to wrap the Collection with the GenericEntity to solve the problem:


import javax.ws.rs.core.GenericEntity;
//...
  @GET
    public Response workshops() {
        List<Workshop> workshops = ...//a list of entities
        GenericEntity<List<Workshop>> l
        ist = new GenericEntity<List<Workshop>>(workshops) {
        };
        return Response.ok(list).build();
    }

See you at Java EE Workshops at Munich Airport, particularly at: Effective Java EE 7!


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Thursday Oct 23, 2014

Rethinking Packaging, Modularization, Interfaces with Plain Java EE 7: 33rd Degree Conference

See also other screencasts at: http://tv.adam-bien.com or subscribe to http://www.youtube.com/user/bienadam.

See you at Java EE Workshops at MUC Airport, particularly at the Java EE Architectures workshop!


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Wednesday Oct 22, 2014

CLI, Docker and Lambdas: Nashorn ...and Java EE

Java 8 comes with fast JavaScript engine called "Nashorn". At this JavaDay 2014 session I'm presenting examples and ideas for using Nashorn in enterprise applications with less code and more flexibility. Command line automation, docker monitoring and bi-directional Java integration:

I wondered during this session, why the audience is so passive. After the session I was told about the deactivation of the wifi to ensure HD streaming quality :-).

See also other screencasts at: http://tv.adam-bien.com or subscribe to http://www.youtube.com/user/bienadam.

See you at Java EE Workshops at Munich Airport or on demand and in a location very near you: airhacks.io!


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Tuesday Oct 21, 2014

Lazy Injection with javax.inject.Provider

javax.inject.Provider is the minimalistic version of the javax.enterprise.inject.Instance interface. In fact Instance inherits from Provider

For on-demand injection of components / resources inject them as Provider:


@Model
public class Index {

    @Inject
    Provider<Boundary> boundary;

    public String getMessage() {
        return boundary.get().message();
    }

}

The injected component is unaware about the laziness:


@Stateless
public class Boundary {

    public String message() {
        return "Good morning";
    }
}

Instance comes with additional functionality: see also Interfaces on Demand with CDI and EJB 3.1

[See also an in-depth discussion in the "Real World Java EE Patterns--Rethinking Best Practices" book (Second Iteration, "Green Book"), page 235 in, chapter "Plugin"]

See you at Java EE Workshops at Munich Airport, particularly at: Effective Java EE 7!


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Monday Oct 20, 2014

JavaFX: A Composite View with WYSIWYG

Creating a composite view in JavaFX with Scene Builder and Dependency Injection:

See also: http://afterburner.adam-bien.com and see you at http://workshops.adam-bien.com/javaee-ui.htm.


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Saturday Oct 18, 2014

Setting The Classpath For Nashorn Script in jjs / Java 8

The Java 8 jjs interpreter accepts the -cp / -classpath argument. After setting the classpath, all the classes become available from within the executable script.

A utility class com.airhacks.naslib.Inspector in the naslib.jar (this is an sample jar) becomes available for Nashorn after setting the jjs classpath:


#!/usr/bin/jjs -cp ./naslib/target/naslib.jar -fv
//built-in variable
var args = $ARG;

var Inspector = com.airhacks.naslib.Inspector;
//class from jar, calling a static method
Inspector.inspect(args);

In case this post looks a bit crazy to you, you would enjoy the Java 8 / Java EE 7 "More Power with Less Code" workshop :-).


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Friday Oct 17, 2014

Java 8, Nashorn, Nanoservices, JavaFX On-Stage Hacking and Airhacks -- End Of Year Events

See also the the Java EE Microservices workshop in January 2014.


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Wednesday Oct 15, 2014

Star 7 feat. James Gosling: The Origins of Java ...and iPhone?

Connected and portable touch devices and color screens in ...1992. Java's origins and probably the first appearance of the duke:


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Tuesday Oct 14, 2014

Named Parameters in Java 8

With Java 8 and the compiler flag: javac -parameters method parameter names are available via reflection. For example: the parameter names of the method hello:


public class Boundary {

    public void hello(String name, int age) {

    }
}

become accessible to the following code:


    public static void main(String[] args) {
        Method[] methods = Boundary.class.getMethods();
        for (Method method : methods) {
            System.out.print(method.getName() + "(");
            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                System.out.print(parameter.getType().getName() + " " + parameter.getName() + " ");
            }
            System.out.println(")");
        }
    }
}

Compilation with javac -parameters produces the following output:


hello(java.lang.String name int age )

Without the -parameters flag the names are not available:


hello(java.lang.String arg0 int arg1 )


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

realworldpatterns.com
...the last 150 posts
...the last 10 comments
License