Frage

I have an existing Spring web application, and then I started a separate Spring Roo application that is basically just a rest api, which the main application can use, as well as third party developers. I have two problems with this setup:

  1. Almost all of the application's work is done from the client through AJAX, so the rest api works well, but there are times when the primary application also needs to access some of these new services from the rest api, but they're separate applications, so that would mean using a resttemplate or rmi within my own application..

  2. Having the rest api as a separate app also means its deployed to a separate subdomain. This is nice for splitting http requests, but not nice for requests to the api that need to be behind authentication, as that means redirection to yet another domain for single sign in, which causes the ajax requests to fail.

It feels nice and clean to have this all separated, but it's also starting to become annoying. I could combine the two projects and all problems would be solved. Recommendations?

War es hilfreich?

Lösung

As requested in the comments, I've included an example of how to server 2 separate webapps via the same sub-domain using Apache ProxyPass and ProxyPassReverse.

The way I've done it is with standard HTTP proxy but its also possible to do with AJP connector (I believe). I think this requires one of the webapps to be running under a different tomcat (due to server.xml requiring a unique Host entry - see below but I'm not 100% sure about this).

Add the following to your Apache conf file (e.g. vhosts.conf or vhosts.d/servername.conf):

<VirtualHost *:80>
     ServerName 127.0.0.1
     ServerName externalName1

     ProxyRequests Off

     # AJP example
     #ProxyPreserveHost On
     #ProxyPass / ajp://localhost:8009/
     #ProxyPassReverse / ajp://localhost:8009/

     ProxyPass         /ws   http://internalName1:8080/webapp1/
     ProxyPassReverse  /ws   http://internalName1:8080/webapp1/
     ProxyPass         /     http://internalName2:8080/webapp2/
     ProxyPassReverse  /     http://internalName2:8080/webapp2/

</VirtualHost>

Because the order of the ProxyPass entries decides the precedence, any requests for /ws/* will always go to the first server. But the first server has to have a /ws controller to respond to these requests - in other words /ws is NOT the context path of the app but an internal path to the webapp. webapp2 can contain paths that include /ws but they will simply be ignored as all requests will go to webapp1.

Each webapp needs a Host entry in the tomcat server.xml file, something like:

<Host name="externalName1" debug="0" appBase="webapps/webapp1" unpackWARs="true" autodeploy="true">
    <Alias>serverName1AltName</Alias>
    <Context path="" docBase="" debug="5" reloadable="true"/>
</Host>

I've not configured this myself for quite a few years and have pulled the info from a few servers we have running, so I apologise if there are any mistakes or errors.

Andere Tipps

I recommend you to merge both projects in only one:

  • Roo won't actuate on classes without Roo annotations
  • You won't need integration API
  • Both are Spring so you should not have merging problems, but you must maintain the project layout of Roo app project
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top