2.SpringBoot-环境配置
来源:互联网 发布:graph cut算法原理 编辑:程序博客网 时间:2024/06/06 01:55
一.MVC相关配置
1.WebMvcConfigurerAdapter(相当于springMVC配置类)
2.编写GeneralWebMvcConfigurer类继承父类(继承方法见配置解析)
可实现配置功能:
配置自己的 requestMapping path 匹配规则
方法名:(configurePathMatch(PathMatchConfigurer configurer))
实例:// localhost:8080/test 与 localhost:8080/test/ 等价configurer.setUseTrailingSlashMatch(true);
// 匹配后缀名: localhost:8080/test 与 localhost:8080/test.jsp 等价configurer.setUseSuffixPatternMatch(true);
设置请求的格式
方法名configureContentNegotiation(ContentNegotiationConfigurer configurer
实例:configurer.ignoreAcceptHeader(true).defaultContentType(MediaType.TEXT_HTML);
异步请求配置
configureAsyncSupport(AsyncSupportConfigurer configurer)
添加拦截器
方法名:(addInterceptors(InterceptorRegistry registry)
实例:registry.addInterceptor(new HandlerInterceptorAdapter(){})
拦截器适配器4个方法:
public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object
public void postHandle
public void afterCompletion
public void afterConcurrentHandlingStarted
dispatch可以访问静态资源
方法名:addResourceHandlers(ResourceHandlerRegistry registry)
实例:registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
例子:url 为 /webjars/** 的请求映射到 classpath:/META-INF/resources/webjars/资源
跨域问题解决
方法名: addCorsMappings(CorsRegistry registry)
定义:CORS是一个W3C标准,全称是"跨域资源共享"。它允许浏览器向跨源服务器,克服了AJAX只能同源使用的限制。
实例:registry.addMapping("/**");
参考:http://blog.csdn.net/isea533/article/details/50449907
不经控制器跳转页面
方法名:addViewControllers(ViewControllerRegistry registry)
实例:registry.addViewController("/login").setViewName("login");
默认视图视图解析:
方法名:configureViewResolvers(ViewResolverRegistry registry)
实例:registry.jsp("/WEB-INF/jsp/", ".jsp")使用默认视图名称前缀“/ WEB-INF /jsp/”注册JSP视图解析器,并使用默认后缀“.jsp”。
参数解析
方法名:addArgumentResolvers(List argumentResolvers))
实例:
class PersonArgumentResolver implementsHandlerMethodArgumentResolver{
@Override
public boolean supportsParameter(MethodParameter parameter){return parameter.getParameterType().equals(Person.class);
}
@Override
publicObjectresolveArgument(....)throwsException{
String firstName = webRequest.getParameter("first_name");
String lastName = webRequest.getParameter("last_name");
return newPerson(firstName, lastName);
}
argumentResolvers.add(newPersonArgumentResolver());
参考:https://my.oschina.net/sugarZone/blog/704575
自定义控制器方法返回值类型
方法名:addReturnValueHandlers(List returnValueHandlers)
参考:https://yq.aliyun.com/articles/38954
重写类型转换(覆盖默认配置)
方法名:configureMessageConverters(List> converters)
实例(转换json格式):
FastJsonpHttpMessageConverter4 converter =newFastJsonpHttpMessageConverter4();
FastJsonConfig config =newFastJsonConfig();
config.setSerializerFeatures(newSerializerFeature[ ]{SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero});
converter.setFastJsonConfig(config);
converter.setDefaultCharset(Charset.forName("UTF-8"));
converters.add(converter);
添加类型转换
方法名:extendMessageConverters(List> converters)
实例:
converters.clear();
converters.add(newByteArrayHttpMessageConverter());
异常处理
方法名:configureHandlerExceptionResolvers(List exceptionResolvers)
实例:exceptionResolvers.add(newHandlerExceptionResolver(){.....})
其他未遇见
public Validator getValidator()
public MessageCodesResolver getMessageCodesResolver()
configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
public void addFormatters(FormatterRegistry registry)
二.注册bean(拓展外部插件)
@Configuration 配置类
@Controller 控制器
@Service 服务类
@Component 其他组件
@Repository 即DAO组件
@Bean 用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean,注册到容器
阅读全文
0 0
- 2.SpringBoot-环境配置
- Springboot 不同环境配置
- springboot多环境配置
- SpringBoot配置多套环境
- springboot 生产环境与开发环境配置
- springboot配置环境生开发测试环境
- springboot下配置junit测试环境
- springboot maven 配置多环境部署
- SpringBoot属性配置实战(多环境)
- SpringBoot ——配置文件多环境配置
- SpringBoot使用profile配置不同环境配置生效
- springboot + profile(不同环境读取不同配置)
- 使用IDEA从零配置springboot项目环境
- 【SpringBoot教程三】:配置文件和多环境配置
- 第十四章 springboot + profile(不同环境读取不同配置)
- springboot多环境(dev、test、prod)配置
- SpringBoot学习笔记——多环境配置
- springboot + profile(不同环境读取不同配置)
- 关于java.lang.OutOfMemoryError: Java heap space的解决方案
- 129. Sum Root to Leaf Numbers
- 关于Java Web 使用JavaMail创建邮件和发送邮件
- 递推专题(一)HDU2044——2050
- PAT 乙级 1008
- 2.SpringBoot-环境配置
- 看图理解单链表的反转
- 【原创】Spring-boot快速入门(二)JPA数据源--转载请注明出处
- JQuery之简单下拉框
- Jmeter性能测试 入门
- 运输层—TCP的拥塞控制
- VLOOKUP使用技巧之按指定次数重复数据
- Cadence ETS v8.1 Linux\
- python yield详解