maven插件spring-boot-starter-tomcat的使用方式

2022-11-13 12:11:02 spring maven 插件

tomcat内嵌到web项目中

将tomcat 内嵌到 web项目中,这样可以直接运行 webapp项目。

不需要再部署到额外的tomcat,直接就可以运行了。

1.pom.xml 配置

    <plugins>
      <plugin>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-tomcat</artifactId>
		    <version>2.3.0.RELEASE</version>
       </plugin>
    
    </plugins>

2.tomcat使用maven内嵌入到web项目需要jdk运行环境

eclipse默认的运行环境式jre,我们需要改成jdk

问题解决:

3.springmvc依赖

Spring V4.1以后的版本运行时会报如下错误:

NoSuchMethodError: javax.servlet.Http.httpservletResponse.getStatus()

我们使用Spring V4.1以前的版本运行,依赖如下

<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.0.1.RELEASE</version>
</dependency>

导入对象转JSON的依赖

<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-annotations</artifactId>
	    <version>2.9.5</version>
	</dependency>
	
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-core</artifactId>
	    <version>2.9.5</version>
	</dependency>
	
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-databind</artifactId>
	    <version>2.9.5</version>
	</dependency>

在pom.xml中配置jdk版本

   <properties>
	 	<maven.compiler.source>1.8</maven.compiler.source>
    	<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

4.编写表现层代码

@RestController
public class UserController {
	@RequestMapping("query")
	public Map<String, Object> query(){
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		return map;
	}
		
}

5.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>TomcatDemo</display-name>
  
                 <!-- 编码级过滤器 -->
	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
  <!-- 配置SpringMVC的核心控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>ContextConfigLocation</param-name>
  		<param-value>classpath:spring.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

6.SpringMVC的配置文件

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop 
		http://www.springframework.org/schema/aop/spring-aop.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd">
		
	<!-- 扫描具有注解的类 -->
	<context:component-scan base-package="com.wn"></context:component-scan>
	<!-- 使SpringMVC的注解生效 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 配置静态资源过滤器 -->
	<mvc:resources location="/" mapping="/**"></mvc:resources>
</beans>

7.配置完成后在项目上鼠标右键

Run As->Maven build->输入tomcat启动命令启动tomcat

tomcat:run

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

相关文章