Link to home
Start Free TrialLog in
Avatar of Jack McKenzie
Jack McKenzie

asked on

Java Spring MVC Project: How to delegate all frontend to an external team (The backend is secret. The frontend uses Freemarker)

There is a Java Spring MVC project and a requirement to delegate the frontend to an external team of frontend developers. The backend is secret (proprietary trading system) and the frontend is therefore the only part of it that can be disclosed (Freemarker templates with HTML + CSS, JS, images).

I figured node.js has a freemarker library and the express server. It can render Freemarker templates:

const express = require('express')
const fs = require('fs');
const app = express()
const port = 3000

const Freemarker = require('freemarker');	 
const freemarker = new Freemarker();

app.get('/', (req, res) => {
	var model = {
		title: 'Test render',
		test: 'freemarker in JS!'
	};	

	console.log("Opening /");
	fs.readFile('main.ftlh', 'utf8', function(err, contents) {
		freemarker.render(contents, model, (err, result) => {
		  if (err) {
			throw new Error(err);
		  }
		  		  
		  res.send(result);
		});
	});	
})

app.listen(port, () => { } /*console.log(`Example app listening on port ${port}!`)*/)

Open in new window


Is there any quick solution to create mock controllers? Mock controllers should have some hardcoded data in their model just to make it possible to render the views.
Ideally, they would be generated somehow from the original Java controllers. I know regular expressions may be able to do it, but is there perhaps a better way? Some tool?
Avatar of girionis
girionis
Flag of Greece image

You can use wiremock, hoverfly or Mock Server. I have used wiremock with success in the past.
Avatar of Jack McKenzie
Jack McKenzie

ASKER

Simulators for HTTP API such as REST and SOAP are useful, but this question is specifically about transforming Java controllers into mocks for node.js with the goal to render every page using least effort.
If you're only looking to serve requests from node.js through mock controllers, yes you are right, my comment is irrelevant.

But you can also use node.js to issue a request to a mock server and return the result. By doing this it makes things much simpler, since you do not need to mock the controllers. Node.js would be completely dumb, it will just be a proxy between the front end and the mock server.
Can you illustrate how that solution could work?

as-is: controllers in Spring with methods (routes). Every method may set model attributes for the specific route (for example "/list" vs "/detail/id/1").

to-be: transformed controllers from Java into node.js (using as much automation as possible) to recreate the same controller names and the same GET/POST routes. ideally, also add some draft of every model variable set to "" in JS. Model variables should exist in their appropriate actions, encapsulated in the right controllers.

Synchronization: a new model attribute is added in Java. The solution should compare Java with JS and add a new variable to JS as well.

For this transformation, and automated synchronization from Java to JS, what solution do you propose?

The naive way would be using regular expressions and simple scripting to extract those fields and do the transformation.
Just to make sure I understand your question completely.

1) You have a Spring controller that returns a ModelAndView.
2) The view is an HTML freemarker template
3) In the model you set the attributes used in the freemarker template.
4) The front end team has the freemarker templates (along with CSS, JS and images).
5) You need to only mock the model (i.e. the attributes in the model passed to the view (the freemarker HTML file in this case)).

Is this correct?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.