Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Boot Introduction

Avatar for Coding Saint Coding Saint
September 28, 2020

Spring Boot Introduction

Avatar for Coding Saint

Coding Saint

September 28, 2020

More Decks by Coding Saint

Other Decks in Education

Transcript

  1. Overview Spring Boot • An easy , robust ,faster way

    to create production ready spring application • It is a very opinionated approach of Spring boot library and third party application with a motive of starting an application without much configuration.
  2. Primary Goals of Spring Boot • A faster way for

    developers to create spring application • Out of the box opinions to provide defaults for spring and third party configurations. • Providing range of non functional features like health checks, metrics, embedded servers, security etc • No code generation and no requirement of XML configuration
  3. Requirements and Prerequisites As an application Java Version 8 Maven

    3.2 + or Gradle 4 Servlet Container as Tomcat 8.5 /Jetty 9.4 /Undertow 1.3 While proceeding further we do expect that you have Java version 8, Maven 3.2 or Gradle As a developer Knowledge of Java Good to have prior knowledge of Spring Framework
  4. First Hello World !!! Handling a URL Url : http://localhost:8080/greetings

    http://localhost:8080/greetings @RestController public class UserController { @RequestMapping(path="greetings", method=RequestMethod.GET) protected String greetings() { return "Hello, World"; } } Hello ,World
  5. @RestController 1. Denotes that the class contains request mappings. 2.

    Automatically serialize and deserialize java object response to Json /XML or any format whatever is intended.
  6. Connecting to database Creating dataSource Just tell Spring boot app

    about which database to connect. # H2 spring.h2.console.enabled =true spring.h2.console.path =/h2 # Datasource spring.datasource.url =jdbc:h2:file:~/user spring.datasource.username =sa spring.datasource.password = spring.datasource.driver-class-name =org.h2.Driver application.properties
  7. HTTP POST • HTTP POST request : Used to create

    a resource on server. • Most of the time it’s like creating INSERT(s) at database. • POST should be used for non-idempotent request, which changes the state of server.
  8. HTTP POST • HTTP POST request : Used to create

    a resource on server. • Most of the time it’s like creating INSERT(s) at database. • POST should be used for non-idempotent request, which changes the state of server.
  9. HTTP POST Model public class User { private Long id;

    private String firstName; private String lastName; private String email; //getters and setters } Controller @RequestMapping(path={"/user"}, method=RequestMethod.POST) public ResponseEntity<Void> add(@RequestBody User user){ //logic } http://localhost:8080/user { "firstName":"Ray", "lastName":"McFallen", "email":"[email protected]" } Request Body in JSON
  10. HTTP GET HTTP GET request : Used to get the

    information already present on server/database. GET request doesn’t change state of server but only fetch the data. http://localhost:8080/user/1
  11. HTTP GET Model public class User { private Long id;

    private String firstName; private String lastName; private String email; //getters and setters } Controller @RequestMapping(path={"/user/{id}"}, method=RequestMethod.GET) public ResponseEntity<User> get(@PathVariable ("id") String id){ //logic } http://localhost:8080/user/1 { "id": 1, "firstName": "Ray", "lastName": "McFallen", "email": "[email protected]" } Response Body in JSON Response as User (JSON) with id
  12. HTTP PUT HTTP PUT request : Used to update an

    existing resource on server. PUT should be used for non-idempotent request, which changes the state of server.
  13. HTTP PUT Controller @RequestMapping(path={"/user"}, method=RequestMethod.PUT) public ResponseEntity<Void> update(@RequestBody User user){

    //logic } http://localhost:8080/user { "id": 1, "firstName": "Tim", "lastName": "McFallen", "email": "[email protected]" } Request Body in JSON
  14. HTTP DELETE HTTP DELETE request : Used to remove/purge an

    existing resource on server. DELETE should be used for non-idempotent request, which changes the state of server.
  15. User Information API - Introduction { "id": 1, "firstName": "Ray",

    "lastName": "McFallen", "email": "[email protected]" } public class User { private Long id; private String firstName; private String lastName; private String email; //getters and setters }
  16. HTTP Status Codes Status Code Range Description 1XX Informational 2XX

    Success 3XX Redirect 4XX Client error 5XX Server Error
  17. Famous HTTP Codes 200 OK 201 Created 204 No content

    301 Moved permanently 400 Bad Request 401 Unauthorized 405 Method not allowed 403 Forbidden access 415 Unsupported Media Type 500 Internal server error
  18. Famous HTTP Codes 200 OK 201 Created 204 No content

    301 Moved permanently 400 Bad Request 401 Unauthorized 405 Method not allowed 403 Forbidden access 415 Unsupported Media Type 500 Internal server error
  19. Message Converters MappingJackson2HttpMessageConverter – To convert JSON if Jackson2 is

    on classpath MappingJacksonHttpMessageConverter – To convert JSON if Jackson is on classpath Jaxb2RootElementHttpMessageConverter – To convert Java objects to/from XML if JAX2B is on classpath FormHttpMessageConverter: read and write the "application/x-www-form-urlencoded" media type as MultiValueMap<String, String> ByteArrayHttpMessageConverter : Converts ByteArray StringHttpMessageConverter : Converts String ResourceHttpMessageConverter : org.springframework.core.io.Resource for any type of octet stream SourceHttpMessageConverter: converts javax.xml.transform.Source AtomFeedHttpMessageConverter – To convert Atom feeds if Rome is on classpath RssChannelHttpMessageConverter – To convert RSS feeds if Rome is on classpath
  20. HATEOAS & Richardson maturity Model LEVEL 1 • Not at

    all RESTful. • No URL sanity • No method sanity 0
  21. HATEOAS & Richardson Maturity Model LEVEL 0 • Not at

    all RESTful. • No URL sanity • No method sanity • http://server:port/postCust omer • http://localhost:8080/delete Order LEVEL 1 • URL sanity. • Single HTTP methods for single resource • http://localhost:8080/order • http://localhost:8080/order/1 LEVEL 2 • URL sanity. • Multiple HTTP methods for single resource POST • http://localhost:8080/order GET : to retrieve DELETE : to purge • http://localhost:8080/order/1 LEVEL 3 • URL sanity. • Multiple HTTP methods for single resource • More information about possible next actions or other useful resources
  22. HATEOAS & Richardson maturity Model 1 Add pom dependency spring-boot-starter-hateoas

    2 Bean extends ResourceSupport org.springfram ew ork.hateoas.ResourceSupport 3 Change M ethod return type w ith Resource<T> and add Links org.springfram ew ork.hateoas.ResourceSupport
  23. Configurations and Properties 1. Devtools global settings properties on your

    home directory (~/.spring-boot-devtools.properties when devtools is active). 2. @TestPropertySource annotations on your tests. 3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application. 4. Command line arguments. 5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property). 6. ServletConfig init parameters. 7. ServletContext init parameters. 8. JNDI attributes from java:comp/env. 9. Java System properties (System.getProperties()). 10. OS environment variables. 11. A RandomValuePropertySource that has properties only in random.*. 12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants). 13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants). 14. Application properties outside of your packaged jar (application.properties and YAML variants). 15. Application properties packaged inside your jar (application.properties and YAML variants). 16. @PropertySource annotations on your @Configuration classes. 17. Default properties (specified by setting SpringApplication.setDefaultProperties).
  24. Type-safe Configuration properties 1. @Value can difficult to use when

    there are lot of values to be read. 2. It could be even tough if the values are hierarchical in nature. 3. Type Safe configuration using @ConfigurationProperties are amazing way to handle it.
  25. Profiles in Spring 1. Spring profiles helps in segregating part

    of the application configuration and make it available to certain environment. dev-db prod db User service
  26. 404 This status code was not found in previous slide.

    It means resource not found. URL is wrong
  27. This lecture is a part of Udemy course. You can

    find the discount coupon for full course in description
  28. Adding UI We will cover JSP support. This is not

    basically a REST api , but we will learn rendering HTML and a web app.