昂首阔步不起作用

时间:2018-08-08 17:03:11

标签: java rest swagger

我需要使用swagger为rest-service添加文档。 REST服务是由Maven在jar归档文件中构建的。 Swagger通过指令https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-RESTEasy-2.X-Project-Setup-1.5#configure-and-initialize-swagger添加为Application类。

Maven依赖项:

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

Swagger应用程序类:

package amnip.service.swagger;

import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;

import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

public class SwaggerApp extends Application {
    public SwaggerApp() {
        BeanConfig beanConfig = new BeanConfig();

        beanConfig.setVersion("1.0.0");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("localhost:22667");
        beanConfig.setBasePath("/api");
        beanConfig.setResourcePackage("io.swagger.resources");
        beanConfig.setScan(true);
    }

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();

        resources.add(ApiListingResource.class);
        resources.add(SwaggerSerializers.class);

        return resources;
    }
}

带有rest api的类:

package amnip.service;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Api(value = "Admin", description = "Admin API")//TODO
@path("api/v1/admin")
public class Admin {
    @ApiOperation(value = "Hello world")//TODO
    @get
    @path("hello")
    @Produces(MediaType.TEXT_PLAIN)
        public String helloWorld() {
        return "Hello, world!";
    }
}

当我尝试访问http://localhost:22667/api/swagger.json的Swagger定义时,它不起作用。请告诉我,我在做什么错了?

1 个答案:

答案 0 :(得分:0)

带有Swagger实现的Spring

    //Add this dependency 
     <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

配置

@Bean
@Autowired
public Docket api() {
    List<Parameter> newArrayList = new ArrayList<>(Arrays.asList((Parameter) new ParameterBuilder()
            .name(CommonConstants.X_AUTH_TOKEN).description(CommonConstants.REQUEST_VALIDATE)
            .modelRef(new ModelRef(CommonConstants.STRING_TXT)).parameterType(CommonConstants.HEADER_TXT)
            .required(false).build()));

    return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any()).build().globalOperationParameters(newArrayList)
            .enable(Boolean.valueOf(propertyReader.getProperty(CommonConstants.SWAGGER_STATUS)));
}

或者您可以按照应用程序的整个方法https://github.com/faizakram/Application

相关问题