当前位置:网站首页>解决允许在postman中写入注释请求接口方法
解决允许在postman中写入注释请求接口方法
2022-04-23 18:00:00 【浅唱~幸福】
1.我们喜欢在postman中写入字段注释 可以更直观的看出字段的意思 如下:
2.但如果后端接口不做任何处理会报如下错误:
3.解决方法 在框架中加入如下配置即可解决问题:
package net.longjin.comm.handle; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.*; import com.fasterxml.jackson.datatype.jsr310.ser.*; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.io.Serializable; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.List; /** * author 何志鹏 * CREATED_DATE 2020/4/21 9:49 */ @Configuration public class JsonSerializableConfig implements WebMvcConfigurer { @Bean public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); // 对于空的对象转json的时候不抛出错误 objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); // 禁用遇到未知属性抛出异常 objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); //允许注释 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); // 序列化BigDecimal时不使用科学计数法输出 objectMapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true); // 日期和时间格式化 JavaTimeModule localDateTime = new JavaTimeModule(); localDateTime.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); localDateTime.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); localDateTime.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); localDateTime.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); localDateTime.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); localDateTime.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); localDateTime.addSerializer(MonthDay.class, new MonthDaySerializer(DateTimeFormatter.ofPattern("MM-dd"))); localDateTime.addDeserializer(MonthDay.class, new MonthDayDeserializer(DateTimeFormatter.ofPattern("MM-dd"))); localDateTime.addSerializer(Year.class, new YearSerializer(DateTimeFormatter.ofPattern("yyyy"))); localDateTime.addDeserializer(Year.class, new YearDeserializer(DateTimeFormatter.ofPattern("yyyy"))); localDateTime.addSerializer(YearMonth.class, new YearMonthSerializer(DateTimeFormatter.ofPattern("yyyy-MM"))); localDateTime.addDeserializer(YearMonth.class, new YearMonthDeserializer(DateTimeFormatter.ofPattern("yyyy-MM"))); objectMapper.registerModule(localDateTime); jsonConverter.setObjectMapper(objectMapper); return jsonConverter; } /*@Bean public FilterRegistrationBean mdcFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new MdcFilter()); filterRegistrationBean.setOrder(99); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.setName("mdcFilter"); return filterRegistrationBean; }*/ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(customJackson2HttpMessageConverter()); } }
4.再次调用就好了
版权声明
本文为[浅唱~幸福]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_39643007/article/details/111032200
边栏推荐
- [UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
- JS high frequency interview questions
- Halo open source project learning (II): entity classes and data tables
- Re expression régulière
- Auto. JS custom dialog box
- YOLOv4剪枝【附代码】
- Nanotechnology + AI enabled proteomics | Luomi life technology completed nearly ten million US dollars of financing
- Using files to save data (C language)
- SystemVerilog (VI) - variable
- Gets the time range of the current week
猜你喜欢
Summary of floating point double precision, single precision and half precision knowledge
[appium] write scripts by designing Keyword Driven files
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
Operators in C language
Qtablewidget usage explanation
Go语言JSON包使用
ArcGIS license error -15 solution
MySQL 中的字符串函数
C1小笔记【任务训练篇一】
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
随机推荐
Open source key component multi_ Button use, including test engineering
Install pyshp Library
_ FindText error
YOLOv4剪枝【附代码】
Element calculation distance and event object
Tensorflow tensor introduction
油猴网站地址
C language array processing batch data
C1小笔记【任务训练篇二】
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
Romance in C language
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
Land cover / use data product download
.104History
An example of linear regression based on tensorflow
positioner
Submit local warehouse and synchronize code cloud warehouse
re正则表达式
Logic regression principle and code implementation
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag