-
[置顶]短信API接口,短信接口怎么对接
本文讲述了短信API接口,短信接口怎么对接。一、关于短信API接口第三方短信服务商提供短信API接口,短信一般是编辑好内容,调用接口发送即可。我们这里提供一个注册好的账号,其中短信API服务器地址为:...
-
[置顶]热门好用的空号检测API推荐,空号检测API数据接口
本文讲述了热门好用的空号检测API推荐,空号检测API数据接口。空号检测,也称号码检测,空号过滤,号码筛选等,是基于运营商大数据及流量使用情况返回手机号码状态,比如 实号、空号 等。今天就给大家推荐一...
-
[置顶]语音验证码短信原理的深入解析
语音合成技术语音合成技术(Text-to-Speech,TTS)是一种将文本信息转换为语音输出的技术。它通过计算机算法和声音合成器,将文本中的文字逐个转换为语音信号,使计算机能够以自然语言的方式朗读出...
-
-
-
-
-
-
$(document).ready(function(){
console.log('init');
$.ajax({url:"http://localhost:8080/getLongValue"})
.then(res=>{
console.log({
'getLongValue':res
});
$('#resId').text(res.id);
$('#idType').text(typeof res.id);
})
});
运行结果
通过输出结果和查看网络的内容,发现实际上id返回的结果是1234567890102349000,最后几位都变成了00, 这是因为,javascript的Number类型最大长度是17http://位,而后端返回的Long类型有19位,导致js的Number不能解析。
方案
既然不能使用js的Number接收,那么前端如何Long类型的数据呢,答案是js使用string类型接收
方案一 @JsonSerialize 注解
修改Dto的id字段,使用@JsonSerialize注解指定类型为string。
这个方案有一个问题,就是需要程序员明确指定@JsonSerialize, 在实际的使用过程中,程序员会很少注意到Long类型的问题,只有和前端联调的时候发现不对。
@Data
public static class GetLongValueDto{
@JsonSerialize(using= ToStringSerializer.class)
private Long id;
}
方案二 全局处理器
添加Configuration, 处理 HttpMessageConverter
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
/**
* 序列化json时,将所有的long变成string
* 因为js中得数字类型不能包含所有的java long值
*/
@Override
public void configureMessageConverters(List
> converters) { MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule=new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(0,jackson2HttpMessageConverter);
}
}
@Data
public static class GetLongValueDto{
private Long id;
}
发现没有@JsonSerialize注解的信息,前端接收到的数据,也是string类型了。
与swagger集成
上面只是解决了传输时的long类型转string,但是当集成了swagger时,swagger文档描述的类型仍然是number类型的,这样在根据swagger文档生成时,会出现类型不匹配的问题
swagger 文档集成
pom或gradle
implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
io.springfox springfox-boot-starter 3.0.0 查看文档, 发现 GetLongValueDto 描述的id类型是 integer($int64)
swagger long类型描述为string
需要修改swagger的配置, 修改 Docket 的配置
.directModelSubstitute(Long.class, String.class)
.directModelSubstitute(long.class, String.class)
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())//api的配置路径
.paths(PathSelectors.any())//扫描路径选择
.build()
.directModelSubstitute(Long.class, String.class)
.directModelSubstitute(long.class, String.class)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("title") //文档标题
.description("description" alt="解析Spring Mvc Long类型精度丢失问题" title="解析Spring Mvc Long类型精度丢失问题" width="200" height="150">
-
-
-
-