yml文件配置如下:
spring:
jackson:
default-property-inclusion: non_null
按照网上的说法,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);
}
}
点开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;
}
配置完后成功了: