¡GoConqr estrena diseño! Cuéntanos qué te parece.

Fichas

Spring Boot

Jerry Reeves Jr Information Technology Inglés 17 fichas

Fichas del conjunto (17)

  1. 1How do you decide whether to choose either Spring or Spring Boot framework for the application?It’s simple, any console/desktop or web application that wants to leverage DI & other key features provided by Core Spring modules can use the Spring framework.
    Spring Boot framework was evolved on top of Spring framework to support microservices-based architecture on the cloud.
    Spring Boot framework is popularly used to develop Web/Enterprise Applications, RESTful API & Microservices.
  2. 2Why do we need a Service layer? Can’t we call the Repository layer directly inside the Controller layer?There is no restriction from compiler, and we can call the Repository layer from the Controller layer directly.
    The layering helps to segregate the Spring application responsibilities and enables loose coupling between the objects.
    Service layer in the application facilitates communication between the Controller and the Persistence/Repository/DAO layer.
    Service layer usually holds business logic e.g. It may include validation logic.
    One Service layer may depend on multiple Repository layers to serve a specific business purpose.
    The method names in Service layer are usually named as per the business purpose they serve whereas method names in Repository are named as standard CRUD operations supported by that Repository.
    Hence, we need Service layer between DAO and Controller layer.
  3. 3Is it safe to keep the DB & other critical passwords directly inside property file(s)?Since property file(s) are readable by anyone who has access to the codebase it is highly recommended not to keep the DB and other critical credentials inside property file(s).
    They are usually stored inside cloud environment variables or passed through command line arguments while executing the application.
  4. 4As a developer you need to check your production application status daily, how does Spring Boot could help here?One of the routine tasks for a developer is to check the status of the already launched production application periodically.
    To simplify this task, Spring Boot provides a sub-project named Spring Boot Actuator.
    Spring Actuator exposes operational information about the executing application, including, metrics, info, dump, env, etc.
    The information is accessed usually via HTTP endpoints, a few of which are listed below:
    /health summarizes the health status of our application.
    /beans returns all available beans in our BeanFactory.
    `/envy returns the current environment properties.
    To enable Spring Boot Actuator, we just need to add the spring-boot-actuator dependency to our maven package manager.
    To access the actuator endpoints using HTTP, we need to both enable and expose them.
    Only the /health and /info endpoints are exposed by default.
    We need to add the following configuration to expose all endpoints: management.endpoints.web.exposure.include=*
  5. 5What is Spring profile and why do we use it?Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments.
    Any @Component, @Configuration or @ConfigurationProperties can be marked with @Profile to limit when it is loaded.
    In the normal Spring way, you can use a spring.profiles.active environment property to specify which profiles are active.
    You can specify the property in any of the usual ways, for example, you could include it in your application.properties: spring.profiles.active=test or specify on the command line using the switch --spring.profiles.active=prod.
  6. 6How can you define multiple profiles in the Spring Boot application? How to add an active profile?The application development process undergoes different stages; the typical ones are development, testing, and production.
    Spring Boot profiles help group parts of the application configuration and make it available only in certain environments.
    A profile is a set of configuration settings.
    Spring Boot allows to definition profile of -specific property files in the form of application-{profile}.properties.
    We can define both profile-specific and default application.properties file in the application.
    The local/default profile (application.properties) is usually called default; all the beans that do not have a profile set belong to the default profile.
    Spring automatically loads the properties in an application.properties file for all profiles and the ones in profile-specific property files only for the specified profile.
    There are plenty of ways of defining active profiles in Spring Boot, including command line arguments, Maven settings, JVM system parameters, environment variables, spring.profiles.active property, and SpringApplication methods.
  7. 7What happens when two profiles are set for property spring.profiles.active in the Spring application?If several profiles are specified, a last-wins strategy applies.
    For example, if profiles prod,live are specified by the spring.profiles.active property (i.e. spring.profiles.active=prod,live), values in application-prod.properties can be overridden by those in application-live.properties.
  8. 8Which pom.xml file inside the Spring Boot framework defines versions of all pre-configured dependencies?Spring Boot framework has utilized the concept of parent and child pom file inheritance and defined all the dependencies specific to its release version under spring-boot-dependencies module.
  9. 9What is Bean Validation API? How can we use it in Spring?To restrict the input provided by the user in Spring MVC applications, the Spring >=4 supports and uses Bean Validation API.
    The Bean Validation API is a Java specification which is used to apply constraints to the fields of a class via annotations.
    Using Bean Validation API we can validate a length, number, regular expression, etc. as well, and build custom validations.
    Hibernate Validator is the most famous/used implementation of the Bean Validation API specification.
    There are 3 types of variables that can be validated using Bean Validation API:
    The request body,
    Variables within the path (e.g., id in /api/{id}) and,
    Query parameters.
    Spring Boot comes with the validation starter, which needs to be included in pom.xml file and the intern added the hibernate-validator dependency
  10. 10Explain a few hibernate-validator Spring bean validation annotations?Few common validation annotations are listed below:
    @NotNull: Field must not be null.
    @NotEmpty: List field must not be empty.
    @NotBlank: String field must not be the empty String (i.e., it must have at least one Character).
    @Min and @Max: Numerical field is only valid when its value is above or below a certain value.
    @Pattern: String field is only valid when it matches a certain regular expression.
    @Email: String field must be a valid email address.
  11. 11What does the @Valid annotation indicate in Spring?The @Valid annotation can be added to variables in a RestController mapping method to validate them.
    In the below code our POST request takes in a request body, and we're mapping that request body to a class InputForm.
    The @Valid annotation will tell Spring to go and validate the data passed into the controller i.e., age is between 18 and 60 inclusive because of those Bean Validation API annotations (min and max).
  12. 12What is the use of @ControllerAdvice in the Spring application?@ControllerAdvice is a specialization of the @Component annotation which allows handling exceptions across the whole application in one global handling component.
    It intercepts exceptions thrown by methods annotated with @RequestMapping and similar.
    All you need to have is a class annotated with @ControllerAdvice.
    If any exception is raised in the defined controller [you can define to which packages this controller advice should listen for exception in base packages] then it is handled by ControllerAdvice.
  13. 13What is the use of @ExceptionHandler in the Spring application?The @ExceptionHandler is an annotation used to handle a specific exception in the controller and send a custom response to the client.
    We need to have a method annotated with @ExceptionHandler which takes Exception Class (any exception that you want to handle) as an argument, if any of these exceptions are raised in the controller, then this handler method will handle it.
    If we have two handler methods in the same controller say for example one handler for Exception and another handler for RuntimeException, then the handler method which is closer to the Exception Class hierarchy is triggered.
  14. 14Have you used the HAL browser in Spring? How to use it?HAL stands for Hypertext Application Language.
    HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API.
    Adopting HAL make API explorable, and its documentation easily discoverable from within the API itself.
    In short, it makes API easier to work with and therefore more attractive to client developers.
    The HAL browser provides an in-browser GUI to traverse your Spring RESTful API.
    Below is the single dependency needed to integrate the HAL browser into our REST API.
    If we have the above dependency, Spring will auto-configure the HAL browser, and make it available via the default endpoint.
    All we need to do now is press run and switch to the browser. The HAL browser will then be available on http://localhost:8080/
  15. 15What is the use of the ResponseEntity class in Spring?ResponseEntity represents an HTTP response, including headers, body, and status.
    While @ResponseBody puts the return value into the body of the response, ResponseEntity also allows us to add headers and HTTP Status codes.
    It can be used in both @RestController and @Controller.
  16. 16What is Spring Boot?Spring Boot is a microservice-based framework and making a production-ready application in it takes very less time.
  17. 17What is the use of @RestController?@RestController is a convenience annotation for creating Restful controllers. It is a specialization of @Component and is autodetected through classpath scanning. It adds the @Controller and @ResponseBody annotations. It converts the response to JSON or XML.

Información

Descripción
Spring Boot Interview Questions
Creado por
Jerry Reeves Jr
Visitas
3
Valoración

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?