集成Swagger2,接口文档不显示

本文介绍了如何在SpringMVC项目中配置Swagger显示接口,同时处理跨域请求的问题,包括使用`@EnableWebMvc`注解启用WebMVC,配置静态资源处理和CORS策略,以及调整Jackson2HttpMessageConverter以避免日期时间戳输出。
摘要由CSDN通过智能技术生成

问题描述

swagger的首页可以显示,就是不显示接口
在这里插入图片描述

解决方法

配置类上添加:@EnableWebMvc

package com.chengshan.camera.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * MvcConfig
 *
 * @author fb
 * @Description 解决跨域、swagger拦截等配置
 * @date 2020-01-10
 */
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    /**
     * Configure how long in seconds the response from a pre-flight request can be cached by clients.
     */
    @Value("${common.mvc.maxAge:3600}")
    private Integer maxAge;

    /**
     * Add handlers to serve static resources such as images, js, and, css
     * files from specific locations under web application root, the classpath,
     * and others.
     *
     * @param registry Stores registrations of resource handlers for serving static resources such as images, css files and others
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/*", "*/*", "/webjars/**", "/static/*", "templates/*")
                .addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/", "classpath:/static/", "classpath:/templates/");
    }

    /**
     * Configure cross origin requests processing.
     * 开启跨域
     *
     * @param registry Assists with the registration of global, URL pattern based
     * @since 4.2
     * <p>
     * SpringBoot升级后,跨域配置报错,将.allowedOrigins替换成.allowedOriginPatterns即可。
     * When allowCredentials is true, allowedOrigins cannot contain thespecial value "*"since that cannot be set on the “Access-Control-Allow-Origin” response header. To allow credentials to a set of origins, list them explicitly or consider using “allowedOriginPatterns” instead.
     * 大概意思是:allowCredentials为true时,allowedOrigins不能包含特殊值“*”,因为不能在“Access Control Allow Origin”响应头上设置该值。要允许凭据指向一组源,请显式列出它们,或者考虑改用“allowedOriginPatterns”。
     * 所以解决办法:将.allowedOrigins替换成.allowedOriginPatterns即可。
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路由
        registry.addMapping("/**")  // 所有的当前站点的请求地址,都支持跨域访问。
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*") // 所有的外部域都可跨域访问。 如果是localhost则很难配置,因为在跨域请求的时候,外部域的解析可能是localhost、127.0.0.1、主机名
                // 是否允许证书(cookies)
                .allowCredentials(true) // 是否支持跨域用户凭证
                // 设置允许的方法,当前站点支持的跨域请求类型是什么
                .allowedMethods(HttpMethod.GET.name(), HttpMethod.POST.name(), HttpMethod.DELETE.name(), HttpMethod.PUT.name(), HttpMethod.PATCH.name())
                // 跨域允许时间
                .maxAge(maxAge);
    }

    /**
     * Extend or modify the list of converters after it has been, either
     * {@link #configureMessageConverters(List) configured} or initialized with
     * a default list.
     * <p>Note that the order of converter registration is important. Especially
     * in cases where clients accept {@link MediaType#ALL}
     * the converters configured earlier will be preferred.
     *
     * @param converters the list of configured converters to be extended
     * @since 4.1.3
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        WebMvcConfigurer.super.extendMessageConverters(converters);
        converters.forEach(httpMessageConverter -> {
            if (httpMessageConverter instanceof AbstractJackson2HttpMessageConverter) {
                ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter) httpMessageConverter).getObjectMapper();
                objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            }
        });
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

strggle_bin

一毛不嫌少,十元不嫌多

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值