Spring Boot 使用 SpringMVC

使用SpringMVC最简单的方法是在pom.xml中加入spring-boot-starter-web依赖,这样Spring Boot的AutoConfiguration模块将为我们自动进行SpringMVC的配置,创建好RequestMappingHandlerAdapterRequestMappingHandlerMapping等,详情可以参考org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfigurationDelegatingWebMvcConfiguration的源码。

这个时候就可以定义如下这样一个控制器,当请求/hello/json时将返回{"key1": "value1", "key2": "value2"}这样一段JSON。当Classpath下存在jackson相关的Class时就会自动添加MappingJackson2HttpMessageConverter这样一个HttpMessageConverter。关于默认添加的HttpMessageConverter可以参考WebMvcConfigurationSupportaddDefaultHttpMessageConverters()的源码说明。

@RestController
@RequestMapping("hello")
public class HelloController {
    @GetMapping("json")
    public Object jsonResult() {
        Map<String, Object> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        return map;
    }
  
}

DispatcherServlet的自动注册将由org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration配置,其默认启用了Servlet的异步支持。

添加自定义的HttpMessageConverter

添加自定义的HttpMessageConverter比较方便的方法是通过org.springframework.boot.autoconfigure.http.HttpMessageConverters定义,它可以在指定使用默认的HttpMessageConverter的同时添加额外的HttpMessageConverter。下面的代码中就指定了在使用默认的HttpMessageConverter的同时添加了一个自定义的CustomHttpMessageConverter

@Configuration
public class MvcConfiguration {
    @Bean
    public HttpMessageConverters httpMessageConverters() {
        HttpMessageConverter<?> customHttpMessageConverter = new CustomHttpMessageConverter();
        List<HttpMessageConverter<?>> additional = new ArrayList<>();
        additional.add(customHttpMessageConverter);
        HttpMessageConverters converters = new HttpMessageConverters(true, additional);
        return converters;
    }
}

HttpMessageConverters有几个重载的构造方法,使用时可以参考对应的API文档选择合适的进行使用。

Spring Boot中拥有一个HttpMessageConvertersAutoConfiguration类,其会在未定义HttpMessageConverters类型的bean时,自动注册一个HttpMessageConverters类型的bean,即会通过它来使用默认的HttpMessageConverter。此外,其会在自动注册bean容器中定义的HttpMessageConverter,所以使用默认配置时也可以把需要注册的HttpMessageConverter定义为bean容器中的一个bean。HttpMessageConvertersAutoConfiguration中拥有一个StringHttpMessageConverterConfiguration类,会在bean容器中未定义StringHttpMessageConverter类型的bean时自动定义一个,使用的字符集默认是UTF-8,可以通过在application.properties中通过spring.http.encoding.charset指定。

Converter和Formatter注册

下面的代码来自于WebMvcAutoConfigurationAdapter,从代码中可以看出Spring Boot会自动注册bean容器中定义的Converter和Formatter。

@Override
public void addFormatters(FormatterRegistry registry) {
    for (Converter<?, ?> converter : getBeansOfType(Converter.class)) {
        registry.addConverter(converter);
    }
    for (GenericConverter converter : getBeansOfType(GenericConverter.class)) {
        registry.addConverter(converter);
    }
    for (Formatter<?> formatter : getBeansOfType(Formatter.class)) {
        registry.addFormatter(formatter);
    }
}

静态资源的处理

Spring Boot默认会把Classpath下的/META-INF/resources//resources//static//public/映射为静态资源路径。静态资源的相关配置由ResourceProperties类定义,对应的配置属性前缀是spring.resources。如果不想使用默认的静态资源位置,可以通过spring.resources.static-locations属性进行自定义。如果不需要把那些路径映射为静态资源路径,则可以设置spring.resources.addMappings的值为true。静态资源默认会映射为/**,即如果在/resources下拥有一个index.html文件,则可以通过/index.html请求到。可以通过spring.mvc.static-path-pattern指定静态资源映射的路径,下面代码就指定了静态资源的映射路径为/resources/**/**对应的才是真实的静态资源的路径,所以此时如果需要请求/resources路径下的index.html文件,需要通过/resources/index.html才能请求到。

