java - Swagger修饰器:简单的JAX

  显示原文与译文双语对照的内容

我新兴的是 to,我发现文档是碎片的,并且缺少最少的文档。 为了使用Swagger注释,我尝试使用Swagger注释来静态地生成几种语言和文档的客户端库。 但是,我似乎找不到一个简单的例子。

以下是我非常简单的REST服务:


package com.mypack.rest;

import com.mypack.entity.Person;
import com.mypack.service.PeopleService;
import com.wordnik.swagger.annotations.*;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Collection;

@Path("/people")
@Api(value ="/people", description ="Manage people")
public class PeopleRestService {
 private final PeopleService peopleService;

 public PeopleRestService(PeopleService peopleService) {
 this.peopleService = peopleService;
 }

 @Produces({MediaType.APPLICATION_JSON})
 @GET
 @ApiOperation(value ="List all people", notes ="List all people using paging", response = Person.class, responseContainer ="List")
 public Collection<Person> getPeople(@ApiParam(value ="Page to fetch", required = true) @QueryParam("page") @DefaultValue("1") final int page) {
 return peopleService.getPeople(page, 5);
 }

 @Produces({MediaType.APPLICATION_JSON})
 @Path("/{email}")
 @GET
 @ApiOperation(value ="Find person by e-mail", notes ="Find person by e-mail", response = Person.class)
 @ApiResponses({
 @ApiResponse(code = 404, message ="Person with such e-mail doesn't exists")
 })
 public Person getPeople(@ApiParam(value ="E-Mail address to lookup for", required = true) @PathParam("email") final String email) {
 return peopleService.getByEmail(email);
 }

//and more methods.. .
}

然后我有一个 Scala 类来生成客户端库( 按照github上的示例):


import com.wordnik.swagger.codegen.BasicJavaGenerator

object JavaPeopleServiceCodegen extends BasicJavaGenerator {
 def main(args: Array[String]) = generateClient(args)

 override def templateDir ="Java"
 override def destinationDir ="src/main/java"
 override def invokerPackage = Some("com.mypack.client")
 override def modelPackage = Some("com.mypack.entity")
 override def apiPackage = Some("com.mypack.api")

 additionalParams ++= Map(
"artifactId" ->"people-rest",
"artifactVersion" ->"1.0.0",
"groupId" ->"com.mypack")

 override def supportingFiles =
 List(
 ("apiInvoker.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator,"ApiInvoker.java"),
 ("JsonUtil.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator,"JsonUtil.java"),
 ("apiException.mustache", destinationDir + java.io.File.separator + invokerPackage.get.replace(".", java.io.File.separator) + java.io.File.separator,"ApiException.java"),
 ("pom.mustache","","pom.xml"))
}

现在我尝试运行这里文件时,总是出现以下错误:


Exception in thread"main" java.lang.NoSuchMethodError: com.wordnik.swagger.model.SwaggerSerializers$.formats(Ljava/lang/String;)Lorg/json4s/Formats;
 at com.wordnik.swagger.codegen.Codegen.<init>(Codegen.scala:45)
 at com.wordnik.swagger.codegen.BasicGenerator.<init>(BasicGenerator.scala:44)
 at com.wordnik.swagger.codegen.BasicJavaGenerator.<init>(BasicJavaGenerator.scala:25)
 at JavaPeopleServiceCodegen$.<init>(JavaPeopleServiceCodegen.scala:3)

finally,在我的nautilus文件中,我有 javax.ws.rs的依赖关系,swaggerswagger-codegen 。 我不明白,如果缺少一些运行时依赖项( 它看起来不像),我的Scala 库是错误的( 我安装了 Scala 2.10.3并且swagger依赖项编译为 2.10,这应该是二进制兼容的),或者我没有理解。

任何帮助?

时间: 作者:

问题是类名冲突。 codegen项目中有一个SwaggerSerializers类,一个是swagger核心。 所以编译器使用了它的中的一个,另一个被尝试在运行时使用,因这里缺少一个方法。

我刚刚发送了一个请求请求,其中一个可能的来解决这个问题。

...