SpringBoot的web开发
一、自动配置的ViewResolver
视图的配置mvcProperties对象中:
org.springframework.boot.autoconfigure.web.WebMvcProperties.View
二、自动配置静态资源
2.1 进入规则为/
如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
测试:
访问:127.0.0.1:8088/timg.jpg
2.2 进入规则为.xxx 或者 不指定静态文件路径时*
将静态资源放置到webapp下的static目录中即可通过地址访问:
测试:
三、自定义消息转化器
原来的(springmvc.xml中):
<!-- 注解驱动 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg index="0" value="UTF-8" />
</bean>
<bean class="com.hcx.common.spring.extend.converter.json.CallbackMappingJackson2HttpMessageConverter">
<property name="callbackName" value="callback" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
自定义消息转化器,只需要在@Configuration的类中添加消息转化器的@bean加入到Spring容器,就会被Spring Boot自动加入到容器中。
@Controller
@SpringBootApplication(exclude={RedisAutoConfiguration.class}) //排除redis的配置
@Configuration //声明该类为一个配置类
public class HelloApplication {
//springboot的项目命名中,一般都有一个xxxApplication类,该类作为springboot项目的入口类
@RequestMapping("hello")
@ResponseBody
public String hello(){
return "hello world";
}
@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(HelloApplication.class);
app.setBannerMode(Banner.Mode.OFF);//关闭banner
app.run(args);
}
}
默认配置:
四、自定义SpringMVC的配置
有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。
HelloApplication和MySpringMVCConfig在同一包下,所以在MySpringMVCConfig加入了@Configuration注解会被扫描进去,运行HelloApplication,会走该自定义拦截器。
MySpringMVCConfig:
package com.hcx.springboot.demo;
import java.nio.charset.Charset;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration //申明这是一个配置
public class MySpringMVCConfig extends WebMvcConfigurerAdapter{
// 自定义拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("自定义拦截器............");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception {
}
};
registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
}
// 自定义消息转化器的第二种方法
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
converters.add(converter);
}
}
HelloApplication:
package com.hcx.springboot.demo;
import java.nio.charset.Charset;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication(exclude={RedisAutoConfiguration.class}) //排除redis的配置
@Configuration //声明该类为一个配置类
public class HelloApplication {
//springboot的项目命名中,一般都有一个xxxApplication类,该类作为springboot项目的入口类
@RequestMapping("hello")
@ResponseBody
public String hello(){
return "hello world";
}
@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(HelloApplication.class);
app.setBannerMode(Banner.Mode.OFF);//关闭banner
app.run(args);
}
}
注意:此时,有两个消息转换器;如果把两个消息转换器都去掉,spring中默认还有一个。把其中一个打开,则有一个,内部的是否创建取决于我们自己有没有创建,我们没有创建则内部的就会创建,如果我们有创建,则内部的不会创建。内部的是判断,容器中是否有,没有则创建,有就不创建了。