解決方法:ProjectDTO.java文件採用一方案
直接在實體類的id屬性上面加上註解 @JsonSerialize(using = ToStringSerializer.class)
這樣一來,在後端依然是 long 類型,當實體類序列化成JSON的時候,在JSON中這個屬性就會變成string類型。
@Id
@GeneratedValue
@JsonSerialize(using = ToStringSerializer.class)
@ApiModelProperty(value = "主鍵ID/非必填")
private Long id;
上面這種辦法有個缺點就是,如果有很多的實體類的id都是 long 類型,那就得給每一個都加上註解,這樣未免有些麻煩。下面這個方法通過添加一個全局配置來使long類型轉爲JSON中的string類型,省去了一個一個添加註解的麻煩。
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)
{
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 全局配置序列化返回 JSON 處理
SimpleModule simpleModule = new SimpleModule();
//JSON Long ==> String
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
return objectMapper;
}
}