Springboot
Springboot
Example
@Controller
public class BooksController
{
@RequestMapping("/computer-science/books")
public String getAllBooks(Model model)
{
//application code
return "bookList";
}
@GetMapping: It maps the HTTP GET requests on the specific handler method. It is
used to create a web service endpoint that fetches It is used instead of using:
@RequestMapping(method = RequestMethod.GET)
@PostMapping: It maps the HTTP POST requests on the specific handler method. It is
used to create a web service endpoint that creates It is used instead of using:
@RequestMapping(method = RequestMethod.POST)
@PutMapping: It maps the HTTP PUT requests on the specific handler method. It is
used to create a web service endpoint that creates or updates It is used instead of
using: @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping: It maps the HTTP DELETE requests on the specific handler method. It
is used to create a web service endpoint that deletes a resource. It is used
instead of using: @RequestMapping(method = RequestMethod.DELETE)
@PatchMapping: It maps the HTTP PATCH requests on the specific handler method. It
is used instead of using: @RequestMapping(method = RequestMethod.PATCH)
@RequestBody: It is used to bind HTTP request with an object in a method parameter.
Internally it uses HTTP MessageConverters to convert the body of the request. When
we annotate a method parameter with @RequestBody, the Spring framework binds the
incoming HTTP request body to that parameter.
@ResponseBody: It binds the method return value to the response body. It tells the
Spring Boot Framework to serialize a return an object into JSON and XML format.
@PathVariable: It is used to extract the values from the URI. It is most suitable
for the RESTful web service, where the URL contains a path variable. We can define
multiple @PathVariable in a method.
@RequestParam: It is used to extract the query parameters form the URL. It is also
known as a query parameter. It is most suitable for web applications. It can
specify default values if the query parameter is not present in the URL.
@RequestHeader: It is used to get the details about the HTTP request headers. We
use this annotation as a method parameter. The optional elements of the annotation
are name, required, value, defaultValue. For each detail in the header, we should
specify separate annotations. We can use it multiple time in a method
@RestController: It can be considered as a combination of @Controller and
@ResponseBody annotations. The @RestController annotation is itself annotated with
the @ResponseBody annotation. It eliminates the need for annotating each method
with @ResponseBody.
@RequestAttribute: It binds a method parameter to request attribute. It provides
convenient access to the request attributes from a controller method. With the help
of @RequestAttribute annotation, we can access objects that are populated on the
server-side.
---------------
Certainly! I'll include the endpoint URLs in the comments for each example:
@GetMapping:
java
Copy code
@RestController
@RequestMapping("/books")
public class BookController {
java
Copy code
@RestController
@RequestMapping("/books")
public class BookController {
java
Copy code
@RestController
@RequestMapping("/books")
public class BookController {
java
Copy code
@RestController
@RequestMapping("/books")
public class BookController {
java
Copy code
@RestController
@RequestMapping("/books")
public class BookController {
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.BUILD-SNAPSHOT</version> <!-- lookup parent from repository -->
<relativePath/>
</parent>Note: In the above dependency, we have specified only the Spring Boot
version. If we want to add additional starters, simply remove the <version> tag.
Similarly, we can also override the individual dependency by overriding a property
in our project.For example, if we want to add another dependency with the same
artifact that we have injected already, inject that dependency again inside the
<properties> tag to override the previous one.
<dependencyManagement>
<dependencies>
<!--Override Spring Data release train-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Spring Boot provides another file to configure the properties is called yml file.
The Yaml file works because the Snake YAML jar is present in the classpath. Instead
of using the application.properties file, we can also use the application.yml file,
but the Yml file should be present in the classpath.
Example of application.yml
spring:
application:
name: demoApplication
server:
port: 8081
In the above example, we have configured the application name and port. The port
8081 denotes that the application runs on port 8081.
Core Properties
Cache Properties
Mail Properties
JSON Properties
Data Properties
Transaction Properties
Data Migration Properties
Integration Properties
Web Properties
Templating Properties
Server Properties
Security Properties
RSocket Properties
Actuator Properties
DevTools Properties
Testing Properties
Third-Party Starters
We can also include third party starters in our project. But we do not use spring-
boot-starter for including third party dependency. The spring-boot-starter is
reserved for official Spring Boot artifacts. The third-party starter starts with
the name of the project. For example, the third-party project name is abc, then the
dependency name will be abc-spring-boot-starter.
The Spring Boot Framework provides the following application starters under the
org.springframework.boot group.
Name Description
spring-boot-starter-thymeleaf It is used to build MVC web applications using
Thymeleaf views.
spring-boot-starter-data-couchbase It is used for the Couchbase document-oriented
database and Spring Data Couchbase.
spring-boot-starter-artemis It is used for JMS messaging using Apache Artemis.
spring-boot-starter-web-services It is used for Spring Web Services.
spring-boot-starter-mail It is used to support Java Mail and Spring
Framework's email sending.
spring-boot-starter-data-redis It is used for Redis key-value data store with
Spring Data Redis and the Jedis client.
spring-boot-starter-web It is used for building the web application, including
RESTful applications using Spring MVC. It uses Tomcat as the default embedded
container.
spring-boot-starter-data-gemfire It is used to GemFire distributed data store
and Spring Data GemFire.
spring-boot-starter-activemq It is used in JMS messaging using Apache ActiveMQ.
spring-boot-starter-data-elasticsearch It is used in Elasticsearch search and
analytics engine and Spring Data Elasticsearch.
spring-boot-starter-integration It is used for Spring Integration.
spring-boot-starter-test It is used to test Spring Boot applications with
libraries, including JUnit, Hamcrest, and Mockito.
spring-boot-starter-jdbc It is used for JDBC with the Tomcat JDBC connection
pool.
spring-boot-starter-mobile It is used for building web applications using Spring
Mobile.
spring-boot-starter-validation It is used for Java Bean Validation with
Hibernate Validator.
spring-boot-starter-hateoas It is used to build a hypermedia-based RESTful web
application with Spring MVC and Spring HATEOAS.
spring-boot-starter-jersey It is used to build RESTful web applications using
JAX-RS and Jersey. An alternative to spring-boot-starter-web.
spring-boot-starter-data-neo4j It is used for the Neo4j graph database and
Spring Data Neo4j.
spring-boot-starter-data-ldap It is used for Spring Data LDAP.
spring-boot-starter-websocket It is used for building the WebSocket applications.
It uses Spring Framework's WebSocket support.
spring-boot-starter-aop It is used for aspect-oriented programming with Spring AOP
and AspectJ.
spring-boot-starter-amqp It is used for Spring AMQP and Rabbit MQ.
spring-boot-starter-data-cassandra It is used for Cassandra distributed database
and Spring Data Cassandra.
spring-boot-starter-social-facebook It is used for Spring Social Facebook.
spring-boot-starter-jta-atomikos It is used for JTA transactions using Atomikos.
spring-boot-starter-security It is used for Spring Security.
spring-boot-starter-mustache It is used for building MVC web applications using
Mustache views.
spring-boot-starter-data-jpa It is used for Spring Data JPA with Hibernate.
spring-boot-starter It is used for core starter, including auto-configuration
support, logging, and YAML.
spring-boot-starter-groovy-templates It is used for building MVC web
applications using Groovy Template views.
spring-boot-starter-freemarker It is used for building MVC web applications
using FreeMarker views.
spring-boot-starter-batch It is used for Spring Batch.
spring-boot-starter-social-linkedin It is used for Spring Social LinkedIn.
spring-boot-starter-cache It is used for Spring Framework's caching support.
spring-boot-starter-data-solr It is used for the Apache Solr search platform with
Spring Data Solr.
spring-boot-starter-data-mongodb It is used for MongoDB document-oriented
database and Spring Data MongoDB.
spring-boot-starter-jooq It is used for jOOQ to access SQL databases. An
alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc.
spring-boot-starter-jta-narayana It is used for Spring Boot Narayana JTA
Starter.
spring-boot-starter-cloud-connectors It is used for Spring Cloud Connectors
that simplifies connecting to services in cloud platforms like Cloud Foundry and
Heroku.
spring-boot-starter-jta-bitronix It is used for JTA transactions using Bitronix.
spring-boot-starter-social-twitter It is used for Spring Social Twitter.
spring-boot-starter-data-rest It is used for exposing Spring Data repositories over
REST using Spring Data REST.
Spring Boot Production Starters
Name Description
spring-boot-starter-actuator It is used for Spring Boot's Actuator that provides
production-ready features to help you monitor and manage your application.
spring-boot-starter-remote-shell It is used for the CRaSH remote shell to
monitor and manage your application over SSH. Deprecated since 1.5.
Spring Boot Technical Starters
Name Description
spring-boot-starter-undertow It is used for Undertow as the embedded servlet
container. An alternative to spring-boot-starter-tomcat.
spring-boot-starter-jetty It is used for Jetty as the embedded servlet
container. An alternative to spring-boot-starter-tomcat.
spring-boot-starter-logging It is used for logging using Logback. Default logging
starter.
spring-boot-starter-tomcat It is used for Tomcat as the embedded servlet
container. Default servlet container starter used by spring-boot-starter-web.
spring-boot-starter-log4j2 It is used for Log4j2 for logging. An alternative to
spring-boot-starter-logging.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
Parent Poms allow us to manage the following things for multiple child projects and
modules:
Configuration: It allows us to maintain consistency of Java Version and other
related properties.
Dependency Management: It controls the versions of dependencies to avoid conflict.
Source encoding
Default Java Version
Resource filtering
It also controls the default plugin configuration.
The spring-boot-starter-parent inherits dependency management from spring-boot-
dependencies. We only need to specify the Spring Boot version number. If there is a
requirement of the additional starter, we can safely omit the version number.
<properties>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
The properties section defines the application default values. The default Java
version is 1.8. We can also override Java version by specifying a property
<java.version>1.8</java.version> in the project pom. The parent pom also contains
the few other settings related to encoding and source. The Spring Boot framework
uses these defaults in case, if we have not defined in the application.properties
file.
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
In the above code, we can see that we have used <scope> tag for this. It is useful
when we want to use different version for a certain dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
Starter of Spring web uses Spring MVC, REST and Tomcat as a default embedded
server. The single spring-boot-starter-web dependency transitively pulls in all
dependencies related to web development. It also reduces the build dependency
count. The spring-boot-starter-web transitively depends on the following:
org.springframework.boot:spring-boot-starter
org.springframework.boot:spring-boot-starter-tomcat
org.springframework.boot:spring-boot-starter-validation
com.fasterxml.jackson.core:jackson-databind
org.springframework:spring-web
org.springframework:spring-webmvc
By default, the spring-boot-starter-web contains the following tomcat server
dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>
The spring-boot-starter-web auto-configures the following things that are required
for the web development:
Dispatcher Servlet
Error Page
Web JARs for managing the static dependencies
Embedded servlet container
Spring Boot Embedded Web Server
Each Spring Boot application includes an embedded server. Embedded server is
embedded as a part of deployable application. The advantage of embedded server is,
we do not require pre-installed server in the environment. With Spring Boot,
default embedded server is Tomcat. Spring Boot also supports another two embedded
servers:
Jetty Server
Undertow Server
Using another embedded web server
For servlet stack applications, the spring-boot-starter-web includes Tomcat by
including spring-boot-starter-tomcat, but we can use spring-boot-starter-jetty or
spring-boot-starter-undertow instead.
Jetty Server
The Spring Boot also supports an embedded server called Jetty Server. It is an HTTP
server and Servlet container that has the capability of serving static and dynamic
content. It is used when machine to machine communication is required.
If we want to add the Jetty server in the application, we need to add the spring-
boot-starter-jetty dependency in our pom.xml file.
ADVERTISEMENT
Remember: While using Jetty server in the application, make sure that the default
Tomcat server is excluded from the spring-boot-starter-web. It avoids the conflict
between servers.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
We can also customize the behavior of the Jetty server by using the
application.properties file.
Undertow Server
Spring Boot provides another server called Undertow. It is also an embedded web
server like Jetty. It is written in Java and manage and sponsored by JBoss. The
main advantages of Undertow server are:
Supports HTTP/2
HTTP upgrade support
Websocket Support
Provides support for Servlet 4.0
Flexible
Embeddable
Remember: While using Undertow server in the application, make sure that the
default Tomcat server is excluded from the spring-boot-starter-web. It avoids the
conflict between servers.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
We can also customize the behavior of the Undertow server by using the
application.properties file.
spring-boot-starter
jackson
spring-core
spring-mvc
spring-boot-starter-tomcat
While the spring-boot-starter-tomcat contains everything related to Tomcat server.
core
el
logging
websocket
The starter-tomcat has the following dependencies:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
We can also use spring-mvc without using the embedded Tomcat server. If we want to
do so, we need to exclude the Tomcat server by using the <exclusion> tag, as shown
in the following code.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
*******
SB hello-world example
Spring AOP
AOP Throwing
SB JDBC Example
SB H2 Database
SB CRUD operations
SB Caching
SB Caching (cache abstraction,caching, why should we use the cache, what data
should be cached,
Run SB Application
SB Change Port
SB Rest Example
23) Creating Post Entity and Many to One Relationship with User Entity
************************************************************
https://javahelps.com/how-to-add-mysql-jdbc-driver-to-eclipse
https://ibytecode.com/blog/jdbc-mysql-connection-tutorial/
https://www.researchgate.net/publication/
324454006_Java_Database_Connectivity_Using_MySQL_A_Tutorial