"Welcome To Ashok IT" "Spring Boot & Microservices" Topic : Rest API Development - PUT & PATCH Request Mapping Date : 14/06/2025 (Session - 67) _____________________________________________________________________________________________________________________________ Yesterday session ================= * We completed various request methods of Rest API Development Get Request >>>>>> @GetMapping Post Request >>>>> @PostMapping Put Request >>>>> @putmapping Patch Request >>>> @PatchMapping Delete Request >>> @DeleteMapping Today Session ============= * Discussed about FileUploading through Rest API development package com.ashokit.service; import java.util.List; import org.springframework.web.multipart.MultipartFile; import com.ashokit.dto.PersonDTO; import com.ashokit.response.PageResponse; public interface PersonService { //supporting for uploading public String uploadProfilePicture(String imageLocation, MultipartFile multipartImage) throws Exception; } package com.ashokit.service; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.time.LocalDate; import java.util.List; import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.ashokit.dao.PersonDao; import com.ashokit.dto.PersonDTO; import com.ashokit.entity.Person; import com.ashokit.exceptions.ResourceNotFoundException; import com.ashokit.response.PageResponse; @Service public class PersonServiceImpl implements PersonService { @Autowired private PersonDao personDao; @Autowired private ModelMapper modelMapper; @Override public String uploadProfilePicture(String imageLocation, MultipartFile multipartImage) throws Exception{ //getting the original FileName i.e.,UserUploadedImage file name String originalFilename = multipartImage.getOriginalFilename(); System.out.println("Original File Name::::" + originalFilename); //Renaming the File Name String randomID = UUID.randomUUID().toString(); System.out.println("RandomID:::::" + randomID); //user.jpeg,user_profile.png String substringfileName= originalFilename.substring(originalFilename.lastIndexOf("."));//.png System.out.println("SubString file name::::" + substringfileName); String renamedFileName = randomID.concat(substringfileName); System.out.println("RenamedFileName:::::" + renamedFileName); //FilePath to be stored i.e.,profilepics-1/adafafafafafewrewrewrwerwrewrwerew.png String destinationFilePath = imageLocation+File.separator+renamedFileName; System.out.println("Destination File Path::::" + destinationFilePath); //creating the folder if not exists File fileFolder = new File(imageLocation);//profilepics if(!fileFolder.exists()) fileFolder.mkdir(); //Copying the file Files.copy(multipartImage.getInputStream(), Paths.get(destinationFilePath)); return renamedFileName; } } PersonController.java ===================== package com.ashokit.controller; import java.time.LocalDateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.ashokit.constants.ApplicationConstants; import com.ashokit.dto.PersonDTO; import com.ashokit.exceptions.ErrorDetails; import com.ashokit.exceptions.ResourceNotFoundException; import com.ashokit.response.PageResponse; import com.ashokit.service.PersonService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; @RestController @RequestMapping("/api/persons") @Tag(name="Person",description = "Person Controller Rest API Methods") public class PersonController { @Autowired private PersonService personService; //Getting image location @Value("${project.images}") private String imageLocation; //Rest API Method for Uploading image @PostMapping("/upload") public ResponseEntity uploadProfileImage(@RequestParam("profileImage") MultipartFile multipartImage) throws Exception { String renamedFileName = personService.uploadProfilePicture(imageLocation, multipartImage); return new ResponseEntity(renamedFileName, HttpStatus.OK); } } PostMan Testing =============== 1) select Post -> http://localhost:8899/api/persons/upload 2) Select form-data profileImage <->file ----> brows the file location 3) Hit the API Response ======== c8c39be4-feee-4d16-a038-5b5ec2dab2a2.jpg Exception Handling in Spring MVC/SpringBootMVC/SpringRest/SpringBootRest ======================================================================== * In Spring framework/SpringBoot having two techniques to Handle Exceptions 1) Global Exception handling (or) Application level exception handler 2) Controller level exception handler * If exception handler method defined in seperate java class with annotation of "@RestControllerAdvice/@ControllerAdvice" will comes under Global exception handling. Example ======= package com.ashokit.exceptions; import java.time.LocalDateTime; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; //Global Exception Handling (or) Application level Exception Handling @RestControllerAdvice public class ApplicationErrorHandling { @ExceptionHandler(ResourceNotFoundException.class) public ResponseEntity handlingResourceNotFoundException(ResourceNotFoundException rnfe){ ErrorDetails errorDetails = ErrorDetails.builder().errorTime(LocalDateTime.now()) .errorMessage(rnfe.getMessage()) .errorStatus("Resource Not Found.....") .build(); return new ResponseEntity(errorDetails,HttpStatus.NOT_FOUND); } @ExceptionHandler(Exception.class) public ResponseEntity handlingException(Exception e){ ErrorDetails errorDetails = ErrorDetails.builder().errorTime(LocalDateTime.now()) .errorMessage(e.getMessage()) .errorStatus("Problem Occured while Executing Resource") .build(); return new ResponseEntity(errorDetails,HttpStatus.INTERNAL_SERVER_ERROR); } } * If exception handler method(@ExceptionHandler) defined inside the controller then will comes under "Controller level exception handler". Example ======= //Controller level Exception Handler @ExceptionHandler(ResourceNotFoundException.class) public ResponseEntity handlingResourceNotFoundException(ResourceNotFoundException rnfe){ ErrorDetails errorDetails = ErrorDetails.builder().errorTime(LocalDateTime.now()) .errorMessage(rnfe.getMessage()) //Person not found with personId : 1256 .errorStatus("Resource Not Found.....") .build(); return new ResponseEntity(errorDetails,HttpStatus.NOT_FOUND); } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++