mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 11:23:58 +00:00
Merge pull request #726 from who/feature/gson-for-android-client
Feature: Use gson for android-client instead of jackson
This commit is contained in:
commit
c7e22b7a3b
@ -51,6 +51,7 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi
|
||||
additionalProperties.put("artifactId", artifactId);
|
||||
additionalProperties.put("artifactVersion", artifactVersion);
|
||||
|
||||
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
|
||||
supportingFiles.add(new SupportingFile("build.mustache", "", "build.gradle"));
|
||||
supportingFiles.add(new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
|
||||
supportingFiles.add(new SupportingFile("apiInvoker.mustache",
|
||||
|
@ -1,23 +1,17 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.core.JsonGenerator.Feature;
|
||||
|
||||
import com.fasterxml.jackson.datatype.joda.*;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class JsonUtil {
|
||||
public static ObjectMapper mapper;
|
||||
public static GsonBuilder gsonBuilder;
|
||||
|
||||
static {
|
||||
mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
mapper.registerModule(new JodaModule());
|
||||
}
|
||||
gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.serializeNulls();
|
||||
}
|
||||
|
||||
public static ObjectMapper getJsonMapper() {
|
||||
return mapper;
|
||||
}
|
||||
}
|
||||
public static Gson getGson() {
|
||||
return gsonBuilder.create();
|
||||
}
|
||||
};
|
@ -1,11 +1,5 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator.Feature;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.apache.http.*;
|
||||
import org.apache.http.client.*;
|
||||
import org.apache.http.client.methods.*;
|
||||
@ -55,6 +49,8 @@ import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
public class ApiInvoker {
|
||||
private static ApiInvoker INSTANCE = new ApiInvoker();
|
||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||
@ -159,9 +155,7 @@ public class ApiInvoker {
|
||||
public static Object deserialize(String json, String containerType, Class cls) throws ApiException {
|
||||
try{
|
||||
if("list".equalsIgnoreCase(containerType) || "array".equalsIgnoreCase(containerType)) {
|
||||
JavaType typeInfo = JsonUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, cls);
|
||||
List response = (List<?>) JsonUtil.getJsonMapper().readValue(json, typeInfo);
|
||||
return response;
|
||||
return JsonUtil.deserializeToList(json, cls);
|
||||
}
|
||||
else if(String.class.equals(cls)) {
|
||||
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
|
||||
@ -170,10 +164,10 @@ public class ApiInvoker {
|
||||
return json;
|
||||
}
|
||||
else {
|
||||
return JsonUtil.getJsonMapper().readValue(json, cls);
|
||||
return JsonUtil.deserializeToObject(json, cls);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
catch (JsonParseException e) {
|
||||
throw new ApiException(500, e.getMessage());
|
||||
}
|
||||
}
|
||||
@ -181,7 +175,7 @@ public class ApiInvoker {
|
||||
public static String serialize(Object obj) throws ApiException {
|
||||
try {
|
||||
if (obj != null)
|
||||
return JsonUtil.getJsonMapper().writeValueAsString(obj);
|
||||
return JsonUtil.serialize(obj);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
@ -24,18 +24,17 @@ android {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ext {
|
||||
swagger_annotations_version = "1.5.3-M1"
|
||||
jackson_version = "2.5.2"
|
||||
gson_version = "2.3.1"
|
||||
httpclient_version = "4.3.3"
|
||||
junit_version = "4.8.1"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "com.wordnik:swagger-annotations:$swagger_annotations_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
|
||||
compile "com.google.code.gson:gson:$gson_version"
|
||||
compile "org.apache.httpcomponents:httpcore:$httpclient_version"
|
||||
compile "org.apache.httpcomponents:httpclient:$httpclient_version"
|
||||
compile "org.apache.httpcomponents:httpmime:$httpclient_version"
|
||||
|
@ -1,20 +1,55 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.core.JsonGenerator.Feature;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import io.swagger.client.model.*;
|
||||
|
||||
public class JsonUtil {
|
||||
public static ObjectMapper mapper;
|
||||
public static GsonBuilder gsonBuilder;
|
||||
|
||||
static {
|
||||
mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.serializeNulls();
|
||||
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||
}
|
||||
|
||||
public static ObjectMapper getJsonMapper() {
|
||||
return mapper;
|
||||
public static Gson getGson() {
|
||||
return gsonBuilder.create();
|
||||
}
|
||||
}
|
||||
|
||||
public static String serialize(Object obj){
|
||||
return getGson().toJson(obj);
|
||||
}
|
||||
|
||||
public static <T> T deserializeToList(String jsonString, Class cls){
|
||||
return getGson().fromJson(jsonString, getListTypeForDeserialization(cls));
|
||||
}
|
||||
|
||||
public static <T> T deserializeToObject(String jsonString, Class cls){
|
||||
return getGson().fromJson(jsonString, getTypeForDeserialization(cls));
|
||||
}
|
||||
|
||||
public static Type getListTypeForDeserialization(Class cls) {
|
||||
String className = cls.getSimpleName();
|
||||
{{#models}}{{#model}}
|
||||
if ("{{classname}}".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<List<{{classname}}>>(){}.getType();
|
||||
}
|
||||
{{/model}}{{/models}}
|
||||
return new TypeToken<List<Object>>(){}.getType();
|
||||
}
|
||||
|
||||
public static Type getTypeForDeserialization(Class cls) {
|
||||
String className = cls.getSimpleName();
|
||||
{{#models}}{{#model}}
|
||||
if ("{{classname}}".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<{{classname}}>(){}.getType();
|
||||
}
|
||||
{{/model}}{{/models}}
|
||||
return new TypeToken<Object>(){}.getType();
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -4,7 +4,7 @@ package {{package}};
|
||||
{{/imports}}
|
||||
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
{{#models}}
|
||||
|
||||
{{#model}}{{#description}}
|
||||
@ -17,7 +17,9 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {
|
||||
public enum {{datatypeWithEnum}} {
|
||||
{{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}}
|
||||
};
|
||||
@SerializedName("{{baseName}}")
|
||||
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{^isEnum}}
|
||||
@SerializedName("{{baseName}}")
|
||||
private {{{datatype}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{/vars}}
|
||||
|
||||
{{#vars}}
|
||||
@ -27,7 +29,6 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {
|
||||
* maximum: {{maximum}}{{/maximum}}
|
||||
**/
|
||||
@ApiModelProperty({{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
|
||||
@JsonProperty("{{baseName}}")
|
||||
public {{{datatypeWithEnum}}} {{getter}}() {
|
||||
return {{name}};
|
||||
}
|
||||
|
@ -0,0 +1,155 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>{{groupId}}</groupId>
|
||||
<artifactId>{{artifactId}}</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>{{artifactId}}</name>
|
||||
<version>{{artifactVersion}}</version>
|
||||
<scm>
|
||||
<connection>scm:git:git@github.com:wordnik/swagger-mustache.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:wordnik/swagger-codegen.git</developerConnection>
|
||||
<url>https://github.com/wordnik/swagger-codegen</url>
|
||||
</scm>
|
||||
<prerequisites>
|
||||
<maven>2.2.0</maven>
|
||||
</prerequisites>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.12</version>
|
||||
<configuration>
|
||||
<systemProperties>
|
||||
<property>
|
||||
<name>loggerPath</name>
|
||||
<value>conf/log4j.properties</value>
|
||||
</property>
|
||||
</systemProperties>
|
||||
<argLine>-Xms512m -Xmx1500m</argLine>
|
||||
<parallel>methods</parallel>
|
||||
<forkMode>pertest</forkMode>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- attach test jar -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
<goal>test-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>add_sources</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>add-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/main/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>add_test_sources</id>
|
||||
<phase>generate-test-sources</phase>
|
||||
<goals>
|
||||
<goal>add-test-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/test/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.wordnik</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
<version>${swagger-annotations-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>${httpclient-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- test dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>sonatype-snapshots</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<properties>
|
||||
<swagger-annotations-version>1.5.1-M1</swagger-annotations-version>
|
||||
<gson-version>2.3.1</gson-version>
|
||||
<junit-version>4.8.1</junit-version>
|
||||
<maven-plugin-version>1.0.0</maven-plugin-version>
|
||||
<junit-version>4.8.1</junit-version>
|
||||
<httpclient-version>4.3.6</httpclient-version>
|
||||
</properties>
|
||||
</project>
|
@ -24,18 +24,17 @@ android {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ext {
|
||||
swagger_annotations_version = "1.5.3-M1"
|
||||
jackson_version = "2.5.2"
|
||||
gson_version = "2.3.1"
|
||||
httpclient_version = "4.3.3"
|
||||
junit_version = "4.8.1"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "com.wordnik:swagger-annotations:$swagger_annotations_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
|
||||
compile "com.google.code.gson:gson:$gson_version"
|
||||
compile "org.apache.httpcomponents:httpcore:$httpclient_version"
|
||||
compile "org.apache.httpcomponents:httpclient:$httpclient_version"
|
||||
compile "org.apache.httpcomponents:httpmime:$httpclient_version"
|
||||
|
155
samples/client/petstore/android-java/pom.xml
Normal file
155
samples/client/petstore/android-java/pom.xml
Normal file
@ -0,0 +1,155 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-android-client</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>swagger-android-client</name>
|
||||
<version>1.0.0</version>
|
||||
<scm>
|
||||
<connection>scm:git:git@github.com:wordnik/swagger-mustache.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:wordnik/swagger-codegen.git</developerConnection>
|
||||
<url>https://github.com/wordnik/swagger-codegen</url>
|
||||
</scm>
|
||||
<prerequisites>
|
||||
<maven>2.2.0</maven>
|
||||
</prerequisites>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.12</version>
|
||||
<configuration>
|
||||
<systemProperties>
|
||||
<property>
|
||||
<name>loggerPath</name>
|
||||
<value>conf/log4j.properties</value>
|
||||
</property>
|
||||
</systemProperties>
|
||||
<argLine>-Xms512m -Xmx1500m</argLine>
|
||||
<parallel>methods</parallel>
|
||||
<forkMode>pertest</forkMode>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- attach test jar -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
<goal>test-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>add_sources</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>add-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/main/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>add_test_sources</id>
|
||||
<phase>generate-test-sources</phase>
|
||||
<goals>
|
||||
<goal>add-test-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/test/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.wordnik</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
<version>${swagger-annotations-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>${httpclient-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- test dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>sonatype-snapshots</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<properties>
|
||||
<swagger-annotations-version>1.5.1-M1</swagger-annotations-version>
|
||||
<gson-version>2.3.1</gson-version>
|
||||
<junit-version>4.8.1</junit-version>
|
||||
<maven-plugin-version>1.0.0</maven-plugin-version>
|
||||
<junit-version>4.8.1</junit-version>
|
||||
<httpclient-version>4.3.6</httpclient-version>
|
||||
</properties>
|
||||
</project>
|
@ -1,11 +1,5 @@
|
||||
package io.swagger.client;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator.Feature;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.apache.http.*;
|
||||
import org.apache.http.client.*;
|
||||
import org.apache.http.client.methods.*;
|
||||
@ -55,6 +49,8 @@ import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
public class ApiInvoker {
|
||||
private static ApiInvoker INSTANCE = new ApiInvoker();
|
||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||
@ -159,9 +155,7 @@ public class ApiInvoker {
|
||||
public static Object deserialize(String json, String containerType, Class cls) throws ApiException {
|
||||
try{
|
||||
if("list".equalsIgnoreCase(containerType) || "array".equalsIgnoreCase(containerType)) {
|
||||
JavaType typeInfo = JsonUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, cls);
|
||||
List response = (List<?>) JsonUtil.getJsonMapper().readValue(json, typeInfo);
|
||||
return response;
|
||||
return JsonUtil.deserializeToList(json, cls);
|
||||
}
|
||||
else if(String.class.equals(cls)) {
|
||||
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
|
||||
@ -170,10 +164,10 @@ public class ApiInvoker {
|
||||
return json;
|
||||
}
|
||||
else {
|
||||
return JsonUtil.getJsonMapper().readValue(json, cls);
|
||||
return JsonUtil.deserializeToObject(json, cls);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
catch (JsonParseException e) {
|
||||
throw new ApiException(500, e.getMessage());
|
||||
}
|
||||
}
|
||||
@ -181,7 +175,7 @@ public class ApiInvoker {
|
||||
public static String serialize(Object obj) throws ApiException {
|
||||
try {
|
||||
if (obj != null)
|
||||
return JsonUtil.getJsonMapper().writeValueAsString(obj);
|
||||
return JsonUtil.serialize(obj);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
@ -1,20 +1,87 @@
|
||||
package io.swagger.client;
|
||||
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.core.JsonGenerator.Feature;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import io.swagger.client.model.*;
|
||||
|
||||
public class JsonUtil {
|
||||
public static ObjectMapper mapper;
|
||||
public static GsonBuilder gsonBuilder;
|
||||
|
||||
static {
|
||||
mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.serializeNulls();
|
||||
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||
}
|
||||
|
||||
public static ObjectMapper getJsonMapper() {
|
||||
return mapper;
|
||||
public static Gson getGson() {
|
||||
return gsonBuilder.create();
|
||||
}
|
||||
}
|
||||
|
||||
public static String serialize(Object obj){
|
||||
return getGson().toJson(obj);
|
||||
}
|
||||
|
||||
public static <T> T deserializeToList(String jsonString, Class cls){
|
||||
return getGson().fromJson(jsonString, getListTypeForDeserialization(cls));
|
||||
}
|
||||
|
||||
public static <T> T deserializeToObject(String jsonString, Class cls){
|
||||
return getGson().fromJson(jsonString, getTypeForDeserialization(cls));
|
||||
}
|
||||
|
||||
public static Type getListTypeForDeserialization(Class cls) {
|
||||
String className = cls.getSimpleName();
|
||||
|
||||
if ("User".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<List<User>>(){}.getType();
|
||||
}
|
||||
|
||||
if ("Category".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<List<Category>>(){}.getType();
|
||||
}
|
||||
|
||||
if ("Pet".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<List<Pet>>(){}.getType();
|
||||
}
|
||||
|
||||
if ("Tag".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<List<Tag>>(){}.getType();
|
||||
}
|
||||
|
||||
if ("Order".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<List<Order>>(){}.getType();
|
||||
}
|
||||
|
||||
return new TypeToken<List<Object>>(){}.getType();
|
||||
}
|
||||
|
||||
public static Type getTypeForDeserialization(Class cls) {
|
||||
String className = cls.getSimpleName();
|
||||
|
||||
if ("User".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<User>(){}.getType();
|
||||
}
|
||||
|
||||
if ("Category".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<Category>(){}.getType();
|
||||
}
|
||||
|
||||
if ("Pet".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<Pet>(){}.getType();
|
||||
}
|
||||
|
||||
if ("Tag".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<Tag>(){}.getType();
|
||||
}
|
||||
|
||||
if ("Order".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<Order>(){}.getType();
|
||||
}
|
||||
|
||||
return new TypeToken<Object>(){}.getType();
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -2,20 +2,21 @@ package io.swagger.client.model;
|
||||
|
||||
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
|
||||
@ApiModel(description = "")
|
||||
public class Category {
|
||||
|
||||
@SerializedName("id")
|
||||
private Long id = null;
|
||||
@SerializedName("name")
|
||||
private String name = null;
|
||||
|
||||
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("id")
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@ -27,7 +28,6 @@ public class Category {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -3,27 +3,32 @@ package io.swagger.client.model;
|
||||
import java.util.Date;
|
||||
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
|
||||
@ApiModel(description = "")
|
||||
public class Order {
|
||||
|
||||
@SerializedName("id")
|
||||
private Long id = null;
|
||||
@SerializedName("petId")
|
||||
private Long petId = null;
|
||||
@SerializedName("quantity")
|
||||
private Integer quantity = null;
|
||||
@SerializedName("shipDate")
|
||||
private Date shipDate = null;
|
||||
public enum StatusEnum {
|
||||
placed, approved, delivered,
|
||||
};
|
||||
@SerializedName("status")
|
||||
private StatusEnum status = null;
|
||||
@SerializedName("complete")
|
||||
private Boolean complete = null;
|
||||
|
||||
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("id")
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@ -35,7 +40,6 @@ public class Order {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("petId")
|
||||
public Long getPetId() {
|
||||
return petId;
|
||||
}
|
||||
@ -47,7 +51,6 @@ public class Order {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("quantity")
|
||||
public Integer getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
@ -59,7 +62,6 @@ public class Order {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("shipDate")
|
||||
public Date getShipDate() {
|
||||
return shipDate;
|
||||
}
|
||||
@ -72,7 +74,6 @@ public class Order {
|
||||
* Order Status
|
||||
**/
|
||||
@ApiModelProperty(value = "Order Status")
|
||||
@JsonProperty("status")
|
||||
public StatusEnum getStatus() {
|
||||
return status;
|
||||
}
|
||||
@ -84,7 +85,6 @@ public class Order {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("complete")
|
||||
public Boolean getComplete() {
|
||||
return complete;
|
||||
}
|
||||
|
@ -5,27 +5,32 @@ import io.swagger.client.model.Tag;
|
||||
import java.util.*;
|
||||
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
|
||||
@ApiModel(description = "")
|
||||
public class Pet {
|
||||
|
||||
@SerializedName("id")
|
||||
private Long id = null;
|
||||
@SerializedName("category")
|
||||
private Category category = null;
|
||||
@SerializedName("name")
|
||||
private String name = null;
|
||||
@SerializedName("photoUrls")
|
||||
private List<String> photoUrls = new ArrayList<String>() ;
|
||||
@SerializedName("tags")
|
||||
private List<Tag> tags = new ArrayList<Tag>() ;
|
||||
public enum StatusEnum {
|
||||
available, pending, sold,
|
||||
};
|
||||
@SerializedName("status")
|
||||
private StatusEnum status = null;
|
||||
|
||||
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("id")
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@ -37,7 +42,6 @@ public class Pet {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("category")
|
||||
public Category getCategory() {
|
||||
return category;
|
||||
}
|
||||
@ -49,7 +53,6 @@ public class Pet {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(required = true, value = "")
|
||||
@JsonProperty("name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@ -61,7 +64,6 @@ public class Pet {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(required = true, value = "")
|
||||
@JsonProperty("photoUrls")
|
||||
public List<String> getPhotoUrls() {
|
||||
return photoUrls;
|
||||
}
|
||||
@ -73,7 +75,6 @@ public class Pet {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("tags")
|
||||
public List<Tag> getTags() {
|
||||
return tags;
|
||||
}
|
||||
@ -86,7 +87,6 @@ public class Pet {
|
||||
* pet status in the store
|
||||
**/
|
||||
@ApiModelProperty(value = "pet status in the store")
|
||||
@JsonProperty("status")
|
||||
public StatusEnum getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
@ -2,20 +2,21 @@ package io.swagger.client.model;
|
||||
|
||||
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
|
||||
@ApiModel(description = "")
|
||||
public class Tag {
|
||||
|
||||
@SerializedName("id")
|
||||
private Long id = null;
|
||||
@SerializedName("name")
|
||||
private String name = null;
|
||||
|
||||
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("id")
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@ -27,7 +28,6 @@ public class Tag {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -2,26 +2,33 @@ package io.swagger.client.model;
|
||||
|
||||
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
|
||||
@ApiModel(description = "")
|
||||
public class User {
|
||||
|
||||
@SerializedName("id")
|
||||
private Long id = null;
|
||||
@SerializedName("username")
|
||||
private String username = null;
|
||||
@SerializedName("firstName")
|
||||
private String firstName = null;
|
||||
@SerializedName("lastName")
|
||||
private String lastName = null;
|
||||
@SerializedName("email")
|
||||
private String email = null;
|
||||
@SerializedName("password")
|
||||
private String password = null;
|
||||
@SerializedName("phone")
|
||||
private String phone = null;
|
||||
@SerializedName("userStatus")
|
||||
private Integer userStatus = null;
|
||||
|
||||
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("id")
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@ -33,7 +40,6 @@ public class User {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("username")
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
@ -45,7 +51,6 @@ public class User {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("firstName")
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
@ -57,7 +62,6 @@ public class User {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("lastName")
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
@ -69,7 +73,6 @@ public class User {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("email")
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
@ -81,7 +84,6 @@ public class User {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("password")
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
@ -93,7 +95,6 @@ public class User {
|
||||
/**
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
@JsonProperty("phone")
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
@ -106,7 +107,6 @@ public class User {
|
||||
* User Status
|
||||
**/
|
||||
@ApiModelProperty(value = "User Status")
|
||||
@JsonProperty("userStatus")
|
||||
public Integer getUserStatus() {
|
||||
return userStatus;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user