Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep server side Java and client side JS DTO properties consistent

Hi I’m looking for a solution/plugin in Eclipse to keep server-side Java DTO properties and their client-side JSON counterparts consistent throughout the evolution of the codebase. For example, for a webapp with a Java backend, with APIs exposed through a REST interface (using Jackson), the webapp could have something like this on the server:

the DTO:

public class Person {
    private String firstName;
    private String lastName;
    public Person(String string, String string2) {
        firstName = string; lastName = string2;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

The REST Service:

@Path("/data")
public class PersonService {
    @GET
    @Path("persons")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Person> getAssets() {
        List<Person> persons = new ArrayList<Person>();
        persons.add(new Person("Jimmy", "Hendrix"));
        persons.add(new Person("Roger", "Waters"));
        return persons;
    }
}

On the Client side, in Javascript/JQuery, we can have code like this:

$.ajax('/data/persons/', function(data){ for(var i = 0; i < data.length; i++){ var firstName = data[i].firstName; var lastName = data[i].lastName; //do some stuff to populate the view with the person data } });

This is all straightforward to set up. However, as the codebase evolves and changes (as it always does), suppose there is a need to change the names of the DTO fields from “firstName” and “lastName”, to “foreName”, and “surName”.

In Eclipse, refactoring all of the Java code is simple using the Refactor menu item, which will find ALL references of the method/field in the Java code, and replace them. Note that Eclipse’s “Refactor…” is different than a Find/Replace action. Find/Replace does a basic text replace on all files specified. Refactor on the other hand, takes into account that Java is a strongly typed programming language, and searches for all method invocations with that signature.

It would be great if there was some plugin or Eclipse feature that would be smart enough to change the reference to “firstName” and “lastName” in the Javascript code as well. That would save developers the time of having to do a Refactor for just the Java code, and then a selective Find/Replace in the Javascript code, and reduce potential runtime errors. Does anyone know if such a tool/plugin exists?

like image 894
Lev Avatar asked Jun 06 '15 17:06

Lev


1 Answers

Yeah, me too. This seems like a pattern I encounter at every job.

Best I've done in the past is generate Javascript stubs from the java DTOs and used JsDoc to indicate where those javascript DTOs are used.

If I was doing that same solution today, I'd probably see what Swagger codegen would give me out of the box.

Intellij at least will highlight missing fields in the javascript, which is helpful.

It would be great if there was a plugin, but this was better than nothing.

With typescript you'd even get compile safety with the stubs.

like image 93
Michael Haefele Avatar answered Oct 21 '22 06:10

Michael Haefele