[go: up one dir, main page]

0% found this document useful (0 votes)
10 views20 pages

FSD NM

Uploaded by

sr2079673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

FSD NM

Uploaded by

sr2079673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

NAAN MUDHALVAN

FULL STACK DEVELOPMENT WITH JAVA

BHARATHIDASAN ENGINEERING COLLEGE


(Approved by AICTE and Affiliated with Anna University, Chennai – 600 025)

Nattrampalli, Tirupattur District – 635 854

NOV & DEC - 2024

DEPARTMENT OF
INFORMATION TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY

5105 –BHARATHIDASAN ENGINEERING COLLEGE, NATTRAMPALLI

ANNA UNIVERSITY, CHENNAI - 600025

FULL STACK DEVELOPMENT WITH JAVA

A PROJECT REPORT

Submitted by

--------------------------- ---------------------------

In partial fulfillment of for the awarded of the degree

BACHELOR OF TECHNOLOGY

IN

INFORMATION TECHNOLOGY
BHARATHIDASAN ENGINEERING COLLEGE
(Approved by AICTE and Affiliated with Anna University, Chennai – 600 025)

Nattrampalli, Tirupattur District – 635 854

BONAFIDE CERTIFICATE

Certified that this Naan Mudhalvan report "FULL STACK DEVELOPMENT


WITH JAVA" is the bonafide work of ______________________ who carried
out the project work under my supervision.

Certified that candidates were examined in the viva – voce examination held at
Bharathidasan Engineering College, Nattrampalli on ________________________.

SIGNATURE SIGNATURE

Mr.R. VASANTHI, M.E, PhD Mr. S.THAMILVANAN M.Ε.,


HEAD OF THE DEPARTMENT SUPERVISOR, Assistant Professor
Department of IT Department of IT
BEC, NATTRAMPALLI – 635 854. BEC, NATTRAMPALLI – 635 854.

SIGNATURE

Mr. M VIJAY SURESH, M.E.,

SPOC, Assistant Professor,

Department of ECE, BEC, NATTRAMPALLI – 635 854.

INTERNAL EXAMINER EXTERNAL EXAMINER


ACKNOWLEDGEMENT

We wish our heartfelt thanks to our respected Management for the Blessings and
constant support over our project period.

We wish to express our sincere thanks to our respected Principal Dr. G. BASKAR
M.E., Ph.D., FIE. For all the blessing and help provided during the period of
project work.

We are in deep gratitude to our department faculty who always been supporting us
through thick and thin respected HEAD OF THE DEPARTMENT Mrs. R.
VASANTHI, M.E ,PhD., for the continuous support for the project.

We wish to express our sincere thanks to our respected PROJECT GUIDE Mr.
S.THAMILVANAN, M.E., Assistant Professor for her constant help and creative
ideas to Complete this project.

We would like to extend warmest thanks to all our Department Faculty Members
and supporting faculties for helping this project's successful completion
unflinching support and encouragement from the member of our Family and
Friends. We must thank them all from our depth and heart.
FULL STACK DEVELOPMENT
WITH JAVA

S.NO TABLE OF CONTENTS PAGE NO

1 Introduction 1

Spring 5 Basic with spring


2 3
Boot

3 Spring Data JPA with Boot 10

4 Spring REST 15

5 Conclusion 17

6 Certificates 20
INTRODUCTION:

Full stack development encompasses the complete process of application


software development, including both the front-end and back-end development. The
front end consists of the user interface (or UI), and the back end handles the
business logic and application workflows that run behind the scenes.

Full stack developers possess the skills and knowledge to work across the entire
technology stack, enabling seamless user experiences and developing robust
backends.

The front end is the face of a web application, the part that users interact with
directly. Full stack web developers possess a deep understanding of front-end
technologies, including HTML, CSS, and JavaScript. They leverage these
foundational languages to structure, style, and enhance the visual appeal of web
pages.

Full stack developers are proficient in server-side languages such as Python, Ruby,
PHP, and JavaScript, allowing them to build robust and scalable back-end systems.
Back-end developers play a crucial role in designing and implementing the
application's core functionality, handling data management, and ensuring smooth
integration with databases like MySQL, MongoDB, or PostgreSQL.

