mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-21 17:17:08 +00:00
add a generator for spring cloud feign
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
{{^interfaceOnly}}
|
||||
# {{artifactId}}
|
||||
|
||||
## Requirements
|
||||
|
||||
Building the API client library requires [Maven](https://maven.apache.org/) to be installed.
|
||||
|
||||
## Installation
|
||||
|
||||
To install the API client library to your local Maven repository, simply execute:
|
||||
|
||||
```shell
|
||||
mvn install
|
||||
```
|
||||
|
||||
To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:
|
||||
|
||||
```shell
|
||||
mvn deploy
|
||||
```
|
||||
|
||||
Refer to the [official documentation](https://maven.apache.org/plugins/maven-deploy-plugin/usage.html) for more information.
|
||||
|
||||
### Maven users
|
||||
|
||||
Add this dependency to your project's POM:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>{{{groupId}}}</groupId>
|
||||
<artifactId>{{{artifactId}}}</artifactId>
|
||||
<version>{{{artifactVersion}}}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### Gradle users
|
||||
|
||||
Add this dependency to your project's build file:
|
||||
|
||||
```groovy
|
||||
compile "{{{groupId}}}:{{{artifactId}}}:{{{artifactVersion}}}"
|
||||
```
|
||||
|
||||
### Others
|
||||
|
||||
At first generate the JAR by executing:
|
||||
|
||||
mvn package
|
||||
|
||||
Then manually install the following JARs:
|
||||
|
||||
* target/{{{artifactId}}}-{{{artifactVersion}}}.jar
|
||||
* target/lib/*.jar
|
||||
{{/interfaceOnly}}
|
||||
{{#interfaceOnly}}
|
||||
# Swagger generated API stub
|
||||
|
||||
Spring Framework stub
|
||||
|
||||
|
||||
## Overview
|
||||
This code was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project.
|
||||
By using the [OpenAPI-Spec](https://github.com/swagger-api/swagger-core), you can easily generate an API stub.
|
||||
This is an example of building API stub interfaces in Java using the Spring framework.
|
||||
|
||||
The stubs generated can be used in your existing Spring-MVC or Spring-Boot application to create controller endpoints
|
||||
by adding ```@Controller``` classes that implement the interface. Eg:
|
||||
```java
|
||||
@Controller
|
||||
public class PetController implements PetApi {
|
||||
// implement all PetApi methods
|
||||
}
|
||||
```
|
||||
|
||||
You can also use the interface to create [Spring-Cloud Feign clients](http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign-inheritance).Eg:
|
||||
```java
|
||||
@FeignClient(name="pet", url="http://petstore.swagger.io/v2")
|
||||
public interface PetClient extends PetApi {
|
||||
|
||||
}
|
||||
```
|
||||
{{/interfaceOnly}}
|
||||
@@ -0,0 +1,8 @@
|
||||
package {{package}};
|
||||
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import {{configPackage}}.ClientConfiguration;
|
||||
|
||||
@FeignClient(name="${ {{{title}}}.name:{{{title}}} }", url="${ {{{title}}}.url:{{{basePath}}} }", configuration = ClientConfiguration.class)
|
||||
public interface {{classname}}Client extends {{classname}} {
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package {{configPackage}};
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import feign.Util;
|
||||
|
||||
|
||||
public class ApiKeyRequestInterceptor implements RequestInterceptor {
|
||||
private final String location;
|
||||
private final String name;
|
||||
private String value;
|
||||
|
||||
public ApiKeyRequestInterceptor(String location, String name, String value) {
|
||||
Util.checkNotNull(location, "location", new Object[0]);
|
||||
Util.checkNotNull(name, "name", new Object[0]);
|
||||
Util.checkNotNull(value, "value", new Object[0]);
|
||||
this.location = location;
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
if(location.equals("header")) {
|
||||
requestTemplate.header(name, value);
|
||||
} else if(location.equals("query")) {
|
||||
requestTemplate.query(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package {{configPackage}};
|
||||
|
||||
import feign.Logger;
|
||||
import feign.auth.BasicAuthRequestInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor;
|
||||
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails;
|
||||
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
||||
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
|
||||
import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitResourceDetails;
|
||||
import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
|
||||
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public class ClientConfiguration {
|
||||
|
||||
{{#authMethods}}
|
||||
{{#isBasic}}
|
||||
@Value("${ {{{title}}}.security.{{{name}}}.username }")
|
||||
private String {{{name}}}Username;
|
||||
|
||||
@Value("${ {{{title}}}.security.{{{name}}}.password }")
|
||||
private String {{{name}}}Password;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "{{{title}}}.security.{{{name}}}.username")
|
||||
public BasicAuthRequestInterceptor {{{name}}}RequestInterceptor() {
|
||||
return new BasicAuthRequestInterceptor(this.{{{name}}}Username, this.{{{name}}}Password);
|
||||
}
|
||||
|
||||
{{/isBasic}}
|
||||
{{#isApiKey}}
|
||||
@Value("${ {{{title}}}.security.{{{name}}}.key }")
|
||||
private String {{{name}}}Key;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "{{{title}}}.security.{{{name}}}.key")
|
||||
public ApiKeyRequestInterceptor {{{name}}}RequestInterceptor() {
|
||||
return new ApiKeyRequestInterceptor({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{{keyParamName}}}", this.{{{name}}}Key);
|
||||
}
|
||||
|
||||
{{/isApiKey}}
|
||||
{{#isOAuth}}
|
||||
@Bean
|
||||
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
|
||||
public OAuth2FeignRequestInterceptor {{{name}}}RequestInterceptor() {
|
||||
return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), {{{name}}}ResourceDetails());
|
||||
}
|
||||
|
||||
{{#isCode}}
|
||||
@Bean
|
||||
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
|
||||
@ConfigurationProperties("{{{title}}}.security.{{{name}}}")
|
||||
public AuthorizationCodeResourceDetails {{{name}}}ResourceDetails() {
|
||||
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
|
||||
details.setAccessTokenUri("{{{tokenUrl}}}");
|
||||
details.setUserAuthorizationUri("{{{authorizationUrl}}}");
|
||||
return details;
|
||||
}
|
||||
|
||||
{{/isCode}}
|
||||
{{#isPassword}}
|
||||
@Bean
|
||||
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
|
||||
@ConfigurationProperties("{{{title}}}.security.{{{name}}}")
|
||||
public ResourceOwnerPasswordResourceDetails {{{name}}}ResourceDetails() {
|
||||
ResourceOwnerPasswordResourceDetails details = new ResourceOwnerPasswordResourceDetails();
|
||||
details.setAccessTokenUri("{{{tokenUrl}}}");
|
||||
return details;
|
||||
}
|
||||
|
||||
{{/isPassword}}
|
||||
{{#isApplication}}
|
||||
@Bean
|
||||
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
|
||||
@ConfigurationProperties("{{{title}}}.security.{{{name}}}")
|
||||
public ClientCredentialsResourceDetails {{{name}}}ResourceDetails() {
|
||||
ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
|
||||
details.setAccessTokenUri("{{{tokenUrl}}}");
|
||||
return details;
|
||||
}
|
||||
|
||||
{{/isApplication}}
|
||||
{{#isImplicit}}
|
||||
@Bean
|
||||
@ConditionalOnProperty("{{{title}}}.security.{{{name}}}.client-id")
|
||||
@ConfigurationProperties("{{{title}}}.security.{{{name}}}")
|
||||
public ImplicitResourceDetails {{{name}}}ResourceDetails() {
|
||||
ImplicitResourceDetails details = new ImplicitResourceDetails();
|
||||
details.setUserAuthorizationUri("{{{authorizationUrl}}}");
|
||||
return details;
|
||||
}
|
||||
|
||||
{{/isImplicit}}
|
||||
{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<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>
|
||||
<properties>
|
||||
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<swagger-core-version>1.5.9</swagger-core-version>
|
||||
</properties>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.5.RELEASE</version>
|
||||
</parent>
|
||||
<build>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
{{^interfaceOnly}}
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
{{/interfaceOnly}}
|
||||
</build>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-parent</artifactId>
|
||||
<version>Brixton.SR1</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
<version>${swagger-core-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-feign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security.oauth</groupId>
|
||||
<artifactId>spring-security-oauth2</artifactId>
|
||||
</dependency>
|
||||
{{#java8}}
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
</dependency>
|
||||
{{/java8}}
|
||||
{{^java8}}
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-joda</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
</dependency>
|
||||
{{/java8}}
|
||||
</dependencies>
|
||||
</project>
|
||||
Reference in New Issue
Block a user