yml文件配置如下:
spring:
jackson:
default-property-inclusion: non_null
1
2
3
按照网上的说法,password属性为null时jackson应该不会将其序列化,但是真实情况如下:
阅读文档发现:
Finally, if you opt out of the Spring Boot default MVC configuration by providing your own @EnableWebMvc configuration, you can take control completely and do everything manually by using getMessageConverters from WebMvcConfigurationSupport.
我并没有用@EnableWebMvc这个注解,只是写了一个配置类继承了WebMvcConfigurationSupport,如下:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
//修改静态资源路径
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
String dir = System.getProperty("user.dir");
//System.out.println("项目当前路径:"+dir);
//构建路径
File file=new File(dir+File.separatorChar+"uploadImage");
if(!file.exists()){
file.mkdir();
}
String resourceLocation=file.getAbsolutePath()+File.separatorChar;
WebConfig.UPLOAD_IMAGE_PATH=resourceLocation;
//System.out.println(resourceLocation+">>>>>>");
registry.addResourceHandler("/ui/**")
.addResourceLocations("file:"+resourceLocation);
//由于使用的是继承WebMvcConfigurationSupport,所以所以的默认匹配设置要重写
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/META-INF/resources/")
.addResourceLocations("classpath:/resources/")
.addResourceLocations("classpath:/static/")
.addResourceLocations("classpath:/public/");
super.addResourceHandlers(registry);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
点开WebMvcConfigurationSupport ,发现:
写了一个配置类继承了WebMvcConfigurationSupport原来包涵了@EnableWebMvc,那就只能用注解的方式配置了,如下:
@JsonInclude(JsonInclude.Include.NON_NULL) //注解控制null不序列化
public class User {
private Long id;
private String username;
private String password;
private String name;
private String nickname;
private Integer sex;
private String phone;
private String email;
private Date birthday;
private String place;
private String street;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
配置完后成功了:
诚o
关注
————————————————
版权声明:本文为CSDN博主「诚o」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_22771739/article/details/89949137