我正在使用springboot 1.5.9,而Tomcat的extarnal版本是8.5.7。首先,我使用Spring Initializr
生成初始包,而不选择任何选项。在我创建一个类控制器并使用一个返回String的端点(Get方法)编写一个方法之后。
package com.test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value = "/test")
public String index() {
return "REST API HERE ";
}
}
如果我使用嵌入式tomcat运行api并与postman打电话,一切正常。
:: Spring Boot :: (v1.5.9.RELEASE)
2018-01-25 20:50:29.954 INFO 5944 --- [ main] com.test.TestApplication : Starting TestApplication on Alvaro with PID 5944 (C:\Projects\dev\test\target\classes started by Alvaro in C:\Projects\dev\test)
2018-01-25 20:50:29.956 INFO 5944 --- [ main] com.test.TestApplication : No active profile set, falling back to default profiles: default
2018-01-25 20:50:29.982 INFO 5944 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3439f68d: startup date [Thu Jan 25 20:50:29 CET 2018]; root of context hierarchy
2018-01-25 20:50:30.697 INFO 5944 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-01-25 20:50:30.709 INFO 5944 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
018-01-25 20:50:30.710 INFO 5944 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23
2018-01-25 20:50:30.768 INFO 5944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-01-25 20:50:30.768 INFO 5944 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 787 ms
2018-01-25 20:50:30.865 INFO 5944 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-01-25 20:50:30.868 INFO 5944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-01-25 20:50:30.869 INFO 5944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-01-25 20:50:30.869 INFO 5944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-01-25 20:50:30.869 INFO 5944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-01-25 20:50:31.040 INFO 5944 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3439f68d: startup date [Thu Jan 25 20:50:29 CET 2018]; root of context hierarchy
2018-01-25 20:50:31.077 INFO 5944 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test]}" onto public java.lang.String com.test.TestController.index()
2018-01-25 20:50:31.079 INFO 5944 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-01-25 20:50:31.079 INFO 5944 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-01-25 20:50:31.096 INFO 5944 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-25 20:50:31.096 INFO 5944 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-25 20:50:31.116 INFO 5944 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-25 20:50:31.191 INFO 5944 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-01-25 20:50:31.234 INFO 5944 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-01-25 20:50:31.236 INFO 5944 --- [ main] com.test.TestApplication : Started TestApplication in 1.434 seconds (JVM running for 1.633)
2018-01-25 20:54:31.002 INFO 5944 --- [nio-8080-exec-4] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-01-25 20:54:31.002 INFO 5944 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-01-25 20:54:31.011 INFO 5944 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 9 ms
Test with postman with enbedded tomcat
之后我生成战争(使用包)并放入外部tomcat。当tomcat部署战争时,没有错误。
2018-01-25 21:14:29.713 INFO 8508 --- [ost-startStop-1] com.test.ServletInitializer : Starting ServletInitializer v0.0.1-SNAPSHOT on Alvaro with PID 8508 (C:\apache-tomcat-8.5.27\webapps\test-0.0.1-SNAPSHOT\WEB-INF\classes started by Alvaro in C:\apache-tomcat-8.5.27\bin)
2018-01-25 21:14:29.714 INFO 8508 --- [ost-startStop-1] com.test.ServletInitializer : No active profile set, falling back to default profiles: default
2018-01-25 21:14:29.754 INFO 8508 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@562627a1: startup date [Thu Jan 25 21:14:29 CET 2018]; root of context hierarchy
2018-01-25 21:14:30.390 INFO 8508 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 636 ms
2018-01-25 21:14:30.708 INFO 8508 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-01-25 21:14:30.710 INFO 8508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'errorPageFilter' to: [/*]
2018-01-25 21:14:30.710 INFO 8508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-01-25 21:14:30.710 INFO 8508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-01-25 21:14:30.710 INFO 8508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-01-25 21:14:30.710 INFO 8508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-01-25 21:14:31.002 INFO 8508 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@562627a1: startup date [Thu Jan 25 21:14:29 CET 2018]; root of context hierarchy
2018-01-25 21:14:31.068 INFO 8508 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test]}" onto public java.lang.String com.test.TestController.index()
2018-01-25 21:14:31.071 INFO 8508 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-01-25 21:14:31.072 INFO 8508 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-01-25 21:14:31.094 INFO 8508 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-25 21:14:31.094 INFO 8508 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-25 21:14:31.125 INFO 8508 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-25 21:14:31.219 INFO 8508 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-01-25 21:14:31.232 INFO 8508 --- [ost-startStop-1] com.test.ServletInitializer : Started ServletInitializer in 1.854 seconds (JVM running for 3.885)
2018-2018 21:14:31.249 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\apache-tomcat-8.5.27\webapps\test-0.0.1-SNAPSHOT.war] has finished in [3.429] ms
2018-2018 21:14:31.250 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Despliegue del directorio [C:\apache-tomcat-8.5.27\webapps\docs] de la aplicaci¾n web
2018-2018 21:14:31.262 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\apache-tomcat-8.5.27\webapps\docs] has finished in [12] ms
2018-2018 21:14:31.262 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Despliegue del directorio [C:\apache-tomcat-8.5.27\webapps\examples] de la aplicaci¾n web
2018-2018 21:14:31.400 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\apache-tomcat-8.5.27\webapps\examples] has finished in [138] ms
2018-2018 21:14:31.401 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Despliegue del directorio [C:\apache-tomcat-8.5.27\webapps\host-manager] de la aplicaci¾n web
2018-2018 21:14:31.414 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\apache-tomcat-8.5.27\webapps\host-manager] has finished in [14] ms
2018-2018 21:14:31.414 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Despliegue del directorio [C:\apache-tomcat-8.5.27\webapps\manager] de la aplicaci¾n web
2018-2018 21:14:31.426 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\apache-tomcat-8.5.27\webapps\manager] has finished in [12] ms
2018-2018 21:14:31.426 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Despliegue del directorio [C:\apache-tomcat-8.5.27\webapps\ROOT] de la aplicaci¾n web
2018-2018 21:14:31.437 INFORMACIËN [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\apache-tomcat-8.5.27\webapps\ROOT] has finished in [11] ms
2018-2018 21:14:31.440 INFORMACIËN [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
2018-2018 21:14:31.447 INFORMACIËN [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
2018-2018 21:14:31.449 INFORMACIËN [main] org.apache.catalina.startup.Catalina.start Server startup in 3656 ms
但是当我和邮递员做同样的文字时,我收到404错误:
Postman error with the war deploy in an external Tomcat
这是pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0 </modelVersion>
<groupId>com </groupId>
<artifactId>test </artifactId>
<version>0.0.1-SNAPSHOT </version>
<packaging>war </packaging>
<name>test </name>
<description>test </description>
<parent>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-parent </artifactId>
<version>1.5.9.RELEASE </version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
<java.version>1.8 </java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-web </artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-tomcat </artifactId>
<scope>provided </scope>
</dependency>
<dependency>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-test </artifactId>
<scope>test </scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
</project>
我迷路了,因为我不知道问题是外部tomcat配置还是springboot包。
我查了很多东西:
我按照所有步骤使用springboot部署外部战争(事实上,war部署没有错误)就像这个文档表明:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html
我检查了外部tomcat中的端口8080是否已打开。
我在不同的tomcats中测试过(一个在windows中,两个在linux中)。
我检查了java版本。
但似乎没有任何效果。
如果您需要更多信息,请问我。谢谢你的帮助!
我在test / src / main / webapp / WEB-INF / web.xml中添加了以下web.xml。但问题还在继续。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<context-param>
<param-name>contextConfigLocation </param-name>
<param-value>test.Application </param-value>
</context-param>
<listener>
<listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener </listener-class>
</listener>
<filter>
<filter-name>metricsFilter </filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy </filter-class>
</filter>
<filter-mapping>
<filter-name>metricsFilter </filter-name>
<url-pattern>/* </url-pattern>
</filter-mapping>
<servlet>
<servlet-name>appServlet </servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<init-param>
<param-name>contextAttribute </param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT </param-value>
</init-param>
<load-on-startup>1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet </servlet-name>
<url-pattern>/ </url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
默认情况下,Tomcat将使用WAR的名称作为部署应用程序的上下文。在您的情况下,上下文名称将为test-0.0.1-SNAPSHOT
,因此访问您应用的网址将为http://localhost:8080/test-0.0.1-SNAPSHOT/。
如果您希望在根部署您的应用,则可以将文件命名为ROOT.war
。您还可以在context.xml
文件中指定不同的上下文名称(包含在战争中或战争外部)。