java spring springfox swagger2 原生字段修改問題

如果項目要求更改原生字段的展示形式。如date boolean等類型。雖然可以口頭約定,但是這個不是最好的辦法。個人覺得最好的辦法就是不改。如果要改的話,只能在swagger2的序列化程序上入手。沒辦法,這方面springfox swagger2在現(xiàn)在的版本是寫死的。我用的版本是2.9.2。

首先要寫一個springfox swagger的序列化器的覆蓋


import java.util.Collections;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

import springfox.documentation.spring.web.json.JacksonModuleRegistrar;

import springfox.documentation.spring.web.json.Json;

import springfox.documentation.spring.web.json.JsonSerializer;

/**

* 覆蓋swagger的實現(xiàn)以使用自己定義的序列化規(guī)則

* @author yanggan

* @date 2019年1月25日 下午6:33:42

*/

public class JsonSerializerOverride extends JsonSerializer {

? ? public JsonSerializerOverride(List<JacksonModuleRegistrar> modules, ObjectMapper objectMapper) {

? ? ? ? super(Collections.emptyList());

? ? ? ? this.objectMapper = objectMapper;

? ? ? ? for (JacksonModuleRegistrar each : modules) {

? ? ? ? ? ? each.maybeRegisterModule(objectMapper);

? ? ? ? }

? ? }

? ? private ObjectMapper objectMapper;

? ? @Override

? ? public Json toJson(Object toSerialize) {

? ? ? ? try {

? ? ? ? ? ? return new Json(objectMapper.writeValueAsString(toSerialize));

? ? ? ? } catch (JsonProcessingException e) {

? ? ? ? ? ? throw new RuntimeException("Could not write JSON", e);

? ? ? ? }

? ? }

}

然后編寫自定義序列化

import java.io.IOException;

import java.util.Arrays;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.SerializerProvider;

import io.swagger.models.properties.BooleanProperty;

import io.swagger.models.properties.IntegerProperty;

public class BooleanPropertySerializer extends JsonSerializer<BooleanProperty> {

? ? private ObjectMapper objectMapper = new ObjectMapper();

? ? public BooleanPropertySerializer(ObjectMapper objectMapper) {

? ? ? ? super();

? ? ? ? this.objectMapper = objectMapper;

? ? }

? ? @Override

? ? public void serialize(BooleanProperty value, JsonGenerator gen, SerializerProvider serializers) throws IOException {

? ? ? ? IntegerProperty newBooleanProperty = new IntegerProperty();

? ? ? ? BeanUtils.copyProperties(value, newBooleanProperty);

? ? ? ? newBooleanProperty.setType("integer");

? ? ? ? newBooleanProperty.setFormat("int32");

? ? ? ? newBooleanProperty.setExample(1);

? ? ? ? newBooleanProperty.setEnum(Arrays.asList(0, 1));

? ? ? ? JsonNode valueToTree = objectMapper.valueToTree(newBooleanProperty);

? ? ? ? gen.writeTree(valueToTree);

? ? }

? ? @Override

? ? public Class<BooleanProperty> handledType() {

? ? ? ? return BooleanProperty.class;

? ? }

}

最后在配置里覆蓋JsonSerializer

@Bean

? ? @Primary

? ? public JsonSerializer jsonSerializer(List<JacksonModuleRegistrar> modules) {

? ? ? ? ObjectMapper objectMapper = new ObjectMapper();

? ? ? ? objectMapper.setSerializationInclusion(Include.NON_NULL);

? ? ? ? Jackson2ObjectMapperBuilder build = Jackson2ObjectMapperBuilder.json()

? ? ? ? ? ? ? ? .serializers( new BooleanPropertySerializer(objectMapper));

? ? ? ? return new JsonSerializerOverride(modules, build.build());

? ? }

這樣就行了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容