SpringBoot--配置fastjson(日期格式转化)和热部署

本文介绍了SpringBoot中配置Fastjson进行日期格式转换以及不序列化属性的方法,包括两种不同的配置方式。同时,文章详细讲解了SpringBoot应用的热部署设置,包括通过spring-boot:run命令以及使用spring-loaded jar实现运行时代码变更的即时生效,确保在开发过程中无需手动重启服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

one.如何配置fastjson

第一种方法:

1.启动类继承extends WebMvcConfigurerAdapter

2.覆盖方法configureMessageConverters

第二种方法注入bean

/**
 * springboot启动类 使用@SpringBootApplication指定这是一个Spring Boot应用程序
 * 能够启动的类,同包下和当前路径的子包
 * 系统名称:
 * 概要说明:
 * @author dq(hasee)
 * @since 2017年2月23日
 */
@SpringBootApplication
public class ApplicationFastJson extends WebMvcConfigurerAdapter{
	
	@Override
	public void configureMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
		/**
		 * 1.需要定义一个convert转换消息的对象
		 * 2.创建配置信息,加入配置信息:比如是否需要格式化返回的json
		 * 3.converter中添加配置信息
		 * 4.convert添加到converters当中
		 */
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = 
				new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(
				SerializerFeature.PrettyFormat
		);
		fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
		converters.add(fastJsonHttpMessageConverter);
	}
	
/*	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastConverter.setFastJsonConfig(fastJsonConfig);
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	}*/
	
	public static void main(String[] args) {
		/*
		 * 在main方法中启动程序
		 */
		SpringApplication.run(ApplicationFastJson.class, args);
		
	}
}


使用fastjson可以使用一些方便的功能

日期格式转化为

@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date date;
不序列化页面不显示属性
@JSONField(serialize=false)
private String notSerilized;

two:热部署

	<!-- SpringLoader plugin -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin </artifactId>
				<dependencies>
					<!--springloaded hot deploy -->
					<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>springloaded</artifactId>
						<version>1.2.4.RELEASE</version>
					</dependency>
				</dependencies>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
						<configuration>
							<classifier>exec</classifier>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
两种启动方式

1.run maven build goals 键入 spring-boot:run

问题 关闭进程但是不会真正关闭,需要任务管理器关闭

2.如果使用的run as – java application的话,那么还需要做一些处理。
把spring-loader-1.2.4.RELEASE.jar下载下来,放到项目的lib目录中,然后把IDEA的run参数里VM参数设置为:
-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify
然后启动就可以了,这样在run as的时候,也能进行热部署

带改变方法热部署

使用devTool:原理,使用了两个classLoader,一个加载不会改变的类,另一个加载会改变的类(restart classloader),所以加载的类少,实现较快的重启时间

修改类 保存 重启

修改配置文件 保存 重启

修改页面 保存 重启 页面会刷新

注意 project  build automatically必须勾选上



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值