We got a project where we are using multiple databases with Spring Boot as backend and AngularJS as frontend. We use our own exchange maven package that tells us about the structure of the database like the following:
public class Place {
public UUID placeID;
public LocalDate start;
public LocalDate end;
public String network;
public String placeName;
public String placeNumber;
public String project;
}
A very basic class for example and we want to convert this to JavaScript so we can also use them in our frontend. Because we are using Camunda as engine we need to access the same variable names in front- and backend. For that purpose we have created a Java class with constants so that we can reuse them at different places without struggling with any uppercase for example:
public class CamVariable {
// Process
public static final String ASSIGNEE = "assignee";
public static final String NAME = "name";
public static final String PLACE = "place";
}
Currently we need to maintain the same variable also in JavaScript to have them there which is redundant and really not a good approach. But we don't know how to do it different and that's why I'm asking how we can maybe convert the Java class to some code thats readable for JavaScript.
const CamVariable = {
AREA: 'area',
AREA_ID: 'areaID',
};
CodePudding user response:
It depends.
As there is Web Development among the topics and there is no NodeJS, my assumption is that this is Web and HTTP communication related question. So the JavaScript is HTTP client side run by the browser and Java class is server side e.g. a Servlet.
In Java there is request and response objects. To pass a value back to the client what you do is write the value as string to the response.
response.getWriter().append(value.toString());
CodePudding user response:
If your web-server exposes API and your DTOs are valid API models, you can expose it through Swagger description. Integrate Swagger in SpringBoot is very easy: https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api and can be done almost declaratively.
Having this done, you already can represent your API and DTOs in a machine-friendly description language (JSON), so can validate/use/build your front-end model against server DTO representation by casting it into JS objects.
For casting can be used swagger parser https://github.com/APIDevTools/swagger-parser
SwaggerParser.validate(myAPI, (err, api) => {
console.log("API name: %s, Version: %s", api.info.title, api.info.version);
});
In this example your api
variable will contains the definitions
property with all your public server DTOs, so you can dynamically use it. Or, if you need it at compile time - generate these objects as part of the pre-build action.
Hope this helps.