Back-end development involves more than just writing code. Full stack developers
have a keen understanding of server architecture and API development. They design
and implement RESTful APIs that enable seamless communication between the
front-end and back-end components of the application.
SPRING 5 BASIC WITH SPRING BOOT:
1. Spring Boot Basics:

 Spring Boot simplifies Java application development by providing a


convention-over-configuration approach.

 Auto-Configuration: It auto-configures beans based on the jar dependencies


in your project, reducing the need for boilerplate configurations.

 Embedded Server: Spring Boot can bundle an embedded Tomcat, Jetty, or


Undertow server, so you can run applications standalone.

 Application Properties: Settings can be customized in an


application.properties or application.yml file.

2. Getting Started with Spring Boot:

 Use Spring Initializr to create a new Spring Boot project with dependencies
(https://start.spring.io).

 Add dependencies like spring-boot-starter-web for web applications or


spring-boot-starter-data-jpa for data persistence.

3. Creating a Simple REST API:

 Controller: Define REST endpoints using @RestController and


@RequestMapping.

 Service: Business logic goes into service classes annotated with @Service.

 Repository: Data access is managed through repository interfaces, usually


extending JpaRepository or CrudRepository.
Example Controller:

@RestController

@RequestMapping("/api")

public class HelloController {

@GetMapping("/hello")

public String sayHello() {

return "Hello, Spring Boot with Spring 5!";

4. Dependency Injection and Components:

 Use @Component, @Service, and @Repository to create beans, which


Spring manages automatically.

 Inject dependencies using @Autowired in constructors or fields.

5. Spring Boot Testing:

 Spring Boot provides testing utilities with the spring-boot-starter-test.

 Use @SpringBootTest for integration tests and @WebMvcTest for controller


layer tests.

6. Running the Application:

Use mvn spring-boot:run or run the main method in the


@SpringBootApplication-annotated class to start your app.
7. Spring Boot DevTools:

 Add spring-boot-devtools for hot-reloading, which helps with development


efficiency.

SPRING DATA JPA WITH BOOT:

1. Add Dependencies:

 In your pom.xml (for Maven) or build.gradle (for Gradle), add the spring-
boot-starter-data-jpa dependency:

<!-- For Maven -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

 This includes JPA and an embedded H2 database, useful for quick testing.

2. Configure Database Connection:

 In src/main/resources/application.properties (or application.yml), configure


your database:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true # Enables the H2 database console
 For production, replace H2 with a database like MySQL or PostgreSQL and
configure accordingly.

3. Define the Entity:

 Create a Java class and annotate it with @Entity to map it to a database table:

import javax.persistence.*;

@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Double price;

// Getters and setters


}

4. Create a Repository Interface:

 Create an interface that extends JpaRepository, specifying the entity and


primary key types:

import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product, Long> {


// Custom query methods can be defined here if needed
}

Spring Data JPA will provide basic CRUD operations out of the box.

5. Service Layer (Optional):


 Although not required, it’s good practice to add a service layer to handle
business logic:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;

public List<Product> findAllProducts() {


return productRepository.findAll();
}

public Product saveProduct(Product product) {


return productRepository.save(product);
}

// Additional business logic methods


}

6. Controller Layer:

 Create a REST controller to expose endpoints for interacting with your data:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping
public List<Product> getAllProducts() {
return productService.findAllProducts();
}

@PostMapping
public Product createProduct(@RequestBody Product product) {
return productService.saveProduct(product);
}
}

7. Run the Application:

 Start the application using mvn spring-boot:run or by running the main


method in the @SpringBootApplication class.

The API endpoints will be accessible, and you can interact with the database
through CRUD operations.

8. Query Methods and Custom Queries:

Spring Data JPA allows for query methods using a naming convention:

List<Product> findByName(String name);


List<Product> findByPriceGreaterThan(Double price);

For custom queries, you can use @Query:

@Query("SELECT p FROM Product p WHERE p.price > :price")


List<Product> findProductsByPrice(@Param("price") Double price);

9. Testing with H2 Database:


 Using an H2 database makes it easy to test your application since it runs in-
memory.

You can access the H2 console at http://localhost:8080/h2-console (make


sure spring.h2.console.enabled=true in your properties).

SPRING REST:

1.Setting Up Dependencies:

To create a REST API, you’ll need the spring-boot-starter-web dependency.


It includes the necessary libraries for building RESTful web services.

<!-- For Maven -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. Creating a Basic REST Controller:

REST controllers in Spring are created using @RestController, which


combines @Controller and @ResponseBody (to convert response data into
JSON).

Define request mappings using @RequestMapping, @GetMapping,


@PostMapping, @PutMapping, and @DeleteMapping.

Example REST Controller:

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/products")
public class ProductController {
@GetMapping("/{id}")
public Product getProductById(@PathVariable Long id) {
// Fetch product from service layer (mock example here)
return new Product(id, "Sample Product", 29.99);
}

@PostMapping
public Product createProduct(@RequestBody Product product) {
// Save product to the database (mock example here)
return product;
}

@PutMapping("/{id}")
public Product updateProduct(@PathVariable Long id, @RequestBody
Product product) {
// Update product in the database (mock example here)
product.setId(id);
return product;
}

@DeleteMapping("/{id}")
public String deleteProduct(@PathVariable Long id) {
// Delete product from the database (mock example here)
return "Product with ID " + id + " deleted successfully";
}
}

3. Request and Response Annotations:

@PathVariable: Binds a URI path parameter to a method parameter.

@RequestBody: Binds the body of a POST/PUT request to a method


parameter, typically used for JSON payloads.

@ResponseBody: Converts the return type to JSON/XML and sends it as the


response (implied by @RestController).

@RequestParam: Binds query parameters in the URL to method parameters.


4. Service Layer (Optional):

 It’s common to have a service layer in Spring applications, separating


business logic from controllers.

Here’s an example service:

import org.springframework.stereotype.Service;

@Service
public class ProductService {
public Product getProductById(Long id) {
// Simulate fetching product from a data source
return new Product(id, "Sample Product", 29.99);
}

// Additional CRUD operations


}

5. Exception Handling

 Spring provides @ExceptionHandler to manage custom exceptions, but


@ControllerAdvice allows global exception handling across all controllers.

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleNotFoundException(ResourceNotFoundException ex)
{
return ex.getMessage();
}}
6. Using ResponseEntity

 ResponseEntity provides control over the HTTP status and headers of a


response.

Example:

@GetMapping("/{id}")
public ResponseEntity<Product> getProductById(@PathVariable Long id) {
Product product = productService.getProductById(id);
return product != null
? ResponseEntity.ok(product)
: ResponseEntity.notFound().build();
}

7. Testing the API with Postman or Curl

 Run the Spring Boot application, then test endpoints using Postman or a tool
like curl:

curl -X GET http://localhost:8080/api/products/1

8. JSON/XML Serialization

 Spring Boot automatically converts Java objects to JSON for the response.
You can customize the serialization with Jackson annotations like
@JsonIgnore or @JsonProperty.

9. Advanced Features

 Pagination and Sorting: Use Spring Data JPA with Pageable and Sort to
handle paginated and sorted responses.
 HATEOAS: For Hypermedia support, you can add links to resources, helping
clients navigate the API more effectively.

 Versioning: For larger APIs, you may want to version your endpoints (e.g.,
/api/v1/products).

10. Running and Testing

 Run the application using mvn spring-boot:run or by executing the main


method in the @SpringBootApplication class.

 This setup gives you a basic REST API in Spring Boot, ready for
enhancements and scaling.

CONCLUSION:
Spring 5 with Spring Boot simplifies application setup and configuration, offering
embedded servers, auto-configuration, and a rich set of dependencies that speed up
development while maintaining best practices. This approach is particularly useful
for creating microservices or standalone applications.Spring Data JPA with Spring
Boot abstracts much of the complexity of database interaction.It provides an
intuitive way to manage data using repositories, minimizing boilerplate code for
CRUD operations.Its integration with various databases, automatic query creation,
and support for custom queries make data handling efficient and developer-
friendly.Spring REST leverages Spring MVC to build RESTful APIs, enabling easy
exposure of business logic to clients. With Spring’s annotations (@RestController,
@RequestMapping, etc.), creating clean, readable, and maintainable APIs becomes
straightforward. Spring REST supports essential features such as request/response
handling, exception management, and integration with JSON and XML, making it a
go-to choice for backend API development.
CERTIFICATES:

1) Spring Data JPA with Boot


2) Spring REST
3) Spring 5 Basics with Spring Boot

You might also like