This addon provides standalone functionality, and exports services for use in other addons.
From Forge CLI:
addon-install-from-git --url https://github.com/forge/springboot-addon.git
The Spring Boot addon reuses standard commands adding options and/or new commands only when required.
- Spring Boot Project type
-
If you want to create a new Spring Boot starter project as you can do using
start.spring.io
, simply use theproject-new
command where you includespring-boot
as project type. The command will issue a REST call againststart.spring.io
, will get a zip file containing the maven project created according to the parameters passed.The content of this file will be unzipped locally under the directory as defined by the
--named
parameter.When no Spring Boot starters / dependencies are defined, then
start.spring.io
will return a project containing thespring-boot-starter
andspring-boot-starter-test
maven artifacts using the default Spring version which is1.5.3.RELEASE
project-new --named example --type spring-boot
- Starter Version and dependencies
-
You can specify which Spring Boot starter version you want to use when the project will be created using the
--spring-boot-version
option like also the starters to be included within the pom.xml file with the--dependencies
option, which is defined as a space-separated list of dependencies.project-new --named example --type spring-boot --spring-boot-version 1.4.3.RELEASE --dependencies actuator web
NoteBy pressing tab after typing --dependencies
option will result in Forge retrieving the list of available dependencies that can then be auto-completed. - One line
-
You can create your project using one
command line
with the required parameters:project-new --named demo --version 1.0 --top-level-package org.demo --type spring-boot --spring-boot-version 1.4.3.RELEASE --dependencies actuator elasticsearch
- Persistence setup
-
Whenever it is required to use the
spring-boot-starter-data-jpa
starter and to setup the Hibernate provider, then you can use the following commandjpa-setup
The persistence provider will be configured with the appropriate driver and sane default settings. Also, by default, if no database is specified, H2 database "in-memory" is used.
To configure by example mysql, pass these parameters:
jpa-setup --db-type MYSQL --database-url jdbc:mysql://mysql:3306/catalogdb --username mysql --password mysql
and the command will populate accordingly the Spring jpa and datasource keys
spring.datasource.url=jdbc\:mysql\://mysql\:3306/catalogdb spring.datasource.username=mysql spring.datasource.password=mysql spring.jpa.properties.hibernate.transaction.flush_before_completion=true spring.jpa.properties.hibernate.show_sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
- Entities & fields
-
In order to create an
Entity
representing the Java class to be mapped with a Database table and its fields, use this commandjpa-new-entity --named Album jpa-new-field --named artist jpa-new-field --named title jpa-new-field --named description --length 2000 jpa-new-field --named price --type java.lang.Float jpa-new-field --named publication_date --type java.util.Date --temporalType DATE
Then a new JPA entity will be created, fields included.
The command also generates a
PagingAndSortingRepository
implementation that can be used with thespring-boot-starter-data-rest
starter to automatically expose a REST endpoint for this entity:import org.springframework.data.repository.PagingAndSortingRepository; public interface AlbumRepository extends PagingAndSortingRepository<Album, Long> { }
For more details on how to access JPA entities RESTfully with Spring Boot, please take a look at https://spring.io/guides/gs/accessing-data-rest/.
PagingAndSortingRepository
is described in the "Create a Person repository" section of that document. - Expose your entities as REST endpoints using JAX-RS
-
The command
rest-generate-endpoints-from-entities
will generate JAX-RS RESTful endpoints based on JPA entities. The behavior is customized for Spring Boot and defaults to use the Apache CXF stack which supports theJAX-RS
specification and is packaged within thecxf-spring-boot-starter-jaxrs
starter.The Spring Boot mode is activated by using the
SPRING_BOOT_JPA_ENTITY
generator:rest-generate-endpoints-from-entities --generator SPRING_BOOT_JPA_ENTITY --targets ...
Example of code generated:
@Path("/catalogs") @Component @Transactional public class CatalogEndpoint { @PersistenceContext private EntityManager em; @POST @Consumes("application/json") public Response create(Catalog entity) { em.persist(entity); return Response.created( UriBuilder.fromResource(CatalogEndpoint.class) .path(String.valueOf(entity.getId())).build()).build(); }
WarningNote that the resources derived from the JPA entities are currently limited to JSON representations (with the Jackson JSON stack). - Generate a REST endpoint
-
The project generated by
start.spring.io
is pretty lean as it only includes aDemoApplication
class annotated with the@SpringBootApplication
annotation performing the Spring Boot magic and anapplication.properties
file. By using the following command you will be able to generate a REST controller class exposing a/greeting
endpoint and generating a response using theGreeting
model class.To create the controller, issue this command where the name passed will be used to create the Java class under the package name of the project
rest-new-endpoint --named GreetingController
To define the path to access the REST endpoints, extend the command with the
--path
parameterrest-new-endpoint --named GreetingController --path api
- Support CORS
-
The
rest-new-cross-origin-resource-sharing-filter
command will create a CORS filter so that cross-origin requests are allowed. The filter is also annotated so that it is automatically recognized by the Apache CXF implementation when it will start to scan the classes to check if some contain the annotation@Component
.Example:
@Provider @Component public class NewCrossOriginResourceSharingFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext request, ContainerResponseContext response) { response.getHeaders().putSingle("Access-Control-Allow-Origin", "*"); response.getHeaders().putSingle("Access-Control-Expose-Headers", "Location"); response.getHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); response.getHeaders() .putSingle("Access-Control-Allow-Headers", "Content-Type, User-Agent, X-Requested-With, X-Requested-By, Cache-Control"); response.getHeaders().putSingle("Access-Control-Allow-Credentials", "true"); } }
- Define the starters to be selected
-
If you want to restrict the list of dependencies/starters to be selected (or to use according to your needs) when you will create your project, pass the reference of this file using the following ENV variable
SPRING_BOOT_CONFIG_FILE
to the commandproject-new
export SPRING_BOOT_CONFIG_FILE=file:///path/to/your/spring-boot-application.yaml project-new --type spring-boot --spring-boot-version 1.4.3.RELEASE --dependencies ...
An example of such a file is available. Only the dependencies section will be used to populate the pom.xml.
Note
|
You can create such a file according to the convention defined by Spring Initialzr |