当前位置:网站首页>Resolves the interface method that allows annotation requests to be written in postman

Resolves the interface method that allows annotation requests to be written in postman

2022-04-23 18:08:00 Shallow singing ~ happiness

1. We like to be in postman Write field comments in   You can more intuitively see the meaning of the field     as follows :

2. However, if the back-end interface does not do any processing, the following error will be reported :

 

 

3. resolvent     Add the following configuration to the framework to solve the problem :

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  He Zhipeng 
 * 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();
        //  For empty objects, turn json Don't throw errors when 
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        //  Disable an exception thrown when an unknown property is encountered 
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        // Allow comments 
        objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        //  serialize BigDecimal Do not use scientific counting to output 
        objectMapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
        //  Date and time formatting 
        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. Just call it again

版权声明
本文为[Shallow singing ~ happiness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231800323152.html