Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/forge/springboot-addon

SpringBoot Addon
https://github.com/forge/springboot-addon

addon ide jboss-forge spring-boot

Last synced: about 1 month ago
JSON representation

SpringBoot Addon

Awesome Lists containing this project

README

        

:idprefix: id_
:source-highlighter: pygments

== Spring Boot Forge addon

image:https://forge.ci.cloudbees.com/buildStatus/icon?job=springboot-addon["Build Status", link="https://forge.ci.cloudbees.com/job/springboot-addon"]
image:http://img.shields.io/:license-EPL-blue.svg["License", link="https://www.eclipse.org/legal/epl-v10.html"]
image:https://maven-badges.herokuapp.com/maven-central/org.jboss.forge.addon/spring-boot/badge.svg["Maven Central", link="https://maven-badges.herokuapp.com/maven-central/org.jboss.forge.addon/spring-boot"]

This addon provides *standalone* functionality, and *exports services* for use in other addons.

=== Prerequisites

- JBoss Forge >= 3.7
- Java 8

=== Installation

From Forge CLI:

[source,shell]
----
addon-install-from-git --url https://github.com/forge/springboot-addon.git
----

=== Depends on
[options="header"]
|===
|Addon |Exported |Optional

|ui
|no
|no

|projects
|no
|no

|maven
|no
|no

|parser-java
|no
|no

|parser-json
|no
|no

|parser-yaml
|no
|no

|cdi
|no
|no

|scaffold
|no
|no

|cdi
|no
|no

|rest-client
|no
|no

|templates
|no
|no
|===

== Features
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 the `project-new` command where you include `spring-boot` as project type.
The command will issue a REST call against `start.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
the `spring-boot-starter` and `spring-boot-starter-test` maven artifacts using the default Spring version which is `1.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
----
+
NOTE: By 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 command
+
----
jpa-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 command
+
----
jpa-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 the
`spring-boot-starter-data-rest` starter to automatically expose a REST endpoint for this entity:
+
[source,java]
----
import org.springframework.data.repository.PagingAndSortingRepository;

public interface AlbumRepository extends PagingAndSortingRepository {
}
----
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/[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 the `JAX-RS` specification and is packaged within the `cxf-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:
+
[source,java]
----
@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();
}
----
+
WARNING: Note 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 a `DemoApplication` class annotated
with the `@SpringBootApplication` annotation performing the Spring Boot magic and an `application.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 the `Greeting` 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
+
[source,java]
----
rest-new-endpoint --named GreetingController
----
+
To define the path to access the REST endpoints, extend the command with the `--path` parameter
+
[source,java]
----
rest-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:
+
[source,java]
----
@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 command `project-new`
+
[source,java]
----
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 link:src/main/resources/spring-boot-application.yaml[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 http://docs.spring.io/initializr/docs/current/reference/htmlsingle/#configuration-format[Spring Initialzr]