1、一个http请求,先走filter,到达servlet后才进行拦截器。(所以CorsFilter比CorsRegistry先执行)
2、jackson将对象转json:字段全部返回,将null值转为""
public static ObjectMapper nullToEmptyStr(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
// Include.Include.ALWAYS 默认
// Include.NON_DEFAULT 属性为默认值不序列化
// Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
// Include.NON_NULL 属性为NULL 不序列化,就是为null的字段不参加序列化
//objectMapper.setSerializationInclusion(Include.NON_EMPTY);
// 字段保留,将null值转为""
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}
3、springboot统一设置request的header头
public static Docket getHeaderDocket() {
ParameterBuilder ticketPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
ticketPar.name("loginName").description("用户账号")
.modelRef(new ModelRef("string")).parameterType("header")
.required(true).build(); //header中的loginNamey参数非必填,不能传空
pars.add(ticketPar.build()); //根据每个方法名也知道当前方法在设置什么参数
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(false)
.globalOperationParameters(pars);
}
static ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Service API")
.description("Swagger API 文档")
.termsOfServiceUrl("https://www.xxx.com")
.version("v1.0")
.contact(new Contact("sinopec", "https://www.xxx.com", "xxx@qq.com"))
.build();
}
4、开启注解事务管理
@EnableTransactionManagement // 开启注解事务管理,等同于xml配置文件中的 <tx:annotation-driven />