spring boot的常用配置

spring boot 统一管理jar,并自动配置,大大方便了我们的开发。当pom文件里有某个jar时,spring会根据classpath: XX.class

是否存在,自动导入相关的配置,如果项目中自己对配置类进行了配置,那么spring boot会采用我们自己的配置。

1.前台传时间格式进行转换

 时间格式转换类和配置

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.convert.converter.Converter;

/**
 * 当参数类型为date时进行处理,可以根据项目需要修改
 * @author Administrator
 *
 */
public class StringToDateConverter implements Converter<String, Date> {

	private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	private static final String SHORT_DATE_FORMAT = "yyyy-MM-dd";
	
	@Override
	public Date convert(String value) {
		if (StringUtils.isBlank(value)) {
			return null;
		}

		value = value.trim();
		
		try {
			if(value.contains("-")){
				// 表示采用的日期格式是以"-"表示的。eg: 2010-09-09
				SimpleDateFormat formatter;
				if (value.contains(":")) {
					// 表示传入的时间是有时分秒的
					formatter = new SimpleDateFormat(DATE_FORMAT);
				}else{
					// 表示传入的时间是没有时分秒的
					formatter = new SimpleDateFormat(SHORT_DATE_FORMAT);
				}
				Date dtDate = formatter.parse(value);
				return dtDate;
			}else if (value.matches("^\\d+$")) {
				// 表示传入的是一串数字
				Long lDate = new Long(value);
				return new Date(lDate);
			}
		} catch (Exception e) {
			throw new RuntimeException(String.format("parser %s to Date fail", value));
		}
		throw new RuntimeException(String.format("parser %s to Date fail", value));
	}

}
import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;



/**
 * 处理时间的
 * @author Administrator
 *
 */
@Configuration
public class ConverterConfigBean {

	@Autowired
    private RequestMappingHandlerAdapter handlerAdapter;
	
	@PostConstruct
    public void initEditableAvlidation() {

        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)handlerAdapter.getWebBindingInitializer();
        if(initializer.getConversionService()!=null) {
            GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService();           
            
            // 注册日期类型转换器
            genericConversionService.addConverter(new StringToDateConverter());
        }
    }
}

2.controller返回的时间格式默认是时间戳,需要我们自己进行转换

在appliaction.yml文件中加入

#设置时间转换
spring:
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss

3.静态资源的配置

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置spring mvc的一些 配置 ,这里只配置了静态资源的访问路径
 * @author Administrator
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer   {
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
		//根据需要配置静态文件
        registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");
        registry.addResourceHandler("/src/**").addResourceLocations("classpath:/src/");
        registry.addResourceHandler("/index.html").addResourceLocations("classpath:/index.html");
	}
	
	
	
	
}


猜你喜欢

转载自blog.csdn.net/hyhanyu/article/details/80814320