Skip to content

Commit 3d12c88

Browse files
committed
Merge pull request #562 from swagger-api/feature/java-client
Feature/java client
2 parents a2dc767 + 5d6d563 commit 3d12c88

File tree

17 files changed

+468
-349
lines changed

17 files changed

+468
-349
lines changed

modules/swagger-codegen/src/main/resources/Java/api.mustache

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import java.util.*;
1111
{{/imports}}
1212

1313
import com.sun.jersey.multipart.FormDataMultiPart;
14+
import com.sun.jersey.multipart.file.FileDataBodyPart;
1415

1516
import javax.ws.rs.core.MediaType;
1617

@@ -77,7 +78,8 @@ public class {{classname}} {
7778
mp.field("{{baseName}}", ApiInvoker.parameterToString({{paramName}}), MediaType.MULTIPART_FORM_DATA_TYPE);
7879
{{/notFile}}{{#isFile}}
7980
hasFields = true;
80-
mp.field("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE);
81+
mp.field("{{baseName}}", file.getName());
82+
mp.bodyPart(new FileDataBodyPart("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE));
8183
{{/isFile}}{{/formParams}}
8284
if(hasFields)
8385
postBody = mp;
@@ -96,12 +98,7 @@ public class {{classname}} {
9698
return {{#returnType}}null{{/returnType}};
9799
}
98100
} catch (ApiException ex) {
99-
if(ex.getCode() == 404) {
100-
return {{#returnType}} null{{/returnType}};
101-
}
102-
else {
103-
throw ex;
104-
}
101+
throw ex;
105102
}
106103
}
107104
{{/operation}}

modules/swagger-codegen/src/main/resources/Java/apiInvoker.mustache

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.sun.jersey.multipart.FormDataMultiPart;
1616
import javax.ws.rs.core.Response.Status.Family;
1717
import javax.ws.rs.core.MediaType;
1818

19+
import java.util.Collection;
1920
import java.util.Map;
2021
import java.util.HashMap;
2122
import java.util.List;
@@ -83,11 +84,19 @@ public class ApiInvoker {
8384
return "";
8485
} else if (param instanceof Date) {
8586
return formatDateTime((Date) param);
87+
} else if (param instanceof Collection) {
88+
StringBuilder b = new StringBuilder();
89+
for(Object o : (Collection)param) {
90+
if(b.length() > 0) {
91+
b.append(",");
92+
}
93+
b.append(String.valueOf(o));
94+
}
95+
return b.toString();
8696
} else {
8797
return String.valueOf(param);
8898
}
8999
}
90-
91100
public void enableDebug() {
92101
isDebug = true;
93102
}
@@ -217,7 +226,7 @@ public class ApiInvoker {
217226
}
218227
else if ("DELETE".equals(method)) {
219228
if(body == null)
220-
response = builder.delete(ClientResponse.class, serialize(body));
229+
response = builder.delete(ClientResponse.class);
221230
else
222231
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
223232
}
@@ -228,12 +237,26 @@ public class ApiInvoker {
228237
return null;
229238
}
230239
else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
231-
return (String) response.getEntity(String.class);
240+
if(response.hasEntity()) {
241+
return (String) response.getEntity(String.class);
242+
}
243+
else {
244+
return "";
245+
}
232246
}
233247
else {
248+
String message = "error";
249+
if(response.hasEntity()) {
250+
try{
251+
message = String.valueOf(response.getEntity(String.class));
252+
}
253+
catch (RuntimeException e) {
254+
// e.printStackTrace();
255+
}
256+
}
234257
throw new ApiException(
235258
response.getClientResponseStatus().getStatusCode(),
236-
response.getEntity(String.class));
259+
message);
237260
}
238261
}
239262

modules/swagger-codegen/src/main/resources/Java/pom.mustache

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,10 @@
157157
</dependency>
158158
</dependencies>
159159
<properties>
160-
<swagger-annotations-version>1.5.0-M1</swagger-annotations-version>
161-
<jersey-version>1.7</jersey-version>
162-
<jackson-version>2.1.4</jackson-version>
160+
<swagger-annotations-version>1.5.3-M1</swagger-annotations-version>
161+
<jersey-version>1.18</jersey-version>
162+
<jackson-version>2.4.2</jackson-version>
163163
<jodatime-version>2.3</jodatime-version>
164-
<junit-version>4.8.1</junit-version>
165164
<maven-plugin-version>1.0.0</maven-plugin-version>
166165
<junit-version>4.8.1</junit-version>
167166
</properties>

modules/swagger-generator/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<id>start-jetty</id>
105105
<phase>pre-integration-test</phase>
106106
<goals>
107-
<goal>run</goal>
107+
<goal>start</goal>
108108
</goals>
109109
<configuration>
110110
<scanIntervalSeconds>0</scanIntervalSeconds>
@@ -226,7 +226,7 @@
226226
<maven-plugin-version>1.0.0</maven-plugin-version>
227227
<servlet-api-version>2.5</servlet-api-version>
228228
<zip-version>1.3.2</zip-version>
229-
<jetty-version>9.0.7.v20131107</jetty-version>
229+
<jetty-version>9.2.9.v20150224</jetty-version>
230230
<jersey2-version>2.4.1</jersey2-version>
231231
</properties>
232232
</project>

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
</activation>
289289
<modules>
290290
<module>samples/client/petstore/objc</module>
291+
<module>samples/client/petstore/java</module>
291292
</modules>
292293
</profile>
293294
</profiles>
@@ -363,7 +364,7 @@
363364
<swagger-parser-version>1.0.3</swagger-parser-version>
364365
<scala-version>2.11.1</scala-version>
365366
<felix-version>2.3.4</felix-version>
366-
<swagger-core-version>1.5.3-M1-SNAPSHOT</swagger-core-version>
367+
<swagger-core-version>1.5.3-M1</swagger-core-version>
367368
<scala-test-version>2.1.4</scala-test-version>
368369
<commons-io-version>2.3</commons-io-version>
369370
<commons-cli-version>1.2</commons-cli-version>

samples/client/petstore/java/pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,10 @@
157157
</dependency>
158158
</dependencies>
159159
<properties>
160-
<swagger-annotations-version>1.5.0-M1</swagger-annotations-version>
161-
<jersey-version>1.7</jersey-version>
162-
<jackson-version>2.1.4</jackson-version>
160+
<swagger-annotations-version>1.5.3-M1</swagger-annotations-version>
161+
<jersey-version>1.18</jersey-version>
162+
<jackson-version>2.4.2</jackson-version>
163163
<jodatime-version>2.3</jodatime-version>
164-
<junit-version>4.8.1</junit-version>
165164
<maven-plugin-version>1.0.0</maven-plugin-version>
166165
<junit-version>4.8.1</junit-version>
167166
</properties>

samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import javax.ws.rs.core.Response.Status.Family;
1717
import javax.ws.rs.core.MediaType;
1818

19+
import java.util.Collection;
1920
import java.util.Map;
2021
import java.util.HashMap;
2122
import java.util.List;
@@ -83,11 +84,19 @@ public static String parameterToString(Object param) {
8384
return "";
8485
} else if (param instanceof Date) {
8586
return formatDateTime((Date) param);
87+
} else if (param instanceof Collection) {
88+
StringBuilder b = new StringBuilder();
89+
for(Object o : (Collection)param) {
90+
if(b.length() > 0) {
91+
b.append(",");
92+
}
93+
b.append(String.valueOf(o));
94+
}
95+
return b.toString();
8696
} else {
8797
return String.valueOf(param);
8898
}
8999
}
90-
91100
public void enableDebug() {
92101
isDebug = true;
93102
}
@@ -217,7 +226,7 @@ else if ("PUT".equals(method)) {
217226
}
218227
else if ("DELETE".equals(method)) {
219228
if(body == null)
220-
response = builder.delete(ClientResponse.class, serialize(body));
229+
response = builder.delete(ClientResponse.class);
221230
else
222231
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
223232
}
@@ -228,12 +237,26 @@ else if ("DELETE".equals(method)) {
228237
return null;
229238
}
230239
else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
231-
return (String) response.getEntity(String.class);
240+
if(response.hasEntity()) {
241+
return (String) response.getEntity(String.class);
242+
}
243+
else {
244+
return "";
245+
}
232246
}
233247
else {
248+
String message = "error";
249+
if(response.hasEntity()) {
250+
try{
251+
message = String.valueOf(response.getEntity(String.class));
252+
}
253+
catch (RuntimeException e) {
254+
// e.printStackTrace();
255+
}
256+
}
234257
throw new ApiException(
235258
response.getClientResponseStatus().getStatusCode(),
236-
response.getEntity(String.class));
259+
message);
237260
}
238261
}
239262

samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.File;
1212

1313
import com.sun.jersey.multipart.FormDataMultiPart;
14+
import com.sun.jersey.multipart.file.FileDataBodyPart;
1415

1516
import javax.ws.rs.core.MediaType;
1617

@@ -76,12 +77,7 @@ public void updatePet (Pet body) throws ApiException {
7677
return ;
7778
}
7879
} catch (ApiException ex) {
79-
if(ex.getCode() == 404) {
80-
return ;
81-
}
82-
else {
83-
throw ex;
84-
}
80+
throw ex;
8581
}
8682
}
8783

@@ -126,12 +122,7 @@ public void addPet (Pet body) throws ApiException {
126122
return ;
127123
}
128124
} catch (ApiException ex) {
129-
if(ex.getCode() == 404) {
130-
return ;
131-
}
132-
else {
133-
throw ex;
134-
}
125+
throw ex;
135126
}
136127
}
137128

@@ -178,12 +169,7 @@ public List<Pet> findPetsByStatus (List<String> status) throws ApiException {
178169
return null;
179170
}
180171
} catch (ApiException ex) {
181-
if(ex.getCode() == 404) {
182-
return null;
183-
}
184-
else {
185-
throw ex;
186-
}
172+
throw ex;
187173
}
188174
}
189175

