在专用的tomcat上将spring-boot应用程序部署为war文件是行不通的

我有一个spring-boot应用程序,现在我想在专用的tomcat服务器上部署应用程序而不使用嵌入式tomcat。 部署的两种方式我都没有收到任何错误。

我已经为spring-boot-starter-tomcat依赖项提供scope =

当我使用嵌入式tomcat运行应用程序(进行了适当的更改)时,它完全可以访问链接http://localhost:8080/testGET 。 但是当我运行带有scope =提供的专用tomcat时,点击链接http://localhost:8080/test-results-upload-1.0/testGEThttp://localhost:8080/testGET我得到了响应映射到/错误

请帮我解决一下这个。 无法理解我正在做的错误..

提前致谢。

  @EnableAutoConfiguration @Configuration @EnableWebMvc @ComponentScan("com............controller") @Import(SpringMongoConfig.class) public class BootStrap extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(BootStrap.class, args); } @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application) { return application.sources(BootStrap.class); } } 

  @Controller public class Controller { @Autowired(required = true) private IRepository config; @RequestMapping(value = "/testGET", method = RequestMethod.GET) public String testGet(HttpServletResponse response) { try { response.sendError(HttpStatus.OK.value()); return "Application working perfectly !"; } catch (IOException e) { e.printStackTrace(); return null; } } } 

  4.0.0 test-results-upload test-results-upload 1.0 war  1.1.5.RELEASE    org.springframework.boot spring-boot-starter ${spring.boot}   org.springframework.boot spring-boot-starter-tomcat ${spring.boot} provided   org.springframework.boot spring-boot-starter-web ${spring.boot}   org.springframework.boot spring-boot-starter-test ${spring.boot}   org.springframework.boot spring-boot-starter-actuator ${spring.boot}   org.springframework.boot spring-boot-starter-aop ${spring.boot}   org.springframework.data spring-data-rest-webmvc 2.1.4.RELEASE   javax.inject javax.inject 1   org.springframework.boot spring-boot-starter-data-mongodb ${spring.boot}   org.springframework.boot spring-boot-maven-plugin ${spring.boot} maven-plugin   com.fasterxml.jackson.core jackson-databind 2.4.2   com.fasterxml.jackson.core jackson-annotations 2.4.2   com.fasterxml.jackson.core jackson-core 2.4.2    src   maven-compiler-plugin 3.1  1.7 1.7    org.springframework.boot spring-boot-maven-plugin   maven-war-plugin 2.4  WebContent false     

顺便说一句,您可以通过在application.properties文件中设置以下属性,为独立的spring-boot应用程序添加上下文路径:

server.contextPath = /测试结果上传-1.0

这样,无论是部署独立容器还是通过外部容器,都将拥有相同的contextPath。

自从第一次提出这个问题以来,已经有一段时间了,分享了我使用Maven将Spring boot application打包为部署在Tomcat服务器( External )上的war所做的事情:

  1. Servlet 3.0 API容器的可部署war文件(意味着它可以在Tomcat 7.0.x以后正确部署)
    • 环境设置:
      • 需要安装Tomcat 7.0.x版本,因为从Tomcat 7.0.x开始可以获得对Servlet 3.0 API的支持。
      • Java 1.8(确保在相同的Java版本上构建(JDK8)并运行(JRE8 Tomcat JVM目标))。
        • 注意:Java 1.7也可以,只需确保在相同版本上编译和运行。
    • 码:
      • Boot Application类更改:按照扩展SpringBootServletInitializer的说明进行操作
      • Maven构建文件pom.xml更改:
        • spring-boot-starter-tomcat的范围更改为provided
        • 最后,我们需要将其捆绑为war文件,以便更改包装war

删除对response.sendError(HttpStatus.OK.value());的调用response.sendError(HttpStatus.OK.value());

首先,HTTP OK(200)响应不是错误,其次,HTTP OK是默认响应状态,因此不需要设置它。 如果要将其设置为OK以外的其他值,则应调用HttpServletResponse.setStatus

我通过从方法签名中删除(HttpServletResponse响应)来解决问题..其他一切都保持不变..

org.springframework.boot spring-boot-starter-tomcat provided

之后获取war文件并放入$ {TOMCAT_DIR} / webapps并点击url http://localhost:8080/test-results-upload-1.0/testGET并且它工作了!!!