我在 Java
开发了一个 RESTful Web 服务和Spring boot
使用Jax-RS
我想用 Swagger
来记录它。到目前为止,我已成功将 swagger-ui.html 页面映射到 http:8080/localhost/<context>/swagger-ui.html
。 不幸的是,我的 RESTful 端点没有出现在任何地方。
我正在使用什么:
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfiguration
{
@Autowired
private TypeResolver typeResolver;
@Bean
public Docket api()
{
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("org.nick.java.webservice.services"))
.paths(PathSelectors.any())
.build()
.enable(true)
.apiInfo(getApiInfo())
.tags(
new Tag("My web service", "Methods for my RESTful service")
);
}
private ApiInfo getApiInfo() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("API Documentation")
.description("API")
.version("1.0")
.contact(new Contact("mycompany", "", "nickath@mycompany.com"))
.build();
return apiInfo;
}
JAX-RS 端点示例
package org.nick.java.webservice.services;
@Path("/contextsapi")
@Consumes("application/json")
@Produces("application/json")
@Api(value = "Contexts API", produces = "application/json")
public interface ContextAPI {
@Path("/contexts/contexts")
@GET
@ApiOperation( value = "get contexts",
response = List.class)
List<Context> getContexts();
swagger-ui.html 页面的屏幕截图
如您所见,没有生成“获取上下文”方法
知道我做错了什么吗?
======= 更新 - 服务实现 ========
package org.nick.java.webservice.services.impl;
@Service
@Api(value = "Contexts Api Impl", produces = "application/json", description = "desc")
@Path("/contextsapi")
public class ContextAPIImpl implements ContextAPI {
@Override
@GET
@ApiOperation( value = "get contexts", response = List.class)
public List<Context> getContexts(){
//code ommitted
}
}
最佳答案
已解决
最后,我按照此处的示例使用 Swagger2Feature
解决了我的问题 https://code.massoudafrashteh.com/spring-boot-cxf-jaxrs-hibernate-maven-swagger-ui/
Maven 依赖项
<cxf.version>3.1.15</cxf.version>
<swagger-ui.version>3.9.2</swagger-ui.version>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-swagger</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
CxfConfig.java
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Bean
public Server rxServer(){
final JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
endpoint.setProvider(new JacksonJsonProvider());
endpoint.setBus(bus);
endpoint.setAddress("/swagger");
endpoint.setServiceBeans(Arrays.<Object>asList(contextAPI());
Swagger2Feature swagger2Feature = new Swagger2Feature();
endpoint.setFeatures(Arrays.asList(swagger2Feature));
return endpoint.create();
}
@Bean
public ContextAPI contextAPI(){
return new ContextAPIImpl();
}
现在 swagger 文档可在 http://localhost:8080/ 上找到。//swagger/api-docs?url=//swagger/swagger.json
要自定义端点的 UI,请查看手册 here
关于java - Swagger 不显示/记录我的 RESTful 端点(JAX-RS、Spring-boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54182934/