橘子味阳光 2016-04-06 09:38 采纳率: 16.7%
浏览 2402
已结题

Swagger 集成spring boot

为什么我的API只出现一部分
图片说明

 package cn.paybay.qingjiao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import static springfox.documentation.builders.PathSelectors.regex;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import cn.paybay.qingjiao.base.filter.UserMessageFilter;
import cn.paybay.qingjiao.base.filter.UserMessageFilter2;
import static com.google.common.base.Predicates.or;
@SpringBootApplication
@EnableSwagger2 //Enable swagger 2.0 spec
@ComponentScan("hello")
public class MainController extends WebMvcConfigurerAdapter{

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MainController.class, args);
    }

    //@Override
    //public void addInterceptors(InterceptorRegistry registry) {  
        //registry.addInterceptor(new UserMessageFilter()).addPathPatterns("/**");  
        //registry.addInterceptor(new UserMessageFilter2()).addPathPatterns("/**"); 
       // super.addInterceptors(registry);
    //}

     @Bean
        public Docket newsApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("greetings")
                    .apiInfo(apiInfo())
                    .select()
                    .paths(regex("/greeting.*"))
                    .build();
        }

        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("Spring REST Sample with Swagger")
                    .description("Spring REST Sample with Swagger")
                    .termsOfServiceUrl("http://www-03.ibm.com/software/sla/sladb.nsf/sla/bm?Open")
                    .contact("Niklas Heidloff")
                    .license("Apache License Version 2.0")
                    .licenseUrl("https://github.com/IBM-Bluemix/news-aggregator/blob/master/LICENSE")
                    .version("2.0")
                    .build();
        }
}

 package cn.paybay.qingjiao;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.ResponseHeader;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @ApiOperation(value = "getGreeting", nickname = "getGreeting")
    @RequestMapping(method = RequestMethod.GET, path="/greeting", produces = "application/json")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "User's name", required = false, dataType = "string", paramType = "query", defaultValue="Niklas")
      })
    @ApiResponses(value = { 
            @ApiResponse(code = 200, message = "Success", response = Greeting.class),
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Forbidden"),
            @ApiResponse(code = 404, message = "Not Found"),
            @ApiResponse(code = 500, message = "Failure")}) 
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, name));
    }
}
 package cn.paybay.qingjiao;

import com.fasterxml.jackson.annotation.JsonProperty;

import io.swagger.annotations.ApiModelProperty;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    @JsonProperty(required = true)
    @ApiModelProperty(notes = "The name of the user", required = true)
    public String getContent() {
        return content;
    }
}

  • 写回答

2条回答

  • 橘子味阳光 2016-04-07 05:09
    关注

    谢谢各位啦! 已解决!

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用