36
Java - Web Spring Boot + JHipster Eueung Mulyana http://eueung.github.io/java/springboot Java CodeLabs | Attribution-ShareAlike CC BY-SA 1 / 36

Spring Boot and JHipster

Embed Size (px)

Citation preview

Page 1: Spring Boot and JHipster

Java - Web

Spring Boot + JHipsterEueung Mulyana

http://eueung.github.io/java/springbootJava CodeLabs | Attribution-ShareAlike CC BY-SA

1 / 36

Page 2: Spring Boot and JHipster

Agenda

Spring Boot

JHipster

2 / 36

Page 3: Spring Boot and JHipster

Spring Boot #1Spring Boot @ spring.io

3 / 36

Page 4: Spring Boot and JHipster

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version>

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.1.RELEASE</version> </parent>

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>

</project>

Example #1Maven

4 / 36

Page 5: Spring Boot and JHipster

Example #1

src/main/java/SampleController.java

import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;

@Controller@EnableAutoConfigurationpublic class SampleController {

@RequestMapping("/") @ResponseBody String home() { return "Hello World!"; }

public static void main(String[] args) throws Exception SpringApplication.run(SampleController.class, args); }}

5 / 36

Page 6: Spring Boot and JHipster

import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;

@RestController@EnableAutoConfigurationpublic class Example {

@RequestMapping("/") String home() { return "Hello World!"; }

public static void main(String[] args) throws Exception SpringApplication.run(Example.class, args); }

}

Example #1Alternative

6 / 36

Page 7: Spring Boot and JHipster

Example #1Maven

$> mvn spring-boot:run

7 / 36

Page 8: Spring Boot and JHipster

Example #1Gradle

$> gradle build

$> gradle bootRun

build.gradle

buildscript { ext { springBootVersion = '1.3.1.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" }}

apply plugin: 'java'apply plugin: 'spring-boot'

jar { baseName = 'demo' version = '0.0.1-SNAPSHOT'}sourceCompatibility = 1.8targetCompatibility = 1.8

repositories { mavenCentral()}

dependencies { compile('org.springframework.boot:spring-boot-starter-web'}

8 / 36

Page 9: Spring Boot and JHipster

Spring Boot #2Building an Application with Spring Boot

9 / 36

Page 10: Spring Boot and JHipster

src/main/java/hello/Application.java

package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;

@SpringBootApplicationpublic class Application {

public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args);

System.out.println("Let's inspect the beans provided by Spring Boot:"

String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } }

}

Example #2gs-spring-boot

src/main/java/hello/HelloController.java

package hello;

import org.springframework.web.bind.annotation.RestController;import org.springframework.web.bind.annotation.RequestMapping;

@RestControllerpublic class HelloController {

@RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; }

}

10 / 36

Page 11: Spring Boot and JHipster

Example #2Maven

pom.xml

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins></build>

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId> <artifactId>gs-spring-boot</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId <scope>test</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties <build> ... </build></project>

11 / 36

Page 12: Spring Boot and JHipster

$> mvn package && java -jar target/gs-spring-boot-0.1.0.jar...Let's inspect the beans provided by Spring Boot:application...viewControllerHandlerMapping

$> curl localhost:8080Greetings from Spring Boot!

Example #2Maven 

12 / 36

Page 13: Spring Boot and JHipster

Example #2Gradle

$> gradle wrapper

$> ./gradlew build && java -jar build/libs/gs-spring-boot-0.1

buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE" }}

apply plugin: 'java'apply plugin: 'spring-boot'

jar { baseName = 'gs-spring-boot' version = '0.1.0'}

repositories { mavenCentral()}

sourceCompatibility = 1.8targetCompatibility = 1.8

dependencies { compile("org.springframework.boot:spring-boot-starter-web" compile("org.springframework.boot:spring-boot-starter-actuator" testCompile("org.springframework.boot:spring-boot-starter-test"}

task wrapper(type: Wrapper) { gradleVersion = '2.10'}

13 / 36

Page 14: Spring Boot and JHipster

Example #2Unit Tests

package hello;

import static org.hamcrest.Matchers.equalTo;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.http.MediaType;import org.springframework.mock.web.MockServletContext;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.setup.MockMvcBuilders;

src/test/java/hello/HelloControllerTest.java

@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = MockServletContext.class)@WebAppConfigurationpublic class HelloControllerTest {

private MockMvc mvc;

@Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); }

@Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("Greetings from Spring Boot!" }}

14 / 36

Page 15: Spring Boot and JHipster

Example #2Integration Test

package hello;

import static org.hamcrest.Matchers.equalTo;import static org.junit.Assert.assertThat;

import java.net.URL;

import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.test.IntegrationTest;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.boot.test.TestRestTemplate;import org.springframework.http.ResponseEntity;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.web.client.RestTemplate;

src/test/java/hello/HelloControllerIT.java

