1

How do I setup my Spring application to use hosts / domains correctly?

I am fairly new to Spring so hopefully I am describing my setup adequately to you.

My Spring setup has Tomcat built in via the configuration file WEB-INF/web.xml and WEB-INF/mvc-dispatcher-servlet.xml and it autogenerates / listens on localhost:8080/spring-ng-seed (the name of the project for now).

If I put into my /etc/host file:

127.0.0.1 spring-ng-seed.dev
127.0.0.1 api.spring-ng-seed.dev

then I still have to type into my browser spring-ng-seed.dev:8080/spring-ng-seed/index.html

How can I loose the port number and the app name prefix before index.html? (:8080/spring-ng-seed)

Also, is it possible to have my API only respond on api.spring-ng-seed.dev and not on spring-ng-seed.dev ? i.e have:

1) spring-ng-seed.dev only serve the NG app

2) api.spring-ng-seed.dev only serve the API

My WEB-INF/mvc-dispatcher-servlet.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd
    "
>

    <context:component-scan base-package="seed.rest.api"/>

    <mvc:resources mapping="/**" location="/app/build/"/>

    <mvc:annotation-driven/>

    <security:global-method-security pre-post-annotations="enabled">
        <security:protect-pointcut expression="execution(* seed.rest.api.*.*.**(..))"
                                   access="ROLE_DUMMY"/>
    </security:global-method-security>

</beans>

My WEB-INF/web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
    "
    version="3.0"
>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/business-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

If there is any other configurations that you need from my project ill happily post them.

Thank you for your time.

Reece Fowell
  • 113
  • 1
  • 10

1 Answers1

0

To skip typing in the port into the browser you can change tomcat to run on port 80 (see How to change the port of Tomcat from 8080 to 80?)

To map apps to subdomains consider using apache (which can also perform the first part of your question. see http://squirrel.pl/blog/2010/03/30/mapping-tomcat-apps-to-subdomains-with-apache/)

Community
  • 1
  • 1
jbwt
  • 384
  • 5
  • 14
  • I don't have a `server.xml` file. My tomcat is in the project from my `pom.xml` and configured in `web.xml` From what I can see, this is for if we have tomcat installed on the system instead of being embedded into the project like I have. If I paste the ` ` in my `web.xml` or `mvc-dispatcher-servlet.xml` it does nothing. – Reece Fowell Mar 30 '16 at 04:56