我有一个spring-boot应用程序,现在我想在专用的tomcat服务器上部署应用程序而不使用嵌入式tomcat。部署的两种方式我都没有收到任何错误。
我已经为 spring-boot-starter-tomcat 依赖项提供了 scope = provided 。
当我使用嵌入式tomcat运行应用程序时(进行了适当的更改),它完全可以点击链接http://localhost:8080/testGET
。
但当我使用 scope =提供运行专用tomcat时,在点击链接http://localhost:8080/test-results-upload-1.0/testGET
或http://localhost:8080/testGET
时,我得到的响应映射到 / error
请帮我解决这个问题。无法理解我正在做的错误..
提前致谢。
@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;
}
}
}
<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>test-results-upload</groupId>
<artifactId>test-results-upload</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<spring.boot>1.1.5.RELEASE</spring.boot>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring.boot}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.boot}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${spring.boot}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>${spring.boot}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot}</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
答案 0 :(得分:1)
顺便说一下,您可以通过在application.properties文件中设置以下属性,为独立的spring-boot应用程序添加上下文路径:
server.contextPath = /测试结果上传-1.0
这样,无论您是单独部署还是通过外部容器,都将拥有相同的contextPath。
答案 1 :(得分:1)
自从第一次提出这个问题以来,已经有一段时间了,使用Maven分享我在Tomcat服务器(Spring boot application
)上部署war
打包为External
所做的工作: / p>
pom.xml
更改:
<artifactId>spring-boot-starter-tomcat</artifactId>
的范围更改为<scope>provided</scope>
<packaging>war</packaging>
答案 2 :(得分:0)
移除对response.sendError(HttpStatus.OK.value());
首先,HTTP OK(200)响应不是错误,其次,HTTP OK是默认响应状态,因此不需要设置它。如果您想将其设置为OK以外的其他内容,则应致电HttpServletResponse.setStatus
。
答案 3 :(得分:0)
我通过从方法签名中删除(HttpServletResponse响应)来解决问题..其他一切都保持不变..
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
之后获取war文件并放入 $ {TOMCAT_DIR} / webapps 并点击网址http://localhost:8080/test-results-upload-1.0/testGET
然后就可以了!!!