@value
annotation is used to gain access to the properties. So, the most possible sources of external configuration are as follows:spring-boot-starter-*
, where *
is a particular application. They intend this naming structure to help when you need to find a starter. Third party starters should not start with spring-boot, as they reserve it for official Spring Boot artifactsspring-boot-starter-web
: Web and RESTful applicationsspring-boot-starter-security
: Spring Securityspring-boot-starter-data-jpa
: Spring Data JPAspring-boot-starter-test
: Unit testingspring-boot-starter-hateoas
: Add HATEOAS featuresspring-boot-starter-data-jpa
: Spring Data JPA with HibernateHSQLDB
in out class-path, it will automatically configure an in-memory database for us. Think of the auto-configuration as an intelligent system which can provide ready to use the application to us based on the configured jars in our class-path. META-INF/spring.factories
file within your published jar for the auto-configuration classes. To register our custom auto-configuration class, we should use a fully qualified name of the class under the EnableAutoConfiguration
in the spring.factories
property file.org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.javadevjournal.config.HelloServiceAutoConfiguration
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
@SpringBootApplication
annotation is equivalent to using @Configuratio
n, @EnableAutoConfiguration
, and @ComponentScan
with their default attributes. Spring Boot enables the developer to use a single annotation instead of using multiple. But, as we know, Spring provided loosely coupled features that we can use for each annotation as per our project needs. @ComponentScan
annotation for your class file to scan your components added to your project. <dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
pom.xml file
. Like you can use spring-boot-starter-jetty
as a dependency for using a jetty server in your project. @EnableAutoConfiguration
if you want auto-configuration not to apply to any specific class.//use of exclude
@EnableAutoConfiguration(exclude={className})
@Controller
Map of the model object to view or template and make it human readable but @RestController
simply returns the object and object data is directly written in HTTP
response as JSON
or XML
. spring-boot-starter-actuator
” in pom.xml
.<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-actuator </artifactId>
</dependency>
logging.level.root
=debug property in application.property file
. HTML5
Java web applications. To use Thymeleaf, you need to add the following code in the pom.xml file
:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
“server.port”
and assign it to any port you wish to. For example, if you want to assign it to 8081
, then you have to mention server.port=8081
. Once you mention the port number, the application properties file will be automatically loaded by Spring Boot and the required configurations will be applied on to the application. pom.xml file
:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
war
file in spring boot you need to define your packaging file as war in your pom.xml
(if it is maven project)..war
file generated for your application.server.http2.enabled configuration property.
spring-boot-starter-jetty
or as a dependency for according you to your need. spring-boot-devtools
module that includes LiveReload
server to trigger a browser refresh when a resource is changed. LiveReload server extensions are available freeware for Firefox, Chrome, and Safari. YamlMapFactoryBean
to load YAML as a MapYamlPropertiesFactoryBean
to load YAML as Properties@RepositoryRestResource(collectionResourceRel = "sample", path = "sample")
public interface SampleRepository
extends CustomerRepository<sample, Long> {
{
"customername": "Venkat"
}
{
"customername": "Venkat"
"_links": {
"self": {
"href": "http://localhost:8080/sample/1"
},
"sample": {
"href": "http://localhost:8080/sample/1"
}
}
@GetMapping
is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET)
. Both these methods support the consumes. The consume options are :consumes = “text/plain”
consumes = {“text/plain”, “application/*”}
H2
is not present in the classpath, then you see the following error :Cannot determine embedded database driver class for database type NONE
H2
to the pom.xml
file, and restart your server.<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
@RestController
@RequestMapping("/freetimelearning")
public class Controller {
@Autowired
private final FreeTimeLearningRepository freetimelearningRepository;
public Controller(FreeTimeLearningRepository freetimelearningRepository) {
}
@RequestMapping(method = RequestMethod.POST)
ResponseEntity<?> insert(@RequestBody Course course) {
greatLearningRepository.save(course);
return ResponseEntity.accepted().build();
}
}
server.port=8084
(your port number) property in your application.properties
file and start your application.ErrorController
interface which is provided by SpringFramework
and then overriding its getErrorPath()
so that you can return a custom path whenever such type of error is occurred.error.html
page and place it into the src/main/resources/templates path
. The BasicErrorController
of of springboot will automatically pick this file by default.server.error.whitelabel.enabled
property to false in the application.properties
file to disable the whitelabel error page.@ControllerAdvice
annotation to handle your exceptions globally.@ExceptionHandler
annotation.@Transactional annotation
.spring-boot-starter-cache
”, adding this to an application brings in all the dependencies to enable JSR-107 (JCACHE - Java Temporary Caching API)
and Spring caching annotations.@EnableCaching
to the required configuration class. This will automatically configure a suitable CacheManager to serve as a provider for the cache.@Configuration
@EnableCaching
public class CachingConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("addresses");
}
}
@Cacheable("addresses")
public String getAddress(Customer customer) {...}
rel
”: Relation with the target resource.href
”: URI for resource{
"contractId": 10067,
"description": "Contract details for the requested orderId",
"status": "created",
"links": [
{
"rel": "self",
"href": "http://demoApplication.com/contracts/10067"}]
}
consumer
" is the user of an API
.ReadOnly
as true in Transaction Management is :STUDENT
database and you have to read the student details such as studentID
, and studentName.log4j2
for logging configuration. what all you have to do is you have to exclude log backfile
and include log4j2
for logging. You can do it by using the spring starter projects.save()
method in JPA, you can directly insert your data into your database.@RestController
@RequestMapping("/greatleasrning")
public class Controller {
@Autowired
private final FreeTimeLearningRepository freetimeLearningRepository;
public Controller(GreatLearningRepository freetimeLearningRepository) {
this. freetimelearningRepository = freetimeLearningRepository;
}
@RequestMapping(method = RequestMethod.POST)
ResponseEntity<?> insert(@RequestBody Course course) {
freetimeLearningRepository.save(course);
return ResponseEntity.accepted().build();
}
}
package com.freetimelearning;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource
public interface GreatLearning extends CrudRepository<Candidate, Long>
{
public List<Candidate> findById(long id);
//@Query("select s from Candidate s where s.age <= ?")
public List<Candidate> findByAgeLessThanEqual (long age);
}
boot:run
runs your Spring Boot application.spring-boot:repackage
repackages your jar/war to be executable.spring-boot:start
and spring-boot:stop
to manage the lifecycle of your Spring Boot application (i.e. for integration tests).spring-boot:build-info
generates build information that can be used by the Actuator. pom.xml
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.5.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
server.ssl.*
properties in the application.properties or yml file to configure and enable SSL
for your Spring Boot application. Here are typical SSL configurations for your application.server.port=8443 //SSL port
server.ssl.key-store=classpath:keystore.jks //You can also configure it to external location
server.ssl.key-store-password= //password for your key
server.ssl.key-password=//key password