0

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:

  • context path: /context-path/servlet-path/service/id
  • servlet path: /servlet-path/service/id
  • request mapping only: /service/id

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/.

pkalinow
  • 1,619
  • 1
  • 17
  • 43

1 Answers1

1

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
shazin
  • 21,379
  • 3
  • 54
  • 71
  • What about the first option (the whole path incl. context path)? For me it looks simpler; it can be taken from the request with combination of HttpServletRequest.getContextPath() and ...getServletPath(), or using Spring's UriComponentsBuilder.build().getPath(). – pkalinow Oct 12 '15 at 11:07