pkalinow
pkalinow

Reputation: 1741

What is the correct base for relative URLs in Spring MVC application?

I have a Spring MVC application with REST controllers, which works as a back-end for a JavaScript, Angular-based front-end application. It is sometimes necessary to return to the front-end a relative URL to a resource.

The question is: what should the relative URL start from?

Assuming that the absolute URL is http://host:port/context-path/servlet-path/service/id, I see the following options:

The front-end application is deployed in the same Web application as the back-end, but in a different directory, e.g. http://host:port/context-path/gui-app/.

Upvotes: 1

Views: 1375

Answers (1)

shazin
shazin

Reputation: 21883

Since Single Page Web Application like Angular based ones communication to the Backend only via an API like REST and doesn't know any other information except the URL of the REST API, You need to store the context path variable in your Front End.

This can be stored in either a JSON configuration file or in a global Javascript file which is shared by all Angular JS controllers.

One Advice for the backend is to use a api/<version> prefix before the REST Controller names which will make it easier to distinguish the REST API from other URLs.

 http://host:port/context-path/api/v1/servlet-path/service/id

Upvotes: 1

Related Questions