Post Validation didn't work with Spring-Boot Swagger RestAPI

Viewed 326

I'm currently working on a Cross Platform App to record the expenses and the incomes. So far so good. However, when I want to add a Backend Post Validation (so nobody can save incomes like €19,99999 etc.) the Post Validation does not work. It would be great if somebody could help me. Here are the details:

I am adding the springfox-bean-validation to my Backend

Entity

@ApiModel(
    value = "Expense",
    description =
    "Information of all expenses with cost, expense date as well as the purpose to which these expenses went"
)
@Entity
@Table(name = "Expenses")
data class Expense(
    @ApiModelProperty(notes = "Cost of the Expense in €")
    @DecimalMin(value = "0.0", inclusive = false, message = "Price have to be higher than 0.0")
    @Digits(integer = Int.MAX_VALUE, fraction = 2, message = "Price have only 2 decimal places")
    var cost: BigDecimal,
    @ApiModelProperty(notes = "Creation date of the Expense")
    @NotNull(message = "Date may not be Null")
    var date: LocalDate,
    @ApiModelProperty(notes = "Purpose of the expense")
    var purpose: String?
) : DbEntity() {
    constructor() : this(BigDecimal.ZERO, LocalDate.now(), "")
}

Controller

@CrossOrigin
@RestController
@Api(value = "/finance", description = "FinanceController", produces = "application/json")
@RequestMapping("/api/finance")
class FinanceController(
    private val expenseRepository: ExpenseRepository
) {

    @ApiOperation(value = "Create an Expense", response = Expense::class)
    @PostMapping("/expanse")
    fun createExpense(@RequestBody expenseInfo: Expense): Expense {
        return expenseRepository.save(expenseInfo)
    }

}

SpringFoxConfig

@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration::class)
class SwaggerConfiguration {

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

And that is the resulting Output from the swagger UI. Why are there no annotations like @NotNull or @Digits(...)?

That is the Output from the swagger UI

When I test a Post request, I get 200 as HTTP Response Code and no Error!

{
  "cost": 19.99999999,
  "date": "2020-06-06",
  "purpose": "test",
  "id": 15
}
0 Answers
Related