0
残りのAPIから生成されたJavaクライアントに問題があり、swaggerで文書化されています。 swagger.json
をswagger editorにupladingしてクライアントを生成してから、Generate Client -> Java
を選択しました。私は、APIからオブジェクトの一覧を取得しようとすると、私は例外以下の取得:SWAGGERのクライアントでRESTサービスからリスト<T>を返す際に例外が発生しました
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at io.swagger.client.JSON.deserialize(JSON.java:103)
at io.swagger.client.ApiClient.deserialize(ApiClient.java:859)
at io.swagger.client.ApiClient.handleResponse(ApiClient.java:1062)
at io.swagger.client.ApiClient.execute(ApiClient.java:989)
at io.swagger.client.api.TestListApi.generateUsersWithHttpInfo(TestListApi.java:146)
at io.swagger.client.api.TestListApi.generateUsers(TestListApi.java:132)
at com.mycompany.testcleint.DailyLimitApiExample.main(DailyLimitApiExample.java:23)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
... 10 more
しかし、私はsagggerエディタにswagger.jsonをインポートするとき、私は成功した結果を取得します。
APIドキュメントの注釈に誤りがありますか?ここで
は私のREST APIのソースコードです:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
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.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
@Path("testList")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "testList", description = "Testing rest list response")
public class TestRestEndpoint {
@GET
@Path("{numberOfUsers}")
@ApiOperation(value = "Retrun x users where x input parameter", notes = "Retrun x users where x input parameter",
response = TestObj.class, responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful retrieval of users", response = TestObj.class, responseContainer = "List"),
@ApiResponse(code = 400, message = "Wrong number format")}
)
public Response generateUsers(@Context UriInfo uri, @PathParam("numberOfUsers") String numberOfUsers) {
Response response;
try {
List<TestObj> resultList = new ArrayList<>();
int identifier = Integer.parseInt(numberOfUsers);
Random random = new Random();
IntStream.range(0, identifier).forEach($ -> {
resultList.add(new TestObj(Long.toHexString(random.nextLong()), Long.toHexString(random.nextLong())));
});
response = Response.ok(resultList)
.location(uri.getRequestUri())
.build();
} catch (NumberFormatException e) {
response = Response.status(Response.Status.BAD_REQUEST).build();
}
return response;
}
}
そして、私のswagger.json:
{
"swagger" : "2.0",
"info" : {
"description" : "This is a spec for our bank transactions REST APIs",
"version" : "v1",
"title" : "Swagger spec for our bank transactions REST APIs"
},
"host" : "localhost:8380",
"basePath" : "/test-service",
"tags" : [ {
"name" : "testList",
"description" : "Testing rest list response"
} ],
"schemes" : [ "http" ],
"paths" : {
"/testList/{numberOfUsers}" : {
"get" : {
"tags" : [ "testList" ],
"summary" : "Retrun x users where x input parameter",
"description" : "Retrun x users where x input parameter",
"operationId" : "generateUsers",
"consumes" : [ "application/json" ],
"produces" : [ "application/json" ],
"parameters" : [ {
"name" : "numberOfUsers",
"in" : "path",
"required" : true,
"type" : "string"
} ],
"responses" : {
"200" : {
"description" : "Successful retrieval of users",
"schema" : {
"$ref" : "#/definitions/TestObj"
}
},
"400" : {
"description" : "Wrong number format"
}
}
}
}
},
"definitions" : {
"TestObj" : {
"type" : "object",
"properties" : {
"firstName" : {
"type" : "string"
},
"lastName" : {
"type" : "string"
}
}
}
}
}