当前位置:网站首页>解决允许在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
边栏推荐
- re正则表达式
- C1 notes [task training part 2]
- 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
- I/O多路复用及其相关详解
- Implementation of image recognition code based on VGg convolutional neural network
- C1 notes [task training chapter I]
- Generate verification code
- re正則錶達式
- How to read literature
- Scikit learn sklearn 0.18 official document Chinese version
猜你喜欢

Qtablewidget usage explanation

Halo open source project learning (II): entity classes and data tables

SystemVerilog (VI) - variable

2022年流动式起重机司机国家题库模拟考试平台操作

Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战

云原生虚拟化:基于 Kubevirt 构建边缘计算实例

String function in MySQL

Client example analysis of easymodbustcp

Halo 开源项目学习(二):实体类与数据表

k8s之实现redis一主多从动态扩缩容
随机推荐
Scikit learn sklearn 0.18 official document Chinese version
Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
C language implements memcpy, memset, strcpy, strncpy, StrCmp, strncmp and strlen
xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
.104History
2022制冷与空调设备运行操作判断题及答案
Thirteen documents in software engineering
Uniapp custom search box adaptation applet alignment capsule
JS high frequency interview questions
2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
20222 return to the workplace
Random number generation of C #
String function in MySQL
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
C [file operation] read TXT text by line
Use of list - addition, deletion, modification and query
C#的随机数生成
Add animation to the picture under V-for timing
Land cover / use data product download
Selenium + phantom JS crack sliding verification 2