SpringBoot自定义配置

@Bean
@Scope("singleton")
public ObjectMapper objectMapper(){
	return new ObjectMapper();
}
声明这个json处理会覆盖掉spring的,导致json解析失败

下面是使用自定义的fastjson



	自定义springboot配置
	@Configuration
	@ConditionalOnClass({FastJsonHttpMessageConverter.class})
	@ConditionalOnProperty(
			name = {"spring.http.converters.preferred-json-mapper"},
			havingValue = "fastjson",
			matchIfMissing = true
	)
	static class FastJson2HttpMessageConverterConfiguration{
		@Bean
		@ConditionalOnMissingBean({FastJsonHttpMessageConverter.class})
		public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
			FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
			FastJsonConfig fastJsonConfig = new FastJsonConfig();
			fastJsonConfig.setSerializerFeatures(
					SerializerFeature.WriteMapNullValue,
					SerializerFeature.WriteNullNumberAsZero,
					SerializerFeature.WriteNullListAsEmpty,
					SerializerFeature.WriteNullStringAsEmpty,
					SerializerFeature.WriteNullBooleanAsFalse,
					SerializerFeature.WriteDateUseDateFormat,
					SerializerFeature.PrettyFormat,
					SerializerFeature.WriteClassName
			);
			converter.setFastJsonConfig(fastJsonConfig);
			return converter;
		}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于构建独立的、生产级别的Spring应用程序的框架。它提供了许多便捷的功能和特性,其中包括自定义注解配置。 在Spring Boot中,我们可以通过自定义注解来实现一些特定的配置。下面是一个简单的示例来介绍如何自定义注解配置: 1. 首先,创建一个自定义注解类,使用`@interface`关键字来定义注解。例如,我们创建一个名为`@CustomAnnotation`的注解: ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface CustomAnnotation { String value() default ""; } ``` 2. 在需要使用该注解的类上添加注解。例如,我们创建一个名为`CustomClass`的类,并在类上添加`@CustomAnnotation`注解: ```java @CustomAnnotation("customValue") public class CustomClass { // 类的具体实现 } ``` 3. 在Spring Boot的配置类中,使用`@ComponentScan`注解来扫描带有自定义注解的类,并进行相应的配置。例如: ```java @SpringBootApplication @ComponentScan(basePackages = "com.example") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 在需要使用自定义注解的地方,可以通过反射获取注解的值,并进行相应的处理。例如,在某个Service类中使用自定义注解: ```java @Service public class CustomService { @Autowired private ApplicationContext applicationContext; public void processCustomAnnotation() { Map<String, Object> customBeans = applicationContext.getBeansWithAnnotation(CustomAnnotation.class); for (Object bean : customBeans.values()) { CustomAnnotation customAnnotation = bean.getClass().getAnnotation(CustomAnnotation.class); String value = customAnnotation.value(); // 处理自定义注解的逻辑 } } } ``` 这样,我们就可以通过自定义注解来实现一些特定的配置。在上述示例中,我们通过自定义注解`@CustomAnnotation`来标记需要进行特定处理的类,并在`CustomService`中通过反射获取带有该注解的类,并进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值