Advertisement
Advertisement

More Related Content

Advertisement

Java-Vienna: Spring Boot 1.x Overview/Intro by Dominik Dorn

  1. Spring Boot (1.x) Overview/Intro by Dominik Dorn @ Java-Vienna April Meetup #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn
  2. Dominik Dorn 4 Freelance Software Engineer 4 Leader of 4 Java-Vienna Meetup 4 PlayFramework.Wien Meetup #Java, #Scala, #Akka, #Play, #Cassandra, #Kafka,#PostgreSQL Reach out! Xing / Twitter / LinkedIn 2
  3. Overview 4 What's Spring-Boot 4 Getting started 4 REST-Endpoints / Testing 4 Day2day Work / Production Usage #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 3
  4. What is Spring-Boot? 4 Running an embedded Webserver ( WAR possible ) 4 Classpath Scanning / Reflection++ 4 Convention over Configuration 4 99.8% 08/15-Spring Programming 4 Security, Templating, Data-Access, etc. #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 4
  5. Maven <project ...> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> </parent> ... <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project> #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 5
  6. Application Entrypoint @Configuration @EnableAutoConfiguration @ComponentScan public class MyApp { public static void main(String[] args) throws Exception { ConfigurableApplicationContext app = SpringApplication .run(MyApp.class, args); } } #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 6
  7. Application Entrypoint @SpringBootApplication public class MyApp { public static void main(String[] args) throws Exception { ConfigurableApplicationContext app = SpringApplication .run(MyAppConfiguration.class, args); // -----------^^^^^^^^^^^^^^^^^<-- look here! } } #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 7
  8. First Rest Controller @RestController public class FrontpageController { @RequestMapping("/") public String index() { return "Hello Java Vienna!"; } } #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 8
  9. Path Parameters @RestController public class FrontpageController { @RequestMapping("/hello/{who}") public String hello(@PathVariable String who){ return "Hello " + who; } } #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 9
  10. Query Parameters @RestController public class FrontpageController { @RequestMapping("/greet/{who}") public String greet(@PathVariable String who, @RequestParam(name = "greeting", defaultValue = "Hello") String greeting) { return greeting + " " + who; } } #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 10
  11. #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 11
  12. Handling different clients @RequestMapping(value = "/", consumes = "application/json", produces = "application/json" ) public Map indexJson( @RequestParam( value = "who", defaultValue = "world") String who) { Map<String, String> m = new HashMap<>(); m.put("greeting", "hello"); m.put("who", who); return m; } #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 12
  13. #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 13
  14. Testing #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 14
  15. Maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 15
  16. @RunWith(SpringRunner.class) @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyApp.class) public class FrontpageControllerTest { @Autowired TestRestTemplate rest; @Test public void helloJavaVienna() throws Exception { String result = rest.getForObject("/hello/java-vienna", String.class); Assert.assertEquals("Hello java-vienna", result); } } #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 16
  17. @RunWith(SpringRunner.class) @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyApp.class) @AutoConfigureMockMvc public class MVCFrontpageControllerTest { @Autowired MockMvc mvc; @Test public void helloJavaVienna() throws Exception { mvc.perform(MockMvcRequestBuilders .get("/hello/{who}", "java-vienna")) .andExpect(status().is2xxSuccessful()) .andExpect(content().string("Hello java-vienna")); } } #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 17
  18. Dev-Workflow #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 18
  19. JRebel 4 Pro: Fastest turnaround ( ~ 1-3 seconds ) 4 Contra: 550 USD / year #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 19
  20. Spring Dev-Tools 4 Pro: Free, includes LiveReload (for browser reload) 4 Contra: Restarts whole application every time classpath changes ( 2 - 30+ seconds ) <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 20
  21. Deploying #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 21
  22. <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.13.BUILD-SNAPSHOT</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 22
  23. java -jar target/myApp.jar #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 23
  24. #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 24
  25. <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <imageName>domdorn/${project.artifactId}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 25
  26. Prod-Monitoring: Actuator <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 26
  27. Endpoint Description auditevents Exposes audit events information for the current application. beans Displays a complete list of all the Spring beans in your application. configprops Displays a collated list of all @ConfigurationProperties env Exposes properties from Spring’s ConfigurableEnvironment. #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 27
  28. Endpoint Description flyway / liquibase Shows any Flyway/Liquibase database migrations that have been applied. health Shows application health information. httptrace Displays the last 100 HTTP req./resp. exchanges loggers Show application loggers and reconfigure them #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 28
  29. Endpoint Description metrics Shows ‘metrics’ information for the current application. mappings Displays a collated list of all @RequestMapping paths. scheduledtasks Displays the scheduled tasks in your application. threaddump / heapdump Performs a thread dump / heap dump (gzipped). #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 29
  30. THANK YOU! Blog https://dominikdorn.com Twitter @domdorn Xing https://www.xing.com/profile/Dominik_Dorn LinkedIn https://www.linkedin.com/in/dominik-dorn Java-Vienna Meetup | PlayFramework.Wien Meetup #Java #Vienna 16 April 2018 | Spring Boot Overview/Intro | Dominik Dorn @domdorn 31
Advertisement