Jersey + SwaggerUI不会创建swagger.json文件

时间:2016-02-12 16:15:01

标签: java json eclipse maven swagger-ui

我在尝试使用SwaggerUI时遇到了一些问题,因为Swagger没有生成" swagger.json"文件

我正在使用Eclipse和Tomcat 8,我从here下载了定义,并按照

中的说明进行操作
  

扬鞭核-新泽西州2.X-项目化安装-1.5

据我所知,有三种方法可以根据您的JERSEY配置在本地存储库中配置swagger 我跟着这个"使用Jersey 2容器Servlet或Filter(带有web.xml)" 这些是我为项目做的步骤

1.-使用这个Maven原型创建了一个泽西项目

mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.22.1

2.-将此SwaggerUI依赖项添加到我的POM文件

        <dependency>
          <groupId>io.swagger</groupId>
          <artifactId>swagger-jersey2-jaxrs</artifactId>
          <version>1.5.0</version>
        </dependency>

3.-我修改了Web.xml文件,添加了Swagger&#34; param-value&#34; (请注意,我的资源位于com.api包中,&#34; url-pattern&#34;值为&#34; api&#34;)以及

中的说明
  

包扫描 - 混凝土级选择

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>io.swagger.jaxrs.listing,com.api</param-value>          
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

4.-最后我将Swagger Servlet添加到Web.xml文件中,这是完整的xml文件(注意&#34; swagger.api.basepath&#34;值)

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>io.swagger.jaxrs.listing,com.api</param-value>          
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Jersey2Config</servlet-name>
        <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0</param-value>
        </init-param>
        <init-param>            
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8080/SwaggerTest1/api/</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

</web-app>

根据该指南,这将生成一个swagger.json文件,但它在我的项目中不存在。 当我试图给出swagger.api.basepath值的路径时出现了问题? 或者可能是&#34; jersey.config.server.provider.packages&#34;价值?

Additionals。

这是MyResource.java类

package com.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;



/**
 * Root resource (exposed at "myresource" path)
 */
@Path("/myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

Project files

我有几天试图解决这个问题 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

UPDATE!

I noticed that there are no "swagger.json" file. Swagger creates it at compile time.

Based on that I changed the Swagger Servlet configuration like this

@implementation TermsAndPoliciesViewController
-(void)viewDidLoad
{
    [super viewDidLoad];
    if (iPadInt==0) {
        self.view=self.iPhoneTermsView;
    } else {
        if (rotationInt=90) {
            self.view=self.iPadLandscapeTermsView;
    } else     self.view=self.iPadPortraitTermsView;
    }

and in the file.html (inside the dist folder) change the url value as follows

<servlet>
        <servlet-name>Jersey2Config</servlet-name>
        <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0</param-value>
        </init-param>
        <init-param>            
            <param-name>swagger.api.basepath</param-name>
            **<param-value>http://localhost:8080/SwaggerTest1/api</param-value>**
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

with this changes I finally open Swagger with this URL

http://localhost:8080/SwaggerTest1/dist