Add support for using Spring HATEOAS to add links in the spring gener… (#1130)

* Add support for using Spring HATEOAS to add links in the spring generator.

* Ensure that Spring HATEOAS links appear last in the JSON serialisation of objects.

* A couple of changes following code review:
1. Make sure the @JsonPropertyOrder annotation is only used when the jackson library is being used since it's a part of the jackson library.
2. Make sure to include the Spring HATEOAS dependency in the pom file for the spring-cloud and spring-mvc generators when the hateoas option is enabled.

* Don't order the json properties since there's no requirement for the links to be last.

* Remove unnecessary import.
This commit is contained in:
mwoodland
2018-10-23 15:56:03 +01:00
committed by William Cheng
parent 4a0bc8f177
commit 258de8909d
7 changed files with 42 additions and 1 deletions

View File

@@ -164,6 +164,9 @@ CONFIG OPTIONS for spring
useOptional
Use Optional container for optional parameters (Default: false)
hateoas
Use Spring HATEOAS library to allow adding HATEOAS links (Default: false)
library
library template (sub-template) to use (Default: spring-boot)
spring-boot - Spring-boot Server application using the SpringFox integration.

View File

@@ -74,6 +74,7 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String IMPLICIT_HEADERS = "implicitHeaders";
public static final String OPENAPI_DOCKET_CONFIG = "swaggerDocketConfig";
public static final String API_FIRST = "apiFirst";
public static final String HATEOAS = "hateoas";
protected String title = "OpenAPI Spring";
protected String configPackage = "org.openapitools.configuration";
@@ -93,6 +94,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean apiFirst = false;
protected boolean useOptional = false;
protected boolean virtualService = false;
protected boolean hateoas = false;
public SpringCodegen() {
super();
@@ -124,6 +126,7 @@ public class SpringCodegen extends AbstractJavaCodegen
cliOptions.add(CliOption.newBoolean(OPENAPI_DOCKET_CONFIG, "Generate Spring OpenAPI Docket configuration class.", openapiDocketConfig));
cliOptions.add(CliOption.newBoolean(API_FIRST, "Generate the API from the OAI spec at server compile time (API first approach)", apiFirst));
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,"Use Optional container for optional parameters", useOptional));
cliOptions.add(CliOption.newBoolean(HATEOAS, "Use Spring HATEOAS library to allow adding HATEOAS links", hateoas));
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application using the SpringFox integration.");
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@@ -260,6 +263,10 @@ public class SpringCodegen extends AbstractJavaCodegen
if (additionalProperties.containsKey(API_FIRST)) {
this.setApiFirst(Boolean.valueOf(additionalProperties.get(API_FIRST).toString()));
}
if (additionalProperties.containsKey(HATEOAS)) {
this.setHateoas(Boolean.valueOf(additionalProperties.get(HATEOAS).toString()));
}
typeMapping.put("file", "Resource");
importMapping.put("Resource", "org.springframework.core.io.Resource");
@@ -700,6 +707,10 @@ public class SpringCodegen extends AbstractJavaCodegen
public void setApiFirst(boolean apiFirst) {
this.apiFirst = apiFirst;
}
public void setHateoas(boolean hateoas) {
this.hateoas = hateoas;
}
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {

View File

@@ -164,5 +164,12 @@
</dependency>
<!-- END Virtual Service API support -->
{{/virtualService}}
{{#hateoas}}
<!-- Spring HATEOAS -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
{{/hateoas}}
</dependencies>
</project>

View File

@@ -86,5 +86,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
{{#hateoas}}
<!-- Spring HATEOAS -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
{{/hateoas}}
</dependencies>
</project>

View File

@@ -222,6 +222,14 @@
<scope>provided</scope>
</dependency>
{{/useBeanValidation}}
{{#hateoas}}
<!-- Spring HATEOAS -->
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.25.0.RELEASE</version>
</dependency>
{{/hateoas}}
</dependencies>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>

View File

@@ -19,6 +19,11 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
{{#withXml}}
import javax.xml.bind.annotation.*;
{{/withXml}}
{{^parent}}
{{#hateoas}}
import org.springframework.hateoas.ResourceSupport;
{{/hateoas}}
{{/parent}}
{{#models}}
{{#model}}

View File

@@ -3,7 +3,7 @@
*/{{#description}}
@ApiModel(description = "{{{description}}}"){{/description}}
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}{{^parent}}{{#hateoas}}extends ResourceSupport {{/hateoas}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
{{#serializableModel}}
private static final long serialVersionUID = 1L;