如何设置我的 Spring 应用程序以正确使用主机/域?
我是 Spring 的新手,所以希望我向您充分描述了我的设置。
我的 Spring 设置通过配置文件 WEB-INF/web.xml
和 WEB-INF/mvc-dispatcher-servlet.xml
内置了 Tomcat,它会自动生成/监听 localhost:8080/spring-ng-seed
(目前项目的名称)。
如果我放入我的/etc/host
文件:
127.0.0.1 spring-ng-seed.dev
127.0.0.1 api.spring-ng-seed.dev
然后我仍然需要在我的浏览器中输入 spring-ng-seed.dev:8080/spring-ng-seed/index.html
如何在 index.html 之前去掉端口号和应用名称前缀? (:8080/spring-ng-seed
)
此外,是否可以让我的 API 仅响应 api.spring-ng-seed.dev
而不是 spring-ng-seed.dev
?即有:
1) spring-ng-seed.dev
只服务于 NG 应用
2) api.spring-ng-seed.dev
只提供 API
我的 WEB-INF/mvc-dispatcher-servlet.xml
文件:
<?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>
我的 WEB-INF/web.xml
文件
<?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>
如果您需要我的项目中的任何其他配置,我会很高兴地发布它们。
感谢您的宝贵时间。
最佳答案
要跳过在浏览器中输入端口,您可以将 tomcat 更改为在端口 80 上运行(请参阅 How to change the port of Tomcat from 8080 to 80?)
要将应用程序映射到子域,请考虑使用 apache(它也可以执行您问题的第一部分。请参阅 http://squirrel.pl/blog/2010/03/30/mapping-tomcat-apps-to-subdomains-with-apache/)
关于java - 为 API 和 Angular 内容设置 spring 主机域作为主域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36296107/