spring.mvc.static-path-pattern=/resources/**

更多配置信息可以参考ResourceProperties的源代码。

浏览器图标文件

Spring Boot默认会在配置的静态资源根路径下或者是Classpath根路径下寻找favicon.ico文件。

Content Negotiation

Spring Boot默认是不支持后缀名匹配的,即请求/report.json时是不会被映射到@GetMapping("/report")的。可以通过如下方式指定支持后缀名匹配。

spring.mvc.contentnegotiation.favor-path-extension=true

关于Content Negotiation的可选配置都定义在WebMvcProperties$Contentnegotiation.class中。可以通过spring.mvc.contentnegotiation.favor-parameter=true指定支持通过查询参数指定请求类型,默认的查询请求类型查询参数是format,需要自定义时可以通过spring.mvc.contentnegotiation.parameter-name属性来指定。

更换内置Web容器

当引入了spring-boot-starter-web后默认使用的Web容器是Tomcat,Spring Boot也提供了一些其它的实现,比如Jetty,当需要使用Jetty实现时需要先把Tomcat实现从spring-boot-starter-web中排除,再添加spring-boot-starter-jetty依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- Exclude the Tomcat dependency -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

不启动Web容器

默认情况下你添加了spring-boot-starter-web后Spring Boot会自动帮你启动Web容器,如果不期望启动Web容器可以配置spring.main.web-application-type=none

内置容器可配置的属性

当使用内置的Web容器时可以针对内置容器进行一些自定义的配置,这些配置将由org.springframework.boot.autoconfigure.web.ServerProperties进行接收。常用的自定义配置包括server.port指定监听端口,server.servlet.contextPath指定contextPath,还可以通过server.servlet.session.*指定session相关的定义信息。下面的配置中定义了监听端口是8081,contextPath是/app,DispatcherServlet匹配的映射路径是/web/*,session的超时时间三30分钟,当指定session的超时时间时,如果不指定时间单位,默认单位是秒。

server.port=8081
server.servlet.context-path=/app
server.servlet.path=/web
server.servlet.session.timeout=30m

更多关于内置Web容器可配置的信息可以参考org.springframework.boot.autoconfigure.web.ServerProperties的API或源码。

注册Servlet/Filter等

根据Servlet3的规范,Classpath下的@WebServlet@WebFilter@WebListener标注的Class会被Web容器自动检测到,并进行注册。但是使用Spring Boot内置的Web容器时,它们是不会自动被检测到并被注册的。有两种方式可以在使用内置的Web容器时能够让它们进行自动注册。

定义为Spring bean

Spring Bean容器中的定义的Filter/Servlet和Listener会被自动注册,Filter会拦截所有请求,而Servlet的请求路径将是bean名称,然后需要以/结尾,比如下面代码中定义的Servlet的请求路径是/hello/

@Component("hello")
public class HelloServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 8345578389259773375L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.write("hello");
        writer.flush();
    }
}

如果上面的Servlet对应的bean名称是servlet/hello,则其对应的请求路径为/servlet/hello/

需要注意的是当bean容器中只定义了一个Servlet时,该Servlet的映射路径不是bean名称,而是/

通过@ServletComponentScan扫描

第二种方式是还是在Class上标注@WebServlet@WebFilter@WebListener注解,然后在某个@Configuration Class上标注@ServletComponentScan,可以通过它的value、basePackages或basePackageClasses三者之一来指定需要扫描的包,如果都不指定,默认会以标注的Class所在的包作为根包进行扫描。下面的代码中先是定义了一个Servlet,使用了@WebServlet标注,并指定了映射的路径为/servlet/test。然后在Application类上使用了@ServletComponentScan标注。Spring Boot将会扫描到TestServlet类,并把它注册为一个Servlet。

@WebServlet("/servlet/test")
public class TestServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = -2499437569852564816L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.write("Hello Servlet.");
        writer.flush();
    }
  
}
@SpringBootApplication
@ServletComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.setAddCommandLineProperties(false);
        app.run(args);
    }
}

如果需要使用@WebServlet的方式定义Servlet,同时又希望可以访问到Spring bean,则可以同时把Servlet定义为Spring的一个bean。下面的代码中定义的Servlet可以通过/servlet/hello访问,同时它是一个Spring bean,被注入了ApplicationContext,在访问它时会输出所有定义的bean的名称。该Servlet同时也可以通过/hello/访问到,因为作为一个HttpServlet类型的bean,它也会被注册为一个Servlet,映射路径为bean名称。

@Component("hello")
@WebServlet("/servlet/hello")
public class HelloServlet extends HttpServlet {
    @Autowired
    private ApplicationContext applicationContext;
  
    /**
     * 
     */
    private static final long serialVersionUID = 8345578389259773375L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.println("bean names: ");
        for (String name : this.applicationContext.getBeanDefinitionNames()) {
            writer.println(name);
        }
        writer.flush();
    }
}

参考文档

https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/html/boot-features-developing-web-applications.html#boot-features-embedded-container-servlets-filters-listeners