• spring mvc4 配置restfulAPI 接口管理工具问题整体


    1.jar

    2.文件配置

    swagger配置

    package com.myPackage.config;
    
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig{
    
    
        @Bean
        public Docket api(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build()
                    .groupName("test")
                    .apiInfo(apiInfo());
        }
    
        private ApiInfo apiInfo() {
            ApiInfo apiInfo = new ApiInfo(
                    "My Project's REST API", 
                    "This is a description of your API.", 
                    "API TOS",
                    "url",
                    "me@wherever.com", 
                    "API License", 
                    "API License URL");
            return apiInfo;
        }
    
    }
    springMVC配置:

    package com.myPackage.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @Import({SwaggerConfig.class}) @ComponentScan("com.myPackage.controller") public class WebSpringConfig extends WebMvcConfigurerAdapter{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); registry .addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry .addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Bean public ViewResolver configureViewResolver() { InternalResourceViewResolver viewResolve = new InternalResourceViewResolver(); viewResolve.setPrefix("/WEB-INF/views/"); viewResolve.setSuffix(".jsp"); return viewResolve; } @Override public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }

      3.分发器配置:

    <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
            </init-param>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>com.myPackage.config.WebSpringConfig</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    

      

    4问题:v2/api-docs 报 404 

    参考:

    https://stackoverflow.com/questions/33780237/springfox-2-2-2-no-api-docs-generated

  • 相关阅读:
    根据服务端生成的WSDL文件创建客户端支持代码的三种方式
    2017第45周一java多线程创建方法
    java 分布式锁
    设计的背后
    Java中的锁概念
    maven变量
    清零成长法
    Java消息队列
    Java9的新特性
    2017第43周日
  • 原文地址:https://www.cnblogs.com/codeinet/p/7978969.html
Copyright © 2020-2023  润新知