forked from loafle/openapi-generator-original
fix (JAVA SPRING RESTCLIENT) 19137: Fix missing imports when using withXml for Spring RestClient and fix its README (#19146)
This commit is contained in:
parent
cfcc2c89d6
commit
045f4b6067
@ -1,5 +1,9 @@
|
|||||||
package {{invokerPackage}};
|
package {{invokerPackage}};
|
||||||
|
|
||||||
|
{{#withXml}}
|
||||||
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||||
|
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
|
||||||
|
{{/withXml}}
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
@ -23,12 +23,7 @@
|
|||||||
Building the API client library requires:
|
Building the API client library requires:
|
||||||
|
|
||||||
1. Java 17+
|
1. Java 17+
|
||||||
{{#jersey2}}
|
|
||||||
2. Maven (3.8.3+)/Gradle (7.2+)
|
|
||||||
{{/jersey2}}
|
|
||||||
{{^jersey2}}
|
|
||||||
2. Maven/Gradle
|
2. Maven/Gradle
|
||||||
{{/jersey2}}
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@ -87,33 +82,6 @@ Then manually install the following JARs:
|
|||||||
- `target/{{{artifactId}}}-{{{artifactVersion}}}.jar`
|
- `target/{{{artifactId}}}-{{{artifactVersion}}}.jar`
|
||||||
- `target/lib/*.jar`
|
- `target/lib/*.jar`
|
||||||
|
|
||||||
{{#jersey2}}
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
To add a HTTP proxy for the API client, use `ClientConfig`:
|
|
||||||
```java
|
|
||||||
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
|
|
||||||
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
|
|
||||||
import org.glassfish.jersey.client.ClientConfig;
|
|
||||||
import org.glassfish.jersey.client.ClientProperties;
|
|
||||||
import {{{invokerPackage}}}.*;
|
|
||||||
import {{{package}}}.{{{classname}}};
|
|
||||||
|
|
||||||
...
|
|
||||||
|
|
||||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
|
||||||
ClientConfig clientConfig = defaultClient.getClientConfig();
|
|
||||||
clientConfig.connectorProvider(new ApacheConnectorProvider());
|
|
||||||
clientConfig.property(ClientProperties.PROXY_URI, "http://proxy_url_here");
|
|
||||||
clientConfig.property(ClientProperties.PROXY_USERNAME, "proxy_username");
|
|
||||||
clientConfig.property(ClientProperties.PROXY_PASSWORD, "proxy_password");
|
|
||||||
defaultClient.setClientConfig(clientConfig);
|
|
||||||
|
|
||||||
{{{classname}}} apiInstance = new {{{classname}}}(defaultClient);
|
|
||||||
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
|
|
||||||
```
|
|
||||||
|
|
||||||
{{/jersey2}}
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
Please follow the [installation](#installation) instruction and execute the following Java code:
|
Please follow the [installation](#installation) instruction and execute the following Java code:
|
||||||
@ -128,7 +96,7 @@ import {{{package}}}.{{{classname}}};
|
|||||||
public class {{{classname}}}Example {
|
public class {{{classname}}}Example {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
ApiClient defaultClient = new ApiClient();
|
||||||
defaultClient.setBasePath("{{{basePath}}}");
|
defaultClient.setBasePath("{{{basePath}}}");
|
||||||
{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
|
{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
|
||||||
// Configure HTTP basic authorization: {{{name}}}
|
// Configure HTTP basic authorization: {{{name}}}
|
||||||
@ -173,10 +141,10 @@ public class {{{classname}}}Example {
|
|||||||
try {
|
try {
|
||||||
{{#returnType}}{{{.}}} result = {{/returnType}}apiInstance.{{{operationId}}}({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});{{#returnType}}
|
{{#returnType}}{{{.}}} result = {{/returnType}}apiInstance.{{{operationId}}}({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});{{#returnType}}
|
||||||
System.out.println(result);{{/returnType}}
|
System.out.println(result);{{/returnType}}
|
||||||
} catch (ApiException e) {
|
} catch (HttpStatusCodeException e) {
|
||||||
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
|
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
|
||||||
System.err.println("Status code: " + e.getCode());
|
System.err.println("Status code: " + e.getStatusCode().value());
|
||||||
System.err.println("Reason: " + e.getResponseBody());
|
System.err.println("Reason: " + e.getResponseBodyAsString());
|
||||||
System.err.println("Response headers: " + e.getResponseHeaders());
|
System.err.println("Response headers: " + e.getResponseHeaders());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -2955,4 +2955,26 @@ public class JavaClientCodegenTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testRestClientWithXML_issue_19137() {
|
||||||
|
final Path output = newTempFolder();
|
||||||
|
final CodegenConfigurator configurator = new CodegenConfigurator()
|
||||||
|
.setGeneratorName("java")
|
||||||
|
.setLibrary(JavaClientCodegen.RESTCLIENT)
|
||||||
|
.setAdditionalProperties(Map.of(
|
||||||
|
CodegenConstants.API_PACKAGE, "xyz.abcdef.api",
|
||||||
|
CodegenConstants.WITH_XML, true
|
||||||
|
))
|
||||||
|
.setInputSpec("src/test/resources/3_1/java/petstore.yaml")
|
||||||
|
.setOutputDir(output.toString().replace("\\", "/"));
|
||||||
|
|
||||||
|
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
|
||||||
|
|
||||||
|
validateJavaSourceFiles(files);
|
||||||
|
TestUtils.assertFileContains(
|
||||||
|
output.resolve("src/main/java/xyz/abcdef/ApiClient.java"),
|
||||||
|
"import com.fasterxml.jackson.dataformat.xml.XmlMapper;",
|
||||||
|
"import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ import org.openapitools.client.api.AuthApi;
|
|||||||
public class AuthApiExample {
|
public class AuthApiExample {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
ApiClient defaultClient = new ApiClient();
|
||||||
defaultClient.setBasePath("http://localhost:3000");
|
defaultClient.setBasePath("http://localhost:3000");
|
||||||
|
|
||||||
// Configure HTTP basic authorization: http_auth
|
// Configure HTTP basic authorization: http_auth
|
||||||
@ -101,10 +101,10 @@ public class AuthApiExample {
|
|||||||
try {
|
try {
|
||||||
String result = apiInstance.testAuthHttpBasic();
|
String result = apiInstance.testAuthHttpBasic();
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
} catch (ApiException e) {
|
} catch (HttpStatusCodeException e) {
|
||||||
System.err.println("Exception when calling AuthApi#testAuthHttpBasic");
|
System.err.println("Exception when calling AuthApi#testAuthHttpBasic");
|
||||||
System.err.println("Status code: " + e.getCode());
|
System.err.println("Status code: " + e.getStatusCode().value());
|
||||||
System.err.println("Reason: " + e.getResponseBody());
|
System.err.println("Reason: " + e.getResponseBodyAsString());
|
||||||
System.err.println("Response headers: " + e.getResponseHeaders());
|
System.err.println("Response headers: " + e.getResponseHeaders());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -89,17 +89,17 @@ import org.openapitools.client.api.ResourceApi;
|
|||||||
public class ResourceApiExample {
|
public class ResourceApiExample {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
ApiClient defaultClient = new ApiClient();
|
||||||
defaultClient.setBasePath("http://localhost");
|
defaultClient.setBasePath("http://localhost");
|
||||||
|
|
||||||
ResourceApi apiInstance = new ResourceApi(defaultClient);
|
ResourceApi apiInstance = new ResourceApi(defaultClient);
|
||||||
try {
|
try {
|
||||||
File result = apiInstance.resourceInResponse();
|
File result = apiInstance.resourceInResponse();
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
} catch (ApiException e) {
|
} catch (HttpStatusCodeException e) {
|
||||||
System.err.println("Exception when calling ResourceApi#resourceInResponse");
|
System.err.println("Exception when calling ResourceApi#resourceInResponse");
|
||||||
System.err.println("Status code: " + e.getCode());
|
System.err.println("Status code: " + e.getStatusCode().value());
|
||||||
System.err.println("Reason: " + e.getResponseBody());
|
System.err.println("Reason: " + e.getResponseBodyAsString());
|
||||||
System.err.println("Response headers: " + e.getResponseHeaders());
|
System.err.println("Response headers: " + e.getResponseHeaders());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -89,17 +89,17 @@ import org.openapitools.client.api.DefaultApi;
|
|||||||
public class DefaultApiExample {
|
public class DefaultApiExample {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
ApiClient defaultClient = new ApiClient();
|
||||||
defaultClient.setBasePath("http://localhost");
|
defaultClient.setBasePath("http://localhost");
|
||||||
|
|
||||||
DefaultApi apiInstance = new DefaultApi(defaultClient);
|
DefaultApi apiInstance = new DefaultApi(defaultClient);
|
||||||
try {
|
try {
|
||||||
List<ByteArrayObject> result = apiInstance.nullableArrayTestGet();
|
List<ByteArrayObject> result = apiInstance.nullableArrayTestGet();
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
} catch (ApiException e) {
|
} catch (HttpStatusCodeException e) {
|
||||||
System.err.println("Exception when calling DefaultApi#nullableArrayTestGet");
|
System.err.println("Exception when calling DefaultApi#nullableArrayTestGet");
|
||||||
System.err.println("Status code: " + e.getCode());
|
System.err.println("Status code: " + e.getStatusCode().value());
|
||||||
System.err.println("Reason: " + e.getResponseBody());
|
System.err.println("Reason: " + e.getResponseBodyAsString());
|
||||||
System.err.println("Response headers: " + e.getResponseHeaders());
|
System.err.println("Response headers: " + e.getResponseHeaders());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ import org.openapitools.client.api.AnotherFakeApi;
|
|||||||
public class AnotherFakeApiExample {
|
public class AnotherFakeApiExample {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
ApiClient defaultClient = new ApiClient();
|
||||||
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
|
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
|
||||||
|
|
||||||
AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient);
|
AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient);
|
||||||
@ -97,10 +97,10 @@ public class AnotherFakeApiExample {
|
|||||||
try {
|
try {
|
||||||
Client result = apiInstance.call123testSpecialTags(client);
|
Client result = apiInstance.call123testSpecialTags(client);
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
} catch (ApiException e) {
|
} catch (HttpStatusCodeException e) {
|
||||||
System.err.println("Exception when calling AnotherFakeApi#call123testSpecialTags");
|
System.err.println("Exception when calling AnotherFakeApi#call123testSpecialTags");
|
||||||
System.err.println("Status code: " + e.getCode());
|
System.err.println("Status code: " + e.getStatusCode().value());
|
||||||
System.err.println("Reason: " + e.getResponseBody());
|
System.err.println("Reason: " + e.getResponseBodyAsString());
|
||||||
System.err.println("Response headers: " + e.getResponseHeaders());
|
System.err.println("Response headers: " + e.getResponseHeaders());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ import org.openapitools.client.api.AnotherFakeApi;
|
|||||||
public class AnotherFakeApiExample {
|
public class AnotherFakeApiExample {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
ApiClient defaultClient = new ApiClient();
|
||||||
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
|
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
|
||||||
|
|
||||||
AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient);
|
AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient);
|
||||||
@ -97,10 +97,10 @@ public class AnotherFakeApiExample {
|
|||||||
try {
|
try {
|
||||||
Client result = apiInstance.call123testSpecialTags(client);
|
Client result = apiInstance.call123testSpecialTags(client);
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
} catch (ApiException e) {
|
} catch (HttpStatusCodeException e) {
|
||||||
System.err.println("Exception when calling AnotherFakeApi#call123testSpecialTags");
|
System.err.println("Exception when calling AnotherFakeApi#call123testSpecialTags");
|
||||||
System.err.println("Status code: " + e.getCode());
|
System.err.println("Status code: " + e.getStatusCode().value());
|
||||||
System.err.println("Reason: " + e.getResponseBody());
|
System.err.println("Reason: " + e.getResponseBodyAsString());
|
||||||
System.err.println("Response headers: " + e.getResponseHeaders());
|
System.err.println("Response headers: " + e.getResponseHeaders());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user