Skip to content

This repo was made to try spring boot project with kotlin lenguaje

Notifications You must be signed in to change notification settings

RusinToustau/spring-boot-with-kotlin-exaple

Repository files navigation

ProductExample

Api Documentation : Swagger2

Swagger2 http://localhost:8080/swagger-ui/

@ApiOperation("Get all Entities")

Esta herramienta permite documentar la api de manera sencilla. Con tan solo importar su dependencia podremos obtener y agregar una configuración automáticamente tendremos ducuemtados los endepoints y autogenerados curls de ejemplo.

	// -------------------- Documentation -------------------- //
	implementation ("io.springfox:springfox-boot-starter:3.0.0")
 
@Configuration
class SpringFoxConfig {

    @Bean
    fun api(): Docket = Docket(DocumentationType.SWAGGER_2)
            .select()
            .paths(PathSelectors.any())
            .build()

}

Finalmente en app deberemos agregar la notaction @EnableSwagger2, en nuestro caso :

@SpringBootApplication
@EnableSwagger2
class ProductExampleApplication
  ...

Validations

How to use Java Bean Validation in Spring Boot

APi Documentation

 data class Product(
    @get:Size(min = 3, max = 20)
    val name: String,
    @get:Min(value = 0)
    val price: Double) {
    
    ...
 }
    @PostMapping
    fun save(@Valid @RequestBody body: T): ResponseEntity<Boolean> {
        ...
    }

Manejo de Errores

@ControllerAdvice
class RestResponseEntityErrorHandler: ResponseEntityExceptionHandler() {

    override fun handleMethodArgumentNotValid(ex: MethodArgumentNotValidException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
        val result: Map<String, List<String?>> = ex.bindingResult.fieldErrors.groupBy({it.field},{it.defaultMessage})
        return ResponseEntity
                .status(status)
                .headers(headers)
                .body(result)
    }
    
    @ExceptionHandler(FileNotFoundException::class)
    fun customExceptionExample(fileNotFoundException: FileNotFoundException): ResponseEntity<String> {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("UPPS!")
    }
}

About

This repo was made to try spring boot project with kotlin lenguaje

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages