SST*_*SST 8 java spring swagger jersey-2.0
我们使用的是spring 4.x和swagger-jersey2-jaxrs_2.10.Swagger没有列出我的API,它总是只返回版本细节.
的pom.xml
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
<version>1.3.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.23.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
web.xml中
<filter>
<filter-name>SpringApplication</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>xxx.xxx.xxx.filter.JerseyApiSpringFilter</param-value>
</init-param>
<init-param>
<param-name>jersey.config.servlet.filter.forwardOn404</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>jersey.config.servlet.filter.staticContentRegex</param-name>
<param-value>/docs/.*</param-value>
</init-param>
</filter>
Run Code Online (Sandbox Code Playgroud)
资源类
@Path("/listApi")
@Component
@Scope("request")
@Api(value = "/listApi", description = "List API")
@Produces({"application/json"})
public class ListApiResource {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Retrieve all list of apps", response = listDto.class)
public Response getAllApps (@QueryParam("appId") int appId) {
// code
}
}
Run Code Online (Sandbox Code Playgroud)
ResourceConfig类
public class JerseyApiSpringFilter extends ResourceConfig {
static {
// JaxrsApiReader.setFormatString("");
}
public JerseyApiSpringFilter() {
packages("com.xxx.xxxx.xxxxxx.resources");
register(RequestContextFilter.class);
register(ApiListingResource.class);
register(ApiListingResourceJSON.class);
register(JerseyApiDeclarationProvider.class);
register(JerseyResourceListingProvider.class);
register(MultiPartFeature.class);
register(JacksonFeature.class);
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序basebath/api-docs返回
{
"apiVersion": "1.0.0",
"swaggerVersion": "1.2",
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题并使用了如下所示的东西。
我有一个 bean,其中包含在应用程序启动时通过配置生成的可能值的列表。Swagger 应该显示列表,但这不起作用。
@GET
@Path("info/types")
@PermitAll
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Possible bean types", notes = "List all available bean types",
response = BeanTypesList.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 200, message = "List of all available bean types",
response = BeanTypesList.class) })
@Timed
@ExceptionMetered
@CacheControl(maxAge = 6, maxAgeUnit = TimeUnit.HOURS)
public List<BeanType> getBeanTypes() throws JsonProcessingException {
return new ArrayList<BeanType>(BeanType.values());
}
Run Code Online (Sandbox Code Playgroud)
public class SwaggerClassHelper {
@ApiModel(value = "BeanTypesList", description = "Overview of possible bean types")
public class BeanTypesList {
@ApiModelProperty(value = "List of several possible bean types.", required = true)
private List<BeanType> types;
@JsonCreator
public BeanTypesList(
List<BeanType> types) {
this.types = types;
}
public List<BeanType> getTypes() {
return this.types;
}
@JsonIgnore
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
}
Run Code Online (Sandbox Code Playgroud)
@ApiModel(value = "Bean type", description = "Represents the type of a bean.")
public final class BeanType {
@JsonIgnore
private static Set<String> names = new HashSet<String>();
@JsonProperty("name")
private String name;
private BeanType(
List<String> names) {
synchronized (BeanType.names) {
BeanType.names.addAll(names);
}
}
private BeanType(
String name) {
this.name = name;
}
//... other methods
}
Run Code Online (Sandbox Code Playgroud)
我知道如果您使用 swagger,这不是想要的解决方案,但您可以通过响应字段指定输入/输出!
| 归档时间: |
|
| 查看次数: |
511 次 |
| 最近记录: |