Commit c38426db authored by Bhargavi Ghanta's avatar Bhargavi Ghanta

Sprint3Day2 Assignment Completed

parent 55ca39bc
...@@ -972,7 +972,7 @@ A.17) Spring Boot Actuator is a powerful tool that provides production-ready fea ...@@ -972,7 +972,7 @@ A.17) Spring Boot Actuator is a powerful tool that provides production-ready fea
🔹 Key Features: Key Features:
Feature Feature
...@@ -1008,7 +1008,7 @@ Shows recent HTTP requests and responses ...@@ -1008,7 +1008,7 @@ Shows recent HTTP requests and responses
🔹 How to Use It How to Use It
Step 1: Add the dependency to your pom.xml or build.gradle Step 1: Add the dependency to your pom.xml or build.gradle
...@@ -1054,6 +1054,180 @@ properties ...@@ -1054,6 +1054,180 @@ properties
management.endpoints.web.exposure.include=health,info,metrics management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=always management.endpoint.health.show-details=always
A.18) The @ComponentScan annotation in Spring (typically used in a configuration class) tells the Spring container where to look for components, such as classes annotated with @Component, @Service, @Repository, or @Controller, so that they can be registered as Spring beans.
Purpose of @ComponentScan:
Automatic Bean Detection: It enables automatic discovery and registration of beans from specified packages.
Defines Base Packages to Scan: You can specify which packages Spring should scan for annotated classes.
How It Works
Spring uses @ComponentScan during context initialization (usually in conjunction with @Configuration) to scan the given base package(s) for classes annotated with stereotypes (@Component, @Service, etc.) and register them as beans in the application context.
Example
@Configuration
@ComponentScan(basePackages = "com.example.myapp")
public class AppConfig {
}
Here, Spring will scan the com.example.myapp package and its sub-packages for components.
Any class in that package marked with @Component, @Service, etc., will be registered as a Spring bean.
Default Behavior
If you do not specify a base package:
@ComponentScan
Then Spring will scan the package of the class where @ComponentScan is declared (and its sub-packages).
Summary
@ComponentScan is essential for enabling component-based development in Spring. It automates bean discovery, reduces boilerplate configuration, and supports modular application design.
A.19)Here’s a clear comparison between REST and SOAP, focusing on their key differences:
Feature REST (Representational State Transfer) SOAP (Simple Object Access Protocol)
Protocol Not a protocol, but an architectural style A protocol based on XML and standards
Data Format Supports multiple formats: JSON (commonly), XML, etc. Only XML
Transport Uses HTTP/HTTPS (typically) Can use HTTP, SMTP, TCP, etc.
Message Size Lightweight (especially with JSON) Heavy due to XML and strict structure
Performance Generally faster and more efficient Slower because of XML parsing and processing
Ease of Use Simpler to implement and consume More complex with strict specifications
Security Uses HTTPS and custom implementations (lighter) Built-in standards like WS-Security, more robust and complex
Standardization Less standardized; more flexible Highly standardized with WSDL, XSD, etc.
State Stateless (each call independent by default) Can be stateless or stateful
Error Handling Uses HTTP status codes Has its own detailed error structure via XML
Tooling Support Supported by most modern web frameworks easily Requires tools like WSDL for service definition
Use Cases Best for public APIs, mobile/web apps Often used in enterprise applications with complex transactions
Spring Rest Basics
A.1) The difference between @RestController and @Controller in Spring MVC is mainly about how the response is handled and what kind of web applications they are used for.
@Controller
Used in traditional web applications where you return views (like JSP, Thymeleaf).
Methods typically return a view name (like a template or JSP page).
If you want to return data (like JSON), you need to annotate the method with @ResponseBody.
Example:
@Controller
public class MyController {
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello from Controller");
return "hello"; // returns view named 'hello.jsp' or 'hello.html'
}
}
To return JSON or other data from @Controller:
@Controller
public class MyApiController {
@GetMapping("/api/greet")
@ResponseBody
public String greet() {
return "Hello from API";
}
}
@RestController
Introduced in Spring 4.
It is a convenience annotation that combines:
@Controller + @ResponseBody
Used in REST APIs where you return data (typically JSON or XML) directly.
All methods return the response body directly—no need for @ResponseBody on each method.
Example:
@RestController
public class MyRestController {
@GetMapping("/api/hello")
public String hello() {
return "Hello from RestController"; // returned as JSON or plain text
}
}
A.2)In Spring, request mapping refers to the process of directing HTTP requests (like GET, POST, PUT, DELETE) to the appropriate controller methods. This is handled using annotations—primarily @RequestMapping and its shortcut variants like @GetMapping, @PostMapping, etc.
Basic Concept
Spring uses the DispatcherServlet to intercept HTTP requests. Based on URL patterns and HTTP methods, it maps them to specific methods in controller classes using mapping annotations.
Common Request Mapping Annotations
Annotation Description
@RequestMapping General-purpose mapping for any method
@GetMapping Shortcut for @RequestMapping(method=GET)
@PostMapping Shortcut for POST requests
@PutMapping Shortcut for PUT requests
@DeleteMapping Shortcut for DELETE requests
@PatchMapping Shortcut for PATCH requests
Example with @RequestMapping
@Controller
@RequestMapping("/api")
public class MyController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String sayHello() {
return "Hello!";
}
}
This maps GET /api/hello to sayHello().
A.4)1. Controller Annotations
@RestController
Combines @Controller and @ResponseBody.
Marks the class as a REST controller, meaning that returned objects are written directly to the HTTP response body (typically as JSON or XML).
@RestController
public class MyRestController { }
@Controller
Marks the class as a web controller.
Used for returning views (like JSP or Thymeleaf) in web applications.
2. Request Mapping Annotations
These map HTTP requests to specific controller methods.
Annotation HTTP Method Description
@RequestMapping Any General-purpose mapping; can specify method, path, headers, etc.
@GetMapping GET Shortcut for @RequestMapping(method = RequestMethod.GET)
@PostMapping POST Shortcut for RequestMethod.POST
@PutMapping PUT Shortcut for RequestMethod.PUT
@DeleteMapping DELETE Shortcut for RequestMethod.DELETE
@PatchMapping PATCH Shortcut for RequestMethod.PATCH
Example:
@GetMapping("/users/{id}")
public User getUser(@PathVariable int id) {
}
A.1) B @RestController
A.2) D.Java object serialized to JSON
A.3) B.Automatically if Jackson is on classpath
A.4) C.201 Created
A.5) C.Solves CORS issues
A.6) A.@PathVariable
A.7) B. /products?limit=5 returns 5 items
A.8) B. Returns 500 Internal Server Error
A.9) A. Add @Validated on method
A.10)A. RESTful endpoint for File upload
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment