The problem is not in making a PL/PgSQL function returning an UUID. Or even returning a custom type containing an UUID (like MYFUNC_RETURN_TYPE in my case). The problem is on Java level, when I need to make a Java handler/mapping of PostgreSQL custom type (which contains an UUID internally).
For now I, instead of out.writeObject, made it through out.writeString(key.toString()), but I am very unsure if this is how it should be done. And if the opposite way, key = (UUID) in.readObject() is also safe.