@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = Application.class)@WebAppConfiguration@IntegrationTest({"server.port=0"})public class HelloControllerIT {

@Value("${local.server.port}") private int port;

private URL base; private RestTemplate template;

@Before public void setUp() throws Exception { this.base = new URL("http://localhost:" + port + "/"); template = new TestRestTemplate(); }

@Test public void getHello() throws Exception { ResponseEntity<String> response = template.getForEntity(base.toString(), String.class); assertThat(response.getBody(), equalTo("Greetings from Spring Boot!" }}

15 / 36

Page 16: Spring Boot and JHipster

Refs/Notes1. Getting Started · Building an Application with Spring Boot2. spring-guides/gs-spring-boot

16 / 36

Page 17: Spring Boot and JHipster

Spring Boot #3Building a RESTful Web Service

17 / 36

Page 18: Spring Boot and JHipster

src/main/java/hello/GreetingController.java

package hello;

import java.util.concurrent.atomic.AtomicLong;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;

@RestControllerpublic class GreetingController {

private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong();

@RequestMapping("/greeting") public Greeting greeting(@RequestParam(value="name", defaultValue= return new Greeting(counter.incrementAndGet(), String.format(template, name)); }}

Example #3gs-rest-service

package hello;

public class Greeting { private final long id; private final String content;

public Greeting(long id, String content) { this.id = id; this.content = content; }

public long getId() { return id; }

public String getContent() { return content; }}

src/main/java/hello/Greeting.java

18 / 36

Page 19: Spring Boot and JHipster

Example #3 

src/main/java/hello/Application.java

package hello;

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplicationpublic class Application {

public static void main(String[] args) { SpringApplication.run(Application.class, args); }}

19 / 36

Page 20: Spring Boot and JHipster

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId> <artifactId>gs-rest-service</artifactId> <version>0.1.0</version>

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.1.RELEASE</version> </parent>

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>

<properties> <java.version>1.8</java.version> </properties

<build>...</build> <repositories>...</repositories> <pluginRepositories>...</pluginRepositories></project>

Example #3Maven

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId </plugin> </plugins></build><repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository></repositories><pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </pluginRepository></pluginRepositories>

20 / 36

Page 21: Spring Boot and JHipster

Example #3Maven

$> mvn clean package$> java -jar target/gs-rest-service-0.1.0.jar

# or$> mvn spring-boot:run

21 / 36

Page 22: Spring Boot and JHipster

build.gradle

buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE" }}

apply plugin: 'java'apply plugin: 'spring-boot'

jar { baseName = 'gs-rest-service' version = '0.1.0'}

repositories { mavenCentral() }

sourceCompatibility = 1.8targetCompatibility = 1.8

dependencies { compile("org.springframework.boot:spring-boot-starter-web" testCompile("junit:junit")}

task wrapper(type: Wrapper) { gradleVersion = '2.10'}

Example #3Gradle

$> gradle build$> java -jar build/libs/gs-rest-service-0.1.0.jar

# or local$> gradle wrapper$> gradlew build$> java -jar build/libs/gs-rest-service-0.1.0.jar

22 / 36

Page 23: Spring Boot and JHipster

Refs/Notes1. Getting Started · Building a RESTful Web Service2. spring-guides/gs-rest-service

23 / 36

Page 24: Spring Boot and JHipster

JHipsterJHipster Yeoman Generator

24 / 36

Page 25: Spring Boot and JHipster

$> yo jhipster Example #4Scaffolding via JHipster

npm install -g yonpm install -g bowernpm install -g grunt-cli

npm install -g generator-jhipster

25 / 36

Page 26: Spring Boot and JHipster

NotesFor Win7 x64 MachineOnly with Stock MSVS 2013 Community Version

npm install node-gyp -g

# perhaps unnecessarynpm config set msvs_version 2013 --globalnpm get msvs_versionnpm config list

set PYTHON=f:\prog\py27\python.exeset GYP_MSVS_VERSION=2013

nodevars

{ ... 'target_defaults': { ... 'configurations': { ... 'Release': { 'conditions': | |'target_arch=="x64"', { 'msvs_configuration_platform': 'x64', 'msbuild_toolset': 'v120_xp' }|, } } }}

~/.node-gyp/5.2.0/include/node/common.gypi

Ref: NPM native builds ... @ Stack Overflow

26 / 36

Page 27: Spring Boot and JHipster

Example #4Maven

# if something wrong happensnpm install && bower install

# equivalent for mvn spring-boot:run$> mvn

# localhost:8080

27 / 36

Page 28: Spring Boot and JHipster

Sign-In | Public

28 / 36

Page 29: Spring Boot and JHipster

Landing | User: admin

29 / 36

Page 30: Spring Boot and JHipster

User Management

30 / 36

Page 31: Spring Boot and JHipster

Metrics

31 / 36

Page 32: Spring Boot and JHipster

Configuration

32 / 36

Page 33: Spring Boot and JHipster

API

33 / 36

Page 34: Spring Boot and JHipster

Database (H2)

34 / 36

Page 35: Spring Boot and JHipster

References1. Spring Boot2. Spring Guides3. spring-boot/spring-boot-samples4. Getting Started · Building Java Projects with Maven5. Getting Started · Building Java Projects with Gradle6. The JHipster Mini-Book - infoq7. The JHipster Mini-Book - Site8. Starting a modern Java project with JHipster9. Getting Started with JHipster, AngularJS and Paymill

35 / 36

Page 36: Spring Boot and JHipster

ENDEueung Mulyana

http://eueung.github.io/java/springbootJava CodeLabs | Attribution-ShareAlike CC BY-SA

36 / 36