Session 24. Spring Framework Introduction. Reading & Reference. dev.to/lechatthecat/how-to-use-spring-boot-java-web-framework-withintellij-idea-202p

Similar documents
Session 25. Spring Controller. Reading & Reference

Exercise for OAuth2 security. Andreas Falk

MEAP Edition Manning Early Access Program Spring in Action

Spring Data JPA, QueryDSL 실습 JPAQueryFactory 이용

Spring Boot Reference Guide

Set up Maven plugins in Eclipse. Creating a new project

Spring Professional v5.0 Exam

IN ACTION. Craig Walls SAMPLE CHAPTER. FOREWORD BY Andrew Glover MANNING

Creating RESTful web services with Spring Boot

4. Check the site specified in previous step to work with, expand Maven osgi-bundles, and select slf4j.api,

Index. Combined lifecycle strategy, annotation, 93 ContentNegotiatingViewResolver, 78

FreeMarker in Spring Web. Marin Kalapać

Setting up a Maven Project

Construction: version control and system building

Spring Web Services Tutorial With Example In

Introduction to Spring 5, Spring MVC and Spring REST

Maven. INF5750/ Lecture 2 (Part II)

Package Management and Build Tools

What is Maven? Apache Maven is a software project management and comprehension tool (build, test, packaging, reporting, site, deploy).

Tools for Accessing REST APIs

Spring Statemachine - Reference Documentation

DevOps and Maven. Eamonn de Leastar Dr. Siobhán Drohan Produced by:

Simplified Build Management with Maven

Application Design and Development: October 30

HP Operations Orchestration

Spring Statemachine - Reference Documentation

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

Produced by. Agile Software Development. Eamonn de Leastar

HP Operations Orchestration

Integrating Spring Boot with MySQL

com Spring + Spring-MVC + Spring-Boot + Design Pattern + XML + JMS Hibernate + Struts + Web Services = 8000/-

Action Developers Guide

JAVA V Tools in JDK Java, winter semester ,2017 1

Introduction to Spring Framework: Hibernate, Spring MVC & REST

gradle : Building Android Apps Mobel Meetup

Creating Custom Builder Components

... Fisheye Crucible Bamboo

Maven POM project modelversion groupid artifactid packaging version name

JVA-117E. Developing RESTful Services with Spring

Administering Apache Geronimo With Custom Server Assemblies and Maven. David Jencks

Remote controlling Parrot AR Drone with Spring Boot & Vaadin. Peter expert & trainer

Demystifying Spring Boot Magic. # K100 on slido.co. Patrick DevDays Vilnius 2018 Socialiniu mokslu kolegija /

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

JAVA MICROSERVICES. Java Language Environment. Java Set Up. Java Fundamentals. Packages. Operations

Apache Isis Maven plugin

Skyway Builder 6.3 Reference

Example ear-testing can be browsed at

Session 24. Introduction to Java Server Faces (JSF) Robert Kelly, Reading.

Topics covered. Introduction to Maven Maven for Dependency Management Maven Lifecycles and Plugins Hands on session. Maven 2

JVA-117A. Spring-MVC Web Applications

Component based Development. Table of Contents. Notes. Notes. Notes. Web Application Development. Zsolt Tóth

OAuth2 Autoconfig. Copyright

Upgrading to Spring Boot 2.0

Maven 2 & Continuum. by Trygve Laugstøl

Apache Maven. Created by anova r&d bvba

CONFIGURING A SPRING DEVELOPMENT ENVIRONMENT

Introduction to Spring Framework: Hibernate, Web MVC & REST

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

Java Advance Frameworks

web.xml Deployment Descriptor Elements

Spark Tutorial. General Instructions

AUTOMATION TESTING FRAMEWORK FOR LUMINOUS LMS

CS506 Web Design & Development Final Term Solved MCQs with Reference

JAVA SYLLABUS FOR 6 MONTHS

Content. Development Tools 2(57)

Desarrollo de Aplicaciones Web Empresariales con Spring 4

Spring boot multipart response

Distributed Multitiered Application

EPL451: Data Mining on the Web Lab 6

Sonatype CLM - IDE User Guide. Sonatype CLM - IDE User Guide

