@ApiModelProperty的name屬性作用是修改參數(shù)的屬性名,但實(shí)際使用的時(shí)候,發(fā)現(xiàn)是無(wú)效的,此時(shí)要怎么辦?
解決方案:
- 如果你springboot項(xiàng)目json轉(zhuǎn)換使用的是jackson, 那么只需要使用
@JsonProperty修改成你需要的屬性名 - 如果你springboot項(xiàng)目json轉(zhuǎn)換使用的是fastjson,那么首先需要使用
@JSONField修改成你需要的屬性名, 然后對(duì)fastjson進(jìn)行配置
@Configuration
@EnableSwagger2
public class Swagger2 {
@Autowired
private ApplicationContext applicationContext;
@PostConstruct
public void setObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
objectMapper.registerModule(module);
objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Override
public boolean isAnnotationBundle(Annotation ann) {
if (ann.annotationType() == JSONField.class) {
return true;
}
return super.isAnnotationBundle(ann);
}
@Override
public PropertyName findNameForSerialization(Annotated a) {
PropertyName nameForSerialization = super.findNameForSerialization(a);
if (nameForSerialization == null || nameForSerialization == PropertyName.USE_DEFAULT) {
JSONField jsonField = _findAnnotation(a, JSONField.class);
if (jsonField != null) {
return PropertyName.construct(jsonField.name());
}
}
return nameForSerialization;
}
@Override
public PropertyName findNameForDeserialization(Annotated a) {
PropertyName nameForDeserialization = super.findNameForDeserialization(a);
if (nameForDeserialization == null || nameForDeserialization == PropertyName.USE_DEFAULT) {
JSONField jsonField = _findAnnotation(a, JSONField.class);
if (jsonField != null) {
return PropertyName.construct(jsonField.name());
}
}
return nameForDeserialization;
}
});
ObjectMapperConfigured objectMapperConfigured = new ObjectMapperConfigured(applicationContext, objectMapper);
applicationContext.publishEvent(objectMapperConfigured);
}
}
參考
Why does @ApiModelProperty “name” attribute has no effect?