@@ -230,12 +216,7 @@ public List<Pet> findPetsByTags (List<String> tags) throws ApiException {
230216
return null;
231217
}
232218
} catch (ApiException ex) {
233-
if(ex.getCode() == 404) {
234-
return null;
235-
}
236-
else {
237-
throw ex;
238-
}
219+
throw ex;
239220
}
240221
}
241222

@@ -281,12 +262,7 @@ public Pet getPetById (Long petId) throws ApiException {
281262
return null;
282263
}
283264
} catch (ApiException ex) {
284-
if(ex.getCode() == 404) {
285-
return null;
286-
}
287-
else {
288-
throw ex;
289-
}
265+
throw ex;
290266
}
291267
}
292268

@@ -340,12 +316,7 @@ public void updatePetWithForm (String petId, String name, String status) throws
340316
return ;
341317
}
342318
} catch (ApiException ex) {
343-
if(ex.getCode() == 404) {
344-
return ;
345-
}
346-
else {
347-
throw ex;
348-
}
319+
throw ex;
349320
}
350321
}
351322

@@ -392,12 +363,7 @@ public void deletePet (String apiKey, Long petId) throws ApiException {
392363
return ;
393364
}
394365
} catch (ApiException ex) {
395-
if(ex.getCode() == 404) {
396-
return ;
397-
}
398-
else {
399-
throw ex;
400-
}
366+
throw ex;
401367
}
402368
}
403369

@@ -431,7 +397,8 @@ public void uploadFile (Long petId, String additionalMetadata, File file) throws
431397
mp.field("additionalMetadata", ApiInvoker.parameterToString(additionalMetadata), MediaType.MULTIPART_FORM_DATA_TYPE);
432398

433399
hasFields = true;
434-
mp.field("file", file, MediaType.MULTIPART_FORM_DATA_TYPE);
400+
mp.field("file", file.getName());
401+
mp.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE));
435402

436403
if(hasFields)
437404
postBody = mp;
@@ -451,12 +418,7 @@ public void uploadFile (Long petId, String additionalMetadata, File file) throws
451418
return ;
452419
}
453420
} catch (ApiException ex) {
454-
if(ex.getCode() == 404) {
455-
return ;
456-
}
457-
else {
458-
throw ex;
459-
}
421+
throw ex;
460422
}
461423
}
462424

0 commit comments

Comments
 (0)