Overview of Web Application Development

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

Red Hat JBoss Enterprise Application Platform 7.1

Demystifying Spring Boot Magic. Patrick DevOps Pro Moscow 2018

Struts: Struts 1.x. Introduction. Enterprise Application

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

Specialized - Mastering JEE 7 Web Application Development

MAVEN MOCK TEST MAVEN MOCK TEST I

Red Hat Fuse 7.1 Deploying into Spring Boot

MAVEN MOCK TEST MAVEN MOCK TEST IV

Call: Core&Advanced Java Springframeworks Course Content:35-40hours Course Outline

Spring-util-3.0.xsd' Must Have Even Number Of Uri's

Spring-beans-2.5.xsd' Must Have Even Number Of Uri's

... Maven.... The Apache Maven Project

Session 9. Introduction to Servlets. Lecture Objectives

COURSE DETAILS: CORE AND ADVANCE JAVA Core Java

J2EE - Version: 25. Developing Enterprise Applications with J2EE Enterprise Technologies

Software Building (Sestavování aplikací)

Developing Spring based WebSphere Portal application using IBM Rational Application Developer

Cheat Sheet: Wildfly Swarm

~$> whoami. Ioan Marius Curelariu SDE at Website Analytics Amazon Development Center Romania

Microservices mit Java, Spring Boot & Spring Cloud. Eberhard Wolff

Chapter 1: First steps with JAX-WS Web Services

In this tutorial, we will understand how to use the OpenNLP library to build an efficient text processing service.

Developing Applications with Java EE 6 on WebLogic Server 12c

Reactive Streams in the Web. Florian Stefan ebay Classifieds Group GOTO Berlin 2017

Deployment to the Cloud

Spring Web Flow Reference Guide

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Transcription:

Session 24 Spring Framework Introduction 1 Reading & Reference Reading dev.to/lechatthecat/how-to-use-spring-boot-java-web-framework-withintellij-idea-202p http://engineering.pivotal.io/post/must-know-spring-boot-annotationscontrollers/ http://engineering.pivotal.io/post/spring-for-normal-people/ Reference Spring home page spring.io/ Spring 4.3 Reference Documentation docs.spring.io/autorepo/docs/spring/4.3.0.release/spring-frameworkreference/pdf/spring-framework-reference.pdf 2 11/25/2018 1

Lecture Objectives Understand the structure of the Spring controller Become familiar with Spring controller annotation 3 IDE Support Thymeleaf integrates well with Spring We defer Thymeleaf until after we cover Spring Controller Thymeleaf does not integrate well with NetBeans, so examples use Intellij If you do not use Intellij, you can download from www.jetbrains.com/idea/download/ 4 11/25/2018 2

Interesting Quote Like many of my friends and fellow engineers, I have recently started working on some Spring projects, specifically using Spring Boot. It has been several years since I have worked with Spring, and some things were immediately apparent: I had no idea how to do things The Spring Way. There is a ton of documentation. Almost all the documentation assumes you already know everything about Spring in order to understand it. Source: Ian Fisher: Pivotal Engineering Post 5 What is Tedious About Using Servlets? One servlet for each form to be processed Servlet controller code that either updates a bean or forwards to a view (or both) Bean actions Storing data Setting up visibility Etc. A good framework can take some of the tedium out of development 6 11/25/2018 3

Spring Overview Open source, easy to use MVC Framework (tightly coupled to Servlet API) Integrates well with other tools Enables development of Java EE systems Initially developed in 2003 Currently well supported and expanding rapidly 7 Spring Versions Version 3.x mapping are provided in xml files (similar to mappings found in web.xml and tld files Version 4.x (4.3 is final version in this series) mappings use annotations Version 5.x 8 11/25/2018 4

Inversion of Control Design principle Custom written portions of a computer program receive the flow of control from a generic framework Inversion highlights the contrast with use of reusable libraries (e.g., APIs) that are called from a custom written control module Similar to event-driven programming 9 Spring Architecture Modular You can select which modules to use Modules Data access (e.g., JDBC, object-relational mapping, JPA, etc.) Web Core Messaging Test More 10 11/25/2018 5

Spring Boot Pre-configured solution Requires you to provide exceptions rather than a complete configuration Integrates with Maven Features Embedded application server No code generation No requirement for XML configuration files Spring initializer that will configure, download, and install your project files https://start.spring.io/ 11 Maven Build automation tool Collects all jars on classpath and builds a single runnable jar Apache Software Foundation XML file describes How software is built Dependencies on external modules Build order Required plug-ins Directories Projects configured using a Project Object Model (pom.xml) Sample pom.xml file <project> <modelversion>4.0.0</modelversion> <groupid>com.mycompany.app</groupid> <artifactid>my-app</artifactid> <version>1.0</version> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> 12 11/25/2018 6

Mapping Approach Relationship between forms, servlets, views, and beans are specified in mapping files, not in the code Recall mapping done in web.xml and tld files Spring versions Version 3.x mapping are provided in xml files (similar to mappings found in web.xml and tld files Version 4.x mappings use annotations 13 Standard Servlet Control Flow User agent Web Server servlet servlet servlet Your browser invokes a servlet (specified in URL), which then controls the server response Thymeleaf template Thymeleaf template JSP 14 11/25/2018 7

Servlet Mapping User agent Web Server servlet Spring allows you to have a single controller receiving all requests Spring allows you to specify a server method for each request Thymeleaf template HTML Page JSP 15 Spring MVC User agent Web Server Controller Think of the interaction as a user specified Controller object setting the rules for forwarding requests Thymeleaf template Other view JSP 16 11/25/2018 8

Step-by-step instructions in Your First Spring Boot dev.to/lechatthecat/how-to-use-spring-boot-java-web-framework-with-intellij-idea-202p We show the code, then look at how it works After we build the application, and enter localhost:8080 in the browser, this page appears 17 Intellij project Standard structure generated by Maven Contains Java code Templates (e.g., Thymeleaf) Java tests Project Object Model Example 18 11/25/2018 9

Example - Project Object Model <?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 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>demo</artifactid> <version>1.0-snapshot</version> An artifact is a file (e.g., JAR) that gets deployed to a Maven repository 19 Example - Project Object Model <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.2.release</version> </parent> pom.xml inherits from a super-pom <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> </dependencies> Maven automatically downloads dependencies 20 11/25/2018 10

Example - Project Object Model <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> 21 Annotation Recap Annotations can be defined to have elements Elements appear in parentheses following the annotation @RequestMapping(method = RequestMethod.GET) Elements are a collection of comma-separated, name-value pairs If the element name is missing, value is the default element name @RequestMapping( /welcome ) Is the same as @RequestMapping(value = /welcome ) 22 11/25/2018 11

Spring Boot Application Runs a SpringApplication Specifies the main class @SpringBootApplication equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.spri ngbootapplication; @SpringBootApplication public class DemoApp { public static void main(string[] args) { SpringApplication.run(DemoApp.class, args); } } 23 What Happens When Application Runs? Configuration resolves Application server (e.g., Tomcat) runs Ready to accept requests to the server port (according to controller rules) 24 11/25/2018 12

Controller annotation (@Controller) indicates that the annotated class is a Web controller Controller class is autodetected Typically use in combination with annotated handler methods based on RequestMapping annotation Example - Controller package com.example.demo.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; @Controller public class HelloController { @GetMapping public String gethello() { return "hello"; } } Shortcut for @RequestMapping with a GET parameter 25 What Happens if We Change the Controller Text (not html) is sent to browser package com.example.demo.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.*; @RestController public class HelloController { @GetMapping public String gethello() { return "hello"; } } 26 11/25/2018 13

Controller Types Each controller contains annotation describing what type of controller it is @Controller often used to serve Web pages Controller methods will return a String that indicates which template to render or which route to redirect to @RestController Serves text (JSON, XML, etc.) Controller methods return an object that will be serialized to the specified format 27 Track Initializer To build a Spring project in Intellij, you can use Spring Initializr Set Web dependencies 28 11/25/2018 14

Are We on Track? Build your first Spring application by following the steps in dev.to/lechatthecat/how-to-use-spring-boot-java-web-framework-with-intellij-idea-202p Or by using Spring Initializr Run the project with a @RestController and @Controller 29 11/25/2018 15