36-Spring Web with Spring Boot Flashcards

1
Q

Types of Spring MVC applications

A
  1. Web Servlet

2. Web Reactive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the starter for Spring MVC?

A

spring-boot-starter-web

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What jar files that spring-boot-starter-web includes?

A
spring-web.jar
spring-webmvc.jar
spring-boot-starter.jar
jackson*.jar
tomcat-embed.jar
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Does the web starter depend on the validation starter?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List out Spring MVC components created at startup time

A
  1. DispatcherServlet
  2. HandlerAdapter
  3. MessageConverter
  4. ContentTypeManager
  5. HandlerMapper
  6. ViewResolver, LocalResolver, ExceptionResolver
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the processing lifecycle of a REST request?

A

client -> DispatcherServlet -> Controller -> MessageConverter -> DispatcherServlet -> client

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are message-converters in Spring MVC?

A

If found on the classpath, Spring will use:

  1. Jackson for JSON/XML *
  2. JAXB for XML*
  3. GSON for JSON*
  4. Byte[], String, BufferredImage
  5. Form-based data
  6. Protobuf*
  7. Feed data such as ATOM/RSS*
  8. JAXP Source

Note:

    • Requires 3rd party libs on the classpath
  1. Protobuf is not setup automatically
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When need to use @ResponseBody?

A
In the controller class with @Controller, the method needs to mark with @ResponseBody to convert the return object to JSON before sending it to the client.
We don't need this in @RestController controller class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can Spring lookup for @PathVariable, @RequestParam parameters without a name? e.g. @PathVariable long accountId

A
  1. To do this the compiled class needs extra metadata – the names of the method parameters by using flag -parameters
  2. Spring will lookup using parameter names:
    a. @PathVariable(“accountId”) long accountId -> @PathVariable long accountId
    b. @RequestParam(“overdrawn”) boolean overdrawn -> @RequestParam boolean overdrawn
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to set a request parameter optional?

A

@RequestParam(required=false) Integer location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to enable message converters?

A
  1. Auto-configure if it’s Spring Boot app
  2. If not, need to
    @Configuration @EnableWebMvc class Config {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to customize message-converters?

A
  1. Override WebMvcConfigurer.configureMessageConverters, annotate with @Configuration
  2. Mark this class with @Configuration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to replace embedded tomcat with jetty?

A

In pom.xml,

  1. exclude sprint-boot-starter-tomcat from spring-boot-starter-web
  2. add dependency spring-boo-starter-jetty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to configure to run the app from both the command line and web container?

A
  1. Extends SpringBootServletInitializer
  2. Implement SpringBootServletInitializer.configure(SpringApplicationBuilder)
    add: builder.sources(Application.class)
  3. Add normal main class (to run from the command line)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does mvn package produce?

A
  1. app-0.0.1.jar: fat & executable jar

2. app-0.0.1.jar.original: traditional jar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the starter for spring mvc and web?

A

spring-boot-starter-web

17
Q

What plugin builds fat & executable jar?

A

spring-boot-maven-plugin

18
Q

What is the purpose of @EnableWebMvc?

A

Without Spring Boot, we need to use @EnableWebMvc to:

  1. Auto-configure controllers, views
  2. Auto-configure rest support, message converters
  3. Stateless converters
19
Q

What if you add @EnableWebMvc to a Spring Boot app?

A

All extra configs from Spring Boot will disappear

20
Q

List out template engines supported by Spring MVC

A
  1. Mustache
  2. Thymeleaf
  3. GSP
  4. Freemarker
  5. Velocity
    and more
21
Q

What is the front controller in MVC-based applications?

A

DispatcherServlet