【问题标题】:Swagger with Spring Rest : api-docs does not generate the jsonSwagger with Spring Rest:api-docs 不生成 json
【发布时间】:2016-03-24 08:52:17
【问题描述】:

我正在尝试将 swagger2 与我的 Spring MVC 集成。但是当我尝试访问http://myhost/myapp/api-docs 时,我无法获取 json。你能帮我弄清楚我的代码出了什么问题吗?

我的 Pom.xml(与招摇相关)

<dependency> 
      <groupId>io.springfox</groupId> 
      <artifactId>springfox-swagger2</artifactId> 
      <version>2.2.2</version> 
</dependency> 
<dependency> 
      <groupId>io.springfox</groupId> 
      <artifactId>springfox-swagger-ui</artifactId> 
      <version>2.2.2</version> 
</dependency> 

我的 web.xml

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

我的 mvc-dispatcher.xml

    <context:component-scan base-package="com.smith.spring.ws" />
    <context:component-scan base-package="com.smith.spring.swagger" />
    <mvc:annotation-driven/>
    <mvc:resources location="classpath:/META-INF/resources/swagger-ui.html" mapping="swagger-ui.html" /> 
    <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**" /> 

SwaggerConfig.java

public class SwaggerConfig{  
    @Autowired  
    @Bean  
    public Docket api(){  
        return new Docket(DocumentationType.SWAGGER_2)  
            .select()  
            .apis(RequestHandlerSelectors.any())  
            .paths(PathSelectors.any())  
            .build()  
            .groupName("person")
            .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;
    }      
}

我的控制器

@RestController
@RequestMapping("/person")
@Api(value="Person Rest Service")
public class PersonController {
 @RequestMapping(value="/getPerson",method=RequestMethod.GET)
 @ApiOperation(value = "Fetch a person")
 public @ResponseBody Person getPersons() {
  Person person = new Person();
  person.setFirstname("fname");
  person.setLastname("lname");
  person.setAge(37);
  person.setDepartment("dept");
  return person;
 }
}  

我不确定我是否在这里遗漏了什么。但是当我尝试访问http://localhost:8080/MyApp/api-docs 时,我看不到 json。

如果有人能说明这一点,我将不胜感激。

谢谢,史密斯

【问题讨论】:

    标签: java rest spring-mvc swagger springfox


    【解决方案1】:

    正确的网址是http://localhost:8080/MyApp/v2/api-docs

    你也可以访问swagger ui http://localhost:8080/MyApp/swagger-ui.html

    在 SwaggerConfig 类的顶部使用 @EnableSwagger2 注释。

    在您的mvc-dispatcher.xml 文件中添加&lt;bean name="swaggerConfig" class="com.smith.spring.swagger.SwaggerConfig"/&gt;

    也不确定是否有必要,但将&lt;mvc:resources location="classpath:/META-INF/resources/swagger-ui.html" mapping="swagger-ui.html" /&gt;更改为&lt;mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html" /&gt;

    【讨论】:

    • 当我尝试使用 v2/api-docs 访问时,我仍然没有得到 json。与直接访问 swagger-ui.html 相同
    • 感谢它的工作。我可以通过链接localhost:8080/MyApp/swagger-ui.html 访问 swagger ui 并获取所有详细信息。但是localhost:8080/MyApp/v2/api-docs 仍然没有给出任何输出。
    • 另外一定要更新springfox依赖到2.4.0(不是2.2.2)
    猜你喜欢
    • 2020-06-11
    • 2017-08-06
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多