RELL-1 add controller and handler

This commit is contained in:
Popov Aleksander 2024-02-04 04:07:22 +06:00
parent 094f73b277
commit 40358c8066
4 changed files with 120 additions and 2 deletions

35
pom.xml
View File

@ -26,6 +26,11 @@
<spring.boot.version>3.2.2</spring.boot.version>
<jacoco.version>0.8.11</jacoco.version>
<junit.version>5.10.1</junit.version>
<swagger-annotations.version>2.2.2</swagger-annotations.version>
<jackson-databind-nullable.version>0.2.2</jackson-databind-nullable.version>
<validation-api.version>2.0.1.Final</validation-api.version>
<javax-annotation-api.version>1.3.2</javax-annotation-api.version>
<springfox-swagger2.version>3.0.0</springfox-swagger2.version>
</properties>
<developers>
@ -53,6 +58,7 @@
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
@ -91,6 +97,33 @@
<version>${version.mapstruct}</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations.version}</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>${jackson-databind-nullable.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation-api.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax-annotation-api.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
@ -105,7 +138,6 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -162,7 +194,6 @@
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,46 @@
package ru.rell.controller;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ru.rell.handler.exception.CustomException;
import java.io.IOException;
@RestController
@RequestMapping("api/v1/recognize")
@RequiredArgsConstructor
public class RecognizeController {
@Operation(
summary = "Распознование текста из фаила вернет txt; attachment; filename= имя фаила",
description = "Распознование текста из фаила вернет txt; attachment; filename= имя фаила"
)
@PostMapping(produces = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Resource> recognize(@RequestPart("file") MultipartFile file){
if (file.isEmpty()) {
return ResponseEntity.badRequest().build();
}
ByteArrayResource resource = null;
try {
resource = new ByteArrayResource(file.getBytes());
} catch (IOException e) {
throw new CustomException(e.getMessage(),e);
}
resource.getFilename();
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"" + file.getOriginalFilename() + "\"")
.body(resource);
}
}

View File

@ -0,0 +1,31 @@
package ru.rell.handler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.boot.actuate.cache.NonUniqueCacheException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import ru.rell.handler.exception.CustomException;
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleGlobalException(Exception ex) {
log.error(ex.getMessage(), ExceptionUtils.getStackTrace(ex));
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Сервис временно не доступен");
}
@ExceptionHandler(CustomException.class)
public ResponseEntity<Object> handleCustomException(CustomException ex) {
log.error(ex.getMessage(), ExceptionUtils.getStackTrace(ex));
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Возникла ошибка при работе с сервисом");
}
}

View File

@ -0,0 +1,10 @@
package ru.rell.handler.exception;
import lombok.Getter;
@Getter
public class CustomException extends RuntimeException {
public CustomException(String message, Throwable cause) {
super(message, cause